import java.util.Random; /** * The purpose of this lab is to practice arrays, loops, and if statements. * I will also use a random number generator package. * * @author John Sloan * @version 02-26-08 */ public class ArrayPractice { // private int [] A is reserve a spot right here for an array of integers. // now I must fill that spot with something and that is what the new // operateor does. // private rand...resrvng a spot to place some value. In the constructor I will // assign the value private int [] A; private Random rand; /** * Constructor for objects of class ArrayPractice */ public ArrayPractice(int size) { // go get a place in memory to store an int array of size "size" A = new int [size]; // ()- contructor in the random class takes no parameters. A new // instance of Random too rand. rand = new Random(); } /* * This Method provides the largest random value integer throught the parameter * too the array. */ public void fillArray (int value) { for(int i = 0; i < A.length; i++) { A [i] = rand.nextInt(value); } } /* * Prints the values of array to the terminal window. */ public void printArray () { System.out.println("The array is "); for(int i=0; i < A.length; i++) { System.out.println(A[i]); } System.out.println(); } /* * Returns the size of the array obtained by the length of the field. */ public int size() { return A.length; } /* * Prints array in reverse order. */ public void printReverseArray() { System.out.println("The reverse values are "); for(int i = A.length - 1; i > 0; i--) { System.out.println(A[i]); } System.out.println(); } /* * Prints array in even order. */ public void printArrayEven() { System.out.println("The even values are "); for(int i=0; i < A.length; i++) { if(A[i] % 2 == 0) { System.out.println(A[i]); } } System.out.println(); } /* * Prints array in odd order. */ public void printArrayOdd() { System.out.println("The odd values are "); for(int i=0; i < A.length; i++) { if(A[i] % 2 == 1) { System.out.println(A[i]); } } System.out.println(); } /* * Prints values greater than the parameter. */ public void printGreaterThan(int val) { System.out.println("The values greater than are "); for(int i=0; i < A.length; i++) if(A[i] > val) { System.out.println(A[i]); } System.out.println(); } /* * Prints the sum to the terminal window. */ public void sumArray() { int sumA= 0; for(int i=0; i < A.length; i++) { sumA+=A[i]; } System.out.println("The sum is: " + sumA); System.out.println(); } /* * Finds the minium and prints to the terminal window. */ public void findMinimum() { int min= A[0]; for(int i= 1; i < A.length; i++) { if(min > A[i]) { min= A[i]; } } System.out.println("The min is: "+ min); System.out.println(); } /* * Finds the maximum and prints to the terminal window. */ public void findMaximum() { int max= A[0]; for(int i= 1; i < A.length; i++) { if(max < A[i]) { max= A[i]; } } System.out.println("The max is: " + max); System.out.println(); } /* * Finds the Average and prints to the terminal window. */ public void findAverage() { int sumA= 0; for(int i=0; i < A.length; i++) { sumA+=A[i]; } System.out.println("The average is " + sumA / size()); System.out.println(); } /* * Finds the location of B and prints to the terminal window. * Do a while loop and do a bolean true false statement. */ public void findLocation(int val2) { int foundLocation= 0; //setting found to false last(WORKS FOR ME) boolean found = false; int i= 0; while(i < A.length && !found) { if(val2 == A[i]) { foundLocation = i;//Store this index untill further use. found = true; } i++; } if(found) { System.out.println("The " + val2 + " was found at " + foundLocation); } else { System.out.println("The value was not found"); } System.out.println(); } /* * This method adds the parameter to each loop. */ public void arrayAdd(int addVal) { int i = 0; while(i < A.length) { A[i]+= addVal; i++; } } /* * this method multiplies the array position by the parameter. */ public void arrayMultiply(int multVal) { int i = 0; while(i < A.length) { A[i]=A[i] * multVal; i++; } } /** * This method sorts the array in ascending order: smallest value to * largest value. It uses the bubble sort algorithm. */ public void bubbleSort( ) { int temp; //used for the swap routine boolean sorted = false; //the exit test int i = 0; //boundary of the sorted area of the array while( !sorted ) { sorted = true; //get ready to exit i++; //expand the sorted section for ( int j = 0; j < A.length - 1; j++) { if( A[ j ] > A[ j + 1 ] ) { //array is not sorted yet, so temp = A[ j ]; //do a swap A[ j ] = A[ j + 1 ]; A[ j + 1 ] = temp; sorted = false; //sorry, the array was not sorted. } } } } }