Question

Design and implement an applicationSortNumbersthat has two buttons, and a label. Label the buttons Generate and...

Design and implement an applicationSortNumbersthat has two buttons, and a label. Label the buttons Generate and Sortseparately. Initially the label is 10 zeros separated by comma. When the user hits the button Generate, 10 numbers between 1 and 50 will berandomly generated and displayed using the label. When the user hits the button Sort, the 10 numbers will be sorted using selection sorting algorithm discussed in.

this is a java problem. Must have 2 separate java files one for main and one for Panel, thanks.

Please don't use substring method in your solution.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// SortNumbersGUI.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class SortNumbersGUI {
   static JFrame frame = new JFrame("Sort");
   static int arr[] = new int[10];

   SortNumbersGUI() {
       JPanel panel = new JPanel();
       panel.setLayout(null);

       // textfield to read input
       JLabel label2 = new JLabel("0,0,0,0,0,0,0,0,0,0");
       label2.setBounds(250, 50, 250, 30);

       // button to check
       JButton check = new JButton();
       check.setText("Generate numbers: ");
       check.setBounds(100, 200, 250, 20);
       JButton sort = new JButton();
       sort.setText("Sort");
       sort.setBounds(400, 200, 120, 20);
       sort.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent aE) {
               sort(arr);
               append(arr);
           }

           private void append(int[] arr) {
               String s = "";
               for (int i = 0; i < arr.length; i++) {
                   s = s + arr[i] + ",";

               }
               s.substring(0, s.length() - 1);
               label2.setText(s);

           }

           public void sort(int arr[]) {
               int N = arr.length;
               int i, j, pos, temp;
               for (i = 0; i < N - 1; i++) {
                   pos = i;
                   for (j = i + 1; j < N; j++) {
                       // finding the min position
                       if (arr[j] < arr[pos]) {
                           pos = j;
                       }
                   }
                   // swapping
                   temp = arr[i];
                   arr[i] = arr[pos];
                   arr[pos] = temp;
               }
           }
       });
       // action listener to check on click of button
       check.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent aE) {

               for (int i = 0; i < 10; i++)
                   arr[i] = (int) (Math.random() * 50 + 1);
               append(arr);
           }

           private void append(int[] arr) {
               String s = "";
               for (int i = 0; i < arr.length; i++) {
                   s = s + arr[i] + ",";
               }
               s.substring(0, s.length() - 1);
               label2.setText(s);

           }

           public void sort(int arr[]) {
               int N = arr.length;
               int i, j, pos, temp;
               for (i = 0; i < N - 1; i++) {
                   pos = i;
                   for (j = i + 1; j < N; j++) {
                       // finding the min position
                       if (arr[j] < arr[pos]) {
                           pos = j;
                       }
                   }
                   // swapping
                   temp = arr[i];
                   arr[i] = arr[pos];
                   arr[pos] = temp;
               }
           }

       });

       panel.add(label2);
       panel.add(check);
       panel.add(sort);
       frame.add(panel);

       frame.setSize(700, 800);
       frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
   }
}

//TestSortNumbersGUI.java
public class TestSortNumbersGUI {
   public static void main(String[] args) {
       new SortNumbersGUI();
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Design and implement an applicationSortNumbersthat has two buttons, and a label. Label the buttons Generate and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

  • Using C#: Create a Form that consists of 3 radio buttons, two textboxes, a label and...

    Using C#: Create a Form that consists of 3 radio buttons, two textboxes, a label and a button. The three radio buttons should represent the names of three fonts, e.g. “Arial”, “Calibri” and “Times New Roman”. In the one textbox, a user should type a message and in the other textbox, the user should type a size. When the user clicks the button, the label should be updated with the text the user typed in the one textbox, using the...

  • in JAVA program.Use top-down design to design and implement a program to ask the user to...

    in JAVA program.Use top-down design to design and implement a program to ask the user to enter a list of integers, and then display to the user the mean and median of this list. You should first prompt the user to specify the length of the list of integers. For this assignment, your code should create an array of size 10, and then allow the user to specify the number of integers in their list, up to a maximum of...

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • Java We did in lecture, and explain your answer briefly. Problem 4: Sorting practice 14 points;...

    Java We did in lecture, and explain your answer briefly. Problem 4: Sorting practice 14 points; 2 points for each part individual-only Important: When answering these questions, make sure to apply the versions of these algorithms that were discussed in lecture. The Java code for each algorithm can be found in our Sort class. Given the following array: {14, 7, 27, 13, 24, 20, 10, 33 1. If the array were sorted using selection sort, what would the array look...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

  • c++ Implement Radix Sort Most sorting algorithms, like bubble, insertion, selection and shell follow similar implementations....

    c++ Implement Radix Sort Most sorting algorithms, like bubble, insertion, selection and shell follow similar implementations. Radix sort is a unique sorting algorithm. In this assignment, implement the Radix Sort algorithm, as explained the text book in chapter 2. Use the Numbers.txt file in the DataFiles folder for the numbers to sort. Extra credit is available for processing alphabetic strings instead of just numbers. Specification: * Using your Doubly-Linked list to create an dynamic array of Doubly-Linked lists (like lab1)....

  • I want just the answer to the third task. to generate a report/graph. Thanks! 1 Problem...

    I want just the answer to the third task. to generate a report/graph. Thanks! 1 Problem Description Instructions. You are provided the skeleton code named Sort.java. The source file is available on Canvas in a folder named HW1. Please modify the skeleton code to solve the following tasks. . Task 1 (80 pts). Implement the Insertion Sort algorithm as discussed in Lecture 1. (Hint: use the function checked sorted to check if your output is indeed sorted.) . Task 3...

  • 1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching...

    1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT