/* File: ArrayDemo.java April 2002 Extend this code for Lab 6: 1. add statements to demoManipulations to thoroughly test each of your solutions. 2. modify randomArray so that it creates an array whose size is based on inputs (the version here is hard wired to produce an array whose size is between 10 and 15. 3. the example method find() solves one of the assignments in lab 6. Identify which one it is. Use it as a template for the other six methods you must write. */ import javax.swing.*; import psJava.KeyIn; public class ArrayDemo { // datafields int [] theArray; // methods public void demoManipulations() { // initialize the array theArray = randomArray(); displayRow(theArray); // Testing assignment ???: return a boolean based on whether a number is in the array int target = KeyIn.readInt("Which number should we look for?"); if ( find(theArray,target) ) JOptionPane.showMessageDialog(null, target + " is in the array"); else JOptionPane.showMessageDialog(null, target + " is not in the array"); target = KeyIn.readInt("Pick a number that is not in the array"); if ( find(theArray,target) ) JOptionPane.showMessageDialog(null, target + " is in the array"); else JOptionPane.showMessageDialog(null, target + " is not in the array"); } public int [] randomArray() { /* The call to Math.random produces a number between 10 and 15 random(), produces a real number between 0 1. We multiply it by five to produce a number between 0 and 5 and add 10 to produce a number between 10 and 15. */ int [] a; int size = (int)(Math.random()*5 + 10); JOptionPane.showMessageDialog(null, "The array has " + size + " elements in it."); a = new int[size]; for(int i = 0; i < size; i++) a[i] = (int)(Math.random()*100); return a; } // Returns true or false depending on whether element is in array a. public boolean find(int [] a, int element) { boolean found = false; int i= 0; while( !found && ( i < a.length) ) { found = (a[i] == element); i = i + 1; } return found; } public void displayRow(int[] ar) { String arrayInfo = ""; arrayInfo += "The values in the array are : \n"; for (int i=0; i