Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java: GUIs (Sorting) Write a visual display of Selection Sort and Insertion Sort

ID: 660361 • Letter: J

Question

Java: GUIs (Sorting)

Write a visual display of Selection Sort and Insertion Sort. The GUI "steps through" the two sorting algorithms.

Program Description

Your program should display bars of various heights, corresponding to random values stored in an array.

The selection and insertion sort arrays should initially contain the same numbers.

A button allows the user to step through a single pass of each sorting algorithm.

When the sort is complete, the bars change color.

Notes

I've provided a shell for the GUI so you don't have to spend too much time focusing on how to get the bars to display.

Several places in the code have placeholders for you to add code: // ??? YOUR CODE HERE

You might also need to add code to additional places.

The code for the two sorting algorithms is posted on WebAccess.

This is the original code. You will need to modify it for the project.

For this program, you will need to "deconstruct" the sorting algorithms. You essentially want only one pass of the outer loop (for each algorithm) to happen with each click of the button. So think about how you can take apart that outer loop.

Where should you put the initialization part of the loop? This happens one time. Where should it go?

Where should you put the conditional check of the loop?

Where should you put the update condition of the loop?

Use private helper methods- at least one for each sort.

Don't forget to call repaint() when you want the display to update.

Extra Credit (10 points)

Initialize your arrays with unique random numbers (i.e., no number may appear more than once in your array).

Code Provided:

Explanation / Answer

public class SortGUI extends JFrame {

ButtonGroup typeGroup=new ButtonGroup(), sortGroup=new ButtonGroup();
// May be: int typeFlag, sortFlag;

// Stuff generated by JBuilder:
BorderLayout borderLayout1 = new BorderLayout();
JPanel textPanel = new JPanel();
JPanel typePanel = new JPanel();
JPanel sortPanel = new JPanel();
JPanel sortButPanel = new JPanel();
JTextField textField = new JTextField();
JLabel typeLabel = new JLabel();
GridLayout gridLayout1 = new GridLayout();
JRadioButton intType = new JRadioButton();
JRadioButton caseSenStrBut = new JRadioButton();
JRadioButton noCaseSenStrBut = new JRadioButton();
GridLayout gridLayout2 = new GridLayout();
JLabel sortLabel = new JLabel();
JRadioButton selSortBut = new JRadioButton();
JRadioButton insSortBut = new JRadioButton();
JRadioButton qSortBut = new JRadioButton();
JButton sortBut = new JButton();

public SortGUI() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
textField.setText("enter the list to be sorteed here");
textField.setColumns(30);
typeLabel.setText("Choose type of list");
typePanel.setLayout(gridLayout1);
gridLayout1.setColumns(1);
gridLayout1.setRows(4);
intType.setSelected(true);
intType.setText("Integer");
intType.addItemListener(new java.awt.event.ItemListener() {

public void itemStateChanged(ItemEvent e) {
type_itemStateChanged(e);
}
});
caseSenStrBut.setText("Case Sensitive String");
caseSenStrBut.addItemListener(new java.awt.event.ItemListener() {

public void itemStateChanged(ItemEvent e) {
type_itemStateChanged(e);
}
});
noCaseSenStrBut.setText("Case Insensitive String");
noCaseSenStrBut.addItemListener(new java.awt.event.ItemListener() {

public void itemStateChanged(ItemEvent e) {
type_itemStateChanged(e);
}
});
sortPanel.setLayout(gridLayout2);
gridLayout2.setColumns(1);
gridLayout2.setRows(4);
sortLabel.setText("Choose Sort Algorithm");
selSortBut.setSelected(true);
selSortBut.setText("Selection Sort");
selSortBut.addItemListener(new java.awt.event.ItemListener() {

public void itemStateChanged(ItemEvent e) {
sort_itemStateChanged(e);
}
});
insSortBut.setText("Insertion Sort");
insSortBut.addItemListener(new java.awt.event.ItemListener() {

public void itemStateChanged(ItemEvent e) {
sort_itemStateChanged(e);
}
});
qSortBut.setText("Quick Sort");
qSortBut.addItemListener(new java.awt.event.ItemListener() {

public void itemStateChanged(ItemEvent e) {
sort_itemStateChanged(e);
}
});
sortBut.setText("Sort Now");
sortBut.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {
sortBut_actionPerformed(e);
}
});
this.getContentPane().add(textPanel, BorderLayout.NORTH);
textPanel.add(textField, null);
this.getContentPane().add(typePanel, BorderLayout.WEST);
typePanel.add(typeLabel, null);
typePanel.add(intType, null);
typePanel.add(caseSenStrBut, null);
typePanel.add(noCaseSenStrBut, null);
this.getContentPane().add(sortPanel, BorderLayout.EAST);
sortPanel.add(sortLabel, null);
sortPanel.add(selSortBut, null);
sortPanel.add(insSortBut, null);
sortPanel.add(qSortBut, null);
this.getContentPane().add(sortButPanel, BorderLayout.CENTER);
sortButPanel.add(sortBut, null);

}

void type_itemStateChanged(ItemEvent e) {
// Here insert code to react to choice of type buttons.
// NOTE: I may have misspoken in class. You don't need to write code
// to instantiate comparable objects here. It may be better to have integer
// valued field, say typeFlag, and set it to, say 1 if integer is
// selected, 2 if case sensitive String is selected, and 3 if case
// sensitive String selected.
}

void sort_itemStateChanged(ItemEvent e) {
// Here insert code to react to choice of sort algorithm.
// NOTE: I may have misspoken in class. You don't need to write code
// to instantiate sorter objects here. It may be better to have integer
// valued field, say sortFlag, and set it to, say 1 if selection sort is
// selected, 2 if insertion sort is selected, and 3 if
// quick sort is selected.

}

void sortBut_actionPerformed(ActionEvent e) {
// Here you write code to react to pushing of button labeled "sort now".
// This is where the bulk of code should be written. You need to read
// the contents of the textField using one of get... methods in JTextField
// class. Then hand it to parseStr method I supplied to get an array of
// Strings. Then determine which type is chosen. If Integer is chosen you
// need to create an array of Integer and use it construct an appropriate
// comparable object. otherwise use the String array returned by parseStr
// to construct the appropriate comparable. Similarly from the sort algorithm
// button construct the appropriate sorter. Next you need to call the
// sorter objects sortIt() method. Finally you need to take the sorted
// list and write it to textField. To do so you may wish to delcare a
// String object, say s, and scan the sorted list and add each item to
// s, preferably separated by commas or spaces. Once s is filled up, then
// you need to put in textField, using one of its set... methods.
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote