Question
XXXXXXX Programming Activity 8-2 Guidance XXXXXXXXXXX ============================================================ This assignment gives you practice with searching and sorting a single dimensional array of int values. The name
XXXXXXX Programming Activity 8-2 Guidance XXXXXXXXXXX ============================================================ This assignment gives you practice with searching and sorting a single dimensional array of int values. The name of the array you will be using is arr. Here is the framework statement that defines arr: private static int [] arr; Here is the framework statement that creates arr: arr = new int[SIZE]; Note that arr is an array of int items. Part 1 ------ The framework comments say that the array you are to search has random values. You CANNOT assume that this array is in sorted order. You must look at each value in the array and compare it to the value of the input parameter named "key". If you find it, immediately return the array index where you found it. Since you will return from inside the loop if you get a hit, the code following the loop is only executed if you did not find it. At this point, your code should simply return -1. Part 2 ------ This week's outline contains the following bubble sort pseudocode: for (i = 0; i array[j + 1]) // Two consecutive elements are in the wrong order { swap them } } } To swap 2 items, you must use of a temporary variable. Assign the first item to the temporary variable. Assign the second item to the first item. Finally, assign the temporary variable to the second item. Here is a swapping example from this week's outline: Example (swapping elements 7 and 8: int temp = array[7]; array[7] = array[8]; array[8] = temp;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// The name of the MAIN Class --> ArrayPractice2.java // Anderson, Franceschi
import java.awt.*; import javax.swing.*; import javax.swing.JOptionPane; import java.awt.event.*; import java.util.Random;
public class ArrayPractice2 extends JFrame { private final int SIZE = 15;
private static ArrayPractice2 app; private static int [] arr; private static int current1 = -1; private static int current2 = -1;
private int key; private BarChart bc;
private JButton sequentialSearch; private JButton bubbleSort; private ButtonHandler bh;
private Image offscreen;
public ArrayPractice2() { super("Choose your activity"); Container c = getContentPane(); c.setLayout(new FlowLayout());
sequentialSearch = new JButton("Sequential Search"); c.add(sequentialSearch); bubbleSort = new JButton("Bubble Sort"); c.add(bubbleSort);
bh = new ButtonHandler(); sequentialSearch.addActionListener(bh); bubbleSort.addActionListener(bh);
arr = new int[SIZE];
// fill with random numbers between 20 and 160 Random rand = new Random(); for (int i = 0; i
bc = new BarChart(arr);
setSize(400,275); setVisible(true); offscreen = this.createImage(getSize().width, getSize().height); }
// 1 ***** student writes this method /** Searches for key in integer array named arr // arr is an instance variable of the class and has been // instantiated and filled with random values. // @param key value to search for // @return if key is found, the index of the first element // in array whose value is key; if key is not found, // the method returns -1 */ public int sequentialSearch(int key) { // Note: To animate the algorithm, put this method call as the // first statement in your for loop // animate(i, 0); // where i is the index of the current array element // Part 1 student code starts here:
return 0; // Replace this line with your return statement
// Part 1 student code ends here. } // end of sequentialSearch
// 2. ***** student writes this method /** Sorts arr in ascending order using the bubble sort algorithm */ public void bubbleSort() { // Note: To animate the algorithm, put this method call as the // last statement in your innermost for loop // animate(i, j); // where i is the value of the outer loop counter // and j is the value of the inner loop counter, // or the index of the current array element // Part 2 student code starts here:
// Part 2 student code ends here. } // end of bubbleSort
public void startActivity(int act) { bc.setActivity(act); boolean goodInput = false; switch(act) { case 0: goodInput = false; while (!goodInput || key 160) { try { String answer = JOptionPane.showInputDialog(null, "Enter an integer value between 20 and 160 to search sequentially"); if (answer != null) { key = Integer.parseInt(answer); goodInput = true; } else { goodInput = false; break; } } catch(Exception e) {} } if (goodInput) { int index = sequentialSearch(key); if (index != -1) JOptionPane.showMessageDialog(null, "You found " + key + " at index " + index); else JOptionPane.showMessageDialog(null, "You did not find " + key); } break;
case 1: bubbleSort(); JOptionPane.showMessageDialog(null, "The sorting of the array is done"); break; } enableButtons(); }
private void animate(int index1, int index2) { try { current1 = index1; current2 = index2; bc.setArray(arr);
Graphics g = offscreen.getGraphics(); paint(g); g = this.getGraphics(); g.drawImage(offscreen, 0, 0, this);
Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("IE Exception " + e.getMessage()); System.out.println(e.toString()); } }
public void disableButtons() { bubbleSort.setEnabled (false); sequentialSearch.setEnabled(false); }
public void enableButtons() { bubbleSort.setEnabled (true); sequentialSearch.setEnabled(true); }
public static int getCurrent1() { return current1; }
public static int getCurrent2() { return current2; }
public static int [] getArray() { return arr; }
public void paint(Graphics g) { super.paint(g); bc.draw(g); bc.updateBarChart(key, current1, g); }
public static void main(String [] args) { app = new ArrayPractice2(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
private class ButtonHandler implements ActionListener { private boolean on = true; public void actionPerformed(ActionEvent e) { PrintArrayT t = new PrintArrayT(app); if (e.getSource() == sequentialSearch) { disableButtons(); sequentialSearch.requestFocus(); bc.setActivity(0); t.start(); } if (e.getSource() == bubbleSort) { disableButtons(); bubbleSort.requestFocus(); bc.setActivity(1); t.start(); } } }
private class PrintArrayT extends Thread { int [] arr; ArrayPractice2 s1; public PrintArrayT(ArrayPractice2 s) { arr = ArrayPractice2.arr; s1 = s; } public void run() { startActivity(bc.getActivity()); } }
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
/*Second Class App name --> Draws a Bar Chart * */ import java.awt.Graphics; import javax.swing.JFrame; import java.awt.Color;
public class BarChart { public static int XMAX = 400; public static int YMAX = 250;
private int [] data; private int barSize; private int xStart; private int activity = 0;
public BarChart( ) {}
public BarChart( int [] dArray ) { data = new int [dArray.length]; for ( int i = 0; i
public void setArray( int [] dArray ) { for ( int i = 0; i
public void setActivity( int a ) { activity = a; }
public int getActivity( ) { return activity; }
public void updateBarChart( int key, int index, Graphics g ) { switch( activity ) { case 0: // sequential search draw( g ); drawSequentialSearch( key, g ); break; case 1: // bubble sort draw( g ); drawBubbleSort( g ); break; } }
public void draw( Graphics g ) { xStart = 10; g.setColor( Color.BLUE ); for ( int i = 0; i
public void drawBubbleSort ( Graphics g ) { xStart = 10; int a = ArrayPractice2.getCurrent1( ); int b = ArrayPractice2.getCurrent2( ); if ( a != -1 && b != -1 ) { g.setColor( Color.RED ); xStart = 10 + b * barSize; g.drawRect( xStart, YMAX - 25 - data[b], barSize-5, data[b] ); g.drawString( String.valueOf( data[b] ), xStart, YMAX - 5 ); xStart = 10 + ( b + 1 ) * barSize; g.drawRect( xStart, YMAX - 25 - data[b + 1], barSize-5, data[b + 1] ); g.drawString( String.valueOf( data[b + 1] ), xStart, YMAX - 5 ); } }
public void drawSequentialSearch( int key, Graphics g ) { int a = ArrayPractice2.getCurrent1( ); if ( a != -1 ) { g.setColor( Color.RED ); xStart = 10 + a * barSize; g.drawRect( xStart, YMAX - 25 - key, barSize-5, key ); g.drawString( String.valueOf( data[a] ), xStart, YMAX - 5 ); } } } -------------------------------------------------------------------------------------------------------------------------------------
This is what your OUTPUT SHOULD LOOK LIKE:
Example Programming Activity 8
Choose your activity window:
Sequential Search input:
Sequential Search results:
Bubble Sort processing:
Bubble Sort results:
Example Programming Activity 8-2 Output Choose your activity window: Choose your activity Sequential Search Bubble Sort 110 11 1 63 138 159 10O 102 154 B 147 87 33 i5 7 108Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started