Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am unable to post the separate question as the nature of questions. I am also the sort1.java file code below and after the code

I am unable to post the separate question as the nature of questions. I am also the sort1.java file code below and after the code finishes, the questions will start pls help me sort this questions out. there are five task to perform. pls help,,,

------------------sort1.java--------------------------------------

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

public class Sort1 extends JFrame implements ActionListener{

private TextField[] items = new TextField[6]; private JButton btnSort, btnClear, btnReset; private TextField tmp; private Label status;

private int pauseInterval = 100; // ms public static void main(String[] args) { new Sort1().setVisible(true); } public Sort1() { init(); }

public void init() { setTitle("Sorting Algorithms"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(440, 200); JPanel jp = new JPanel(new BorderLayout()); jp.setPreferredSize(new Dimension(440, 200)); jp.setBackground(Color.white); JPanel itemPanel = new JPanel(); itemPanel.setBackground(Color.white); for (int i = 0; i < items.length; i++) { items[i] = new TextField(3); items[i].setPreferredSize(new Dimension(30,40)); itemPanel.add(items[i]); } initItems(); itemPanel.add(new Label("Temp:")); tmp = new TextField(4); tmp.setPreferredSize(new Dimension(30,40)); tmp.setEditable(false); itemPanel.add(tmp); itemPanel.add(new Label("")) .setPreferredSize(new Dimension(380, 65)); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(Color.white); btnSort = new JButton("Sort"); btnReset = new JButton("Reset"); btnClear = new JButton("Clear"); btnSort.addActionListener(this); btnClear.addActionListener(this); btnReset.addActionListener(this); buttonPanel.add(btnSort); buttonPanel.add(btnClear); buttonPanel.add(btnReset); status = new Label("Wating ... "); status.setPreferredSize(new Dimension(380, 40)); JPanel statusPanel = new JPanel(); statusPanel.setBackground(Color.white); statusPanel.add(status, BorderLayout.SOUTH); jp.add(itemPanel, BorderLayout.NORTH); jp.add(buttonPanel, BorderLayout.CENTER); jp.add(statusPanel, BorderLayout.SOUTH); getContentPane().add(jp); }

private void initItems() { for (int i = 0; i < items.length; i++) { items[i].setText(( String.valueOf((int)(Math.random()*100)))); } }

private void pause(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { showStatus(e.toString()); } }

private void assign(TextField to, TextField from) { Color tobg = to.getBackground(); to.setBackground(Color.green); pause(pauseInterval); to.setText(from.getText()); pause(pauseInterval); to.setBackground(tobg); }

private void swapItems(TextField t1, TextField t2) { assign(tmp,t1); assign(t1,t2); assign(t2,tmp); } private boolean greaterThan(TextField t1, TextField t2) { boolean greater; Color t1bg = t1.getBackground(); Color t2bg = t2.getBackground(); t1.setBackground(Color.cyan); t2.setBackground(Color.cyan); pause(pauseInterval); greater = Integer.parseInt(t1.getText()) > Integer.parseInt(t2.getText()); pause(pauseInterval); t1.setBackground(t1bg); t2.setBackground(t2bg); return greater; }

private void bubbleSort() { showStatus("Sorting ..."); boolean swap = true; while (swap) { swap=false; for (int i = 0; i < items.length-1; i++) { if (greaterThan(items[i],items[i+1])) { swapItems(items[i],items[i+1]); swap=true; } } //for } // while showStatus("Sort complete"); } // bubbleSort

public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); switch (command) { case "Clear": for (int i = 0; i < items.length; i++) { items[i].setText(""); } break; case "Sort": bubbleSort(); break; case "Reset": initItems(); break; default: showStatus("Unrecognised button: " + e.toString()); } }

private void showStatus(String s) { status.setText(s); } }

---------------------------------------------------------------------------------------------------------------

Getting Started

Start jGrasp and load Sort1.java. Compile the program and then run it by selecting run application from the run menu. Once the application window has appeared and the application started, click on the Sort button. The application will display each step in sorting 6 numbers. Each time a value is copied from one place to another it will appear with a green background momentarily, the destination will then be displayed with a blue background. The values to be sorted can be generated automatically by clicking on the Reset button, cleared by clicking the Clear button or entered manually.

Task 1 Modify the method bubbleSort so that the values are sorted into descending order (largest to smallest), rather than ascending order. Note that the numbers to be sorted are stored as strings so they must be converted to integers before they can be compared (the code already does this). Also, the values must be swapped using the method assign (it not only assigns the values but performs other functions). Hint: only one character in the method needs to be modified to complete the task!

Task 2 1. Make sure you have saved the file you were working on for Task 1. 2. Change all occurrences of Sort1 to Sort2 There are three places where you have to do this 3. Save the file as Sort2.java 4. Modify the method bubbleSort so that it calculates and outputs, in the applications status label which appears at the bottom of the applications window, the number of swaps performed during sorting. To count the number of swaps you will need to define an integer variable and initialise it to 0. It should be increased by one each time a swap is performed. The output should be generated by modifying the following line which appears at the end of the method bubbleSort: showStatus("Sort complete"); Compile and run the application. After clicking the Sort button and once sorting is complete a line of the following form should appear at the bottom of the applications window: Sorting complete, number of swaps = 7 To enter your own list to sort, click Clear and then type integers into each empty box. The following list should cause the above status line output to be produced: 3 4 9 17 1 2

Task 3 This task involves recognising the methods and objects which are being used to achieve certain program behaviours. In particular, it is not an exercise in understanding how applications are constructed this is not necessary to complete the task. 1. Make sure you have saved the file you were working on for Task 2. 2. Change all occurrences of Sort2 to Sort3 3. Save the file as Sort3.java 4. The version of bubble sort implemented in the application traverses the entire list during each step of the sort. This is not necessary since after the first traversal the smallest item will be at the end of the list and after the second traversal, the second smallest item will be in its correct position (2nd last) and so on. Modify the method bubbleSort so that it does not perform unnecessary comparisons (the for loop should terminate earlier on each successive step).

Task 4 This task involves recognising the methods and objects which are being used to achieve certain program behaviours. 1. Make sure you have saved the file you were working on for Task 3. 2. Change all occurrences of Sort3 to Sort4 3. Save the file as Sort4.java 4. Modify the application so that clicking the Reset button, after sorting is complete, will restore the values in the list to their original values, rather than generating a new set of randomly chosen vales. The original values should be stored in an array of Strings (you will need to declare this). A value in a list can be retrieved with the expression: items[i].getText(); and restored with: items[i].setText(...); where . . . is the value to be restored (a string).

Task 5 This task involves recognising the methods and objects which are being used to achieve certain program behaviours. 1. Make sure you have saved the file you were working on for Task 4. 2. Change all occurrences of Sort4 to Sort5 3. Save the file as Sort5.java 4. Modify the application so that it performs a selection sort or bubble sort. The Clear buttons label should be changed to Toggle Sort and the action performed when it is clicked should be changed to toggle the type of sort performed. Note: The status label should indicate the number of swaps for either sort. The Sort buttons label should change to indicate the current type of sort (Selection or Bubble). A buttons label can be changed with its setLabel method. e.g. mybutton.setLabel("Wombat");

These are the five tasks pls help!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Window Functions For Data Analysis And Beyond

Authors: Itzik Ben Gan

2nd Edition

0135861446, 978-0135861448

More Books

Students also viewed these Databases questions