3
\$\begingroup\$

This is pretty much one of my first programs that I have created in Java and I just wanted to ask if anyone sees some obvious errors or mistakes I made.

The purpose of this program is to sort numbers given by a user using an algorithm.

First off, the program gets an input in form of ints from the user, I have used a popup here, and than creates an array arr1 with that size. After that, another popup opens that asks the user for the 1st number he wants to sort. Then the 2nd, 3rd, etc, until the array has been filled with ints.

The array gets transferred into the sorting algorithm which, surprise, sorts the numbers into a different array arr2.

This array then gets passed into a method that takes a static frame and puts a text field with the numbers (in correct order) onto it. [+ 1 close and 1 repeat button]

If the repeat method (buttonRetry.addActionListener.actionPerformed) is called / the button is pressed, the frame gets "cleaned" (main_frame.getContentPane().removeAll();) and the main() method is called again.

Is it possible to make the process of this happening (getting input from the user and sorting that input) faster and do you have any additional advice about what and how I did this (Noob errors, graphical tips, etc.).

Here is the code :

import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.Color; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class AWTCounter extends JFrame { public static int[] arr1; public static JFrame frame = new JFrame("Super Simple Sorting Program"); public static JFrame main_frame = new JFrame("Super Simple Sorting Program"); public void main() { String input_1 = JOptionPane.showInputDialog(frame, "How many numers do you want to sort with SSSP ?"); if (input_1 == null) {dispose(); System.exit(0);} int checksum = Integer.parseInt(input_1); arr1 = new int[checksum]; int logNumber = 0; int debug = checksum; checksum = 0; while (checksum <= debug-1) { logNumber++; String input_2 = JOptionPane.showInputDialog(frame, "Please enter your " + logNumber + " . number."); int member = Integer.parseInt(input_2); arr1 [checksum] = member ; checksum++; } int[] arr2 = doSelect(arr1); print(arr2); } public int[] doSelect(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { int index = i; for (int j = i + 1; j < arr.length; j++) if (arr[j] < arr[index]) index = j; int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; } return arr; } public void print(int[] arr2) { JTextField t2; t2 = new JTextField("Here are your sorted numbers : " + Arrays.toString(arr2)); main_frame.add(t2, BorderLayout.NORTH); JButton buttonEnd = new JButton("Close apllication"); main_frame.add(buttonEnd, BorderLayout.CENTER); JButton buttonRetry = new JButton("Try again !"); main_frame.add(buttonRetry, BorderLayout.SOUTH); main_frame.setLayout(new FlowLayout()); main_frame.setSize(600,300); main_frame.setVisible(true); buttonEnd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { dispose(); System.exit(0); } }); buttonRetry.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { main_frame.getContentPane().removeAll(); main(); } }); } } 
\$\endgroup\$
1

1 Answer 1

5
\$\begingroup\$

Im impressed that you used selection sort. The standard bubble sort method would be slower. Have a look at the bubble sort method anyway.

int n = arr.length, r; for (int i = 0; i < n; i++) { for (int (j = i + 1); j < n; j++) { if (arr[i] > arr[j]) { r = arr[i]; arr[i] = arr[j]; arr[j] = r; } } } 

You could also have used the default Arrays.sort method

Arrays.sort(arr); 

You are creating an applet. In this case, it would probably be good to declare the main method as static

When you are importing the packages, why have you imported the individual classes after importing them by wildcard character '*'; There is no need for it. That is redundant and the computer wastes time re-importing and garbage-collecting. Just do:

import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; 

Also, printing mistake here:

String input_1 = JOptionPane.showInputDialog(frame, "How many numbers do you want to sort with SSSP ?"); 

Consider setting bounds to the buttons and text boxes in your mainframes, to make the locations more exact. That way, it will be easier to add a new GUI component to the window when you want to add it. The command goes: <name of object>.setBounds(//x-axis from left, //y-axis from top, //width, //height) For example:

t2 = new JTextField("Here are your sorted numbers : " + Arrays.toString(arr2)); t2.setBounds(20, 40, 75, 10); main_frame.add(t2); JButton buttonEnd = new JButton("Close application"); t2.setBounds(95, 40, 75, 10); main_frame.add(buttonEnd); JButton buttonRetry = new JButton("Try again !"); t2.setBounds(180, 40, 75, 10); main_frame.add(buttonRetry); 

WHY have you named your class AWTCounter? That makes me think you are counting numbers with certain properties or words in a sentence. Won't AWTSorter be better?

Great program though, especially if it's your first!

\$\endgroup\$
1
  • \$\begingroup\$@greybeard yelp looks like I've done a mistake.\$\endgroup\$CommentedDec 7, 2019 at 12:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.