patternjavaMinor
Java application for comparing sorting methods
Viewed 0 times
sortingcomparingapplicationjavamethodsfor
Problem
I was asked to make a Java application with main class q2.
How can I make it better and more efficient?
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Sort extends q2
{
public static double SWITCH;
}
class q2 extends JFrame
{
public static void main(String[] args)
{
q2 window = new q2();
Container cont = window.getContentPane();
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
window.setVisible(true);
window.setSize(dim);
JPanel LeftPanel = new JPanel();
JPanel RightPanel = new JPanel();
JPanel MiddlePanel = new JPanel();
JPanel Middle1Panel =new JPanel();
JPanel Middle2Panel = new JPanel();
JPanel Middle3Panel = new JPanel();
MiddlePanel.add(Middle1Panel);
MiddlePanel.add(Middle2Panel);
MiddlePanel.add(Middle3Panel);
cont.setLayout(new GridLayout(1,3));
cont.add(LeftPanel);
cont.add(MiddlePanel);
cont.add(RightPanel);
LeftPanel.setBackground(Color.red);
RightPanel.setBackground(Color.black);
JRadioButton BubbleButton = new JRadioButton("BubbleSort");
JRadioButton SelectionButton = new JRadioButton("SelectionSort");
JRadioButton InsertionButton = new JRadioButton("InsertionSort");
RightPanel.add(BubbleButton);
RightPanel.add(SelectionButton);
RightPanel.add(InsertionButton);
ButtonGroup Group1 = new ButtonGroup();
Group1.add(InsertionButton);
Group1.add(SelectionButton);
Group1.add(BubbleButton);
BubbleButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e)
{
Sort.SWITCH = 1;
}
});
SelectionButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e)
How can I make it better and more efficient?
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Sort extends q2
{
public static double SWITCH;
}
class q2 extends JFrame
{
public static void main(String[] args)
{
q2 window = new q2();
Container cont = window.getContentPane();
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
window.setVisible(true);
window.setSize(dim);
JPanel LeftPanel = new JPanel();
JPanel RightPanel = new JPanel();
JPanel MiddlePanel = new JPanel();
JPanel Middle1Panel =new JPanel();
JPanel Middle2Panel = new JPanel();
JPanel Middle3Panel = new JPanel();
MiddlePanel.add(Middle1Panel);
MiddlePanel.add(Middle2Panel);
MiddlePanel.add(Middle3Panel);
cont.setLayout(new GridLayout(1,3));
cont.add(LeftPanel);
cont.add(MiddlePanel);
cont.add(RightPanel);
LeftPanel.setBackground(Color.red);
RightPanel.setBackground(Color.black);
JRadioButton BubbleButton = new JRadioButton("BubbleSort");
JRadioButton SelectionButton = new JRadioButton("SelectionSort");
JRadioButton InsertionButton = new JRadioButton("InsertionSort");
RightPanel.add(BubbleButton);
RightPanel.add(SelectionButton);
RightPanel.add(InsertionButton);
ButtonGroup Group1 = new ButtonGroup();
Group1.add(InsertionButton);
Group1.add(SelectionButton);
Group1.add(BubbleButton);
BubbleButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e)
{
Sort.SWITCH = 1;
}
});
SelectionButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e)
Solution
Unless you absolutely must keep everything in
If it were up to me, I think I'd split it out a bit more than this as well, with one function to create the UI, and separate one to update the UI with results. In reality, a lot of creating the UI is also pretty much identical code repeated three times, so you probably want to split that into a couple of pieces as well, with one to handle only the repeated part (that you can call three times) and another that handles the parts that aren't repeated per-button.
If you want it to be nicer from an OO viewpoint, you could go a bit further than that, and define a
main, this should probably be split up into a number of separate functions. At the very least, I'd think of:- One function for the UI.
- One function for each sort.
- One function to randomize the data.
If it were up to me, I think I'd split it out a bit more than this as well, with one function to create the UI, and separate one to update the UI with results. In reality, a lot of creating the UI is also pretty much identical code repeated three times, so you probably want to split that into a couple of pieces as well, with one to handle only the repeated part (that you can call three times) and another that handles the parts that aren't repeated per-button.
If you want it to be nicer from an OO viewpoint, you could go a bit further than that, and define a
sort interface, and then have selectionSort, insertionSort and bubbleSort each implement that interface. Right now you also have essentially identical timing code in triplicate (once for each sort); you'd probably be better off isolating that in one place as well, and applying it to each sort as you run it.Context
StackExchange Code Review Q#3902, answer score: 8
Revisions (0)
No revisions yet.