You should already have a class called Numbers from the first exercise. Add the following methods to
Question:
You should already have a class called Numbers from the first exercise. Add the following methods to it.
An instance method called randomize() that creates and assigns random number in the range 0 to 100 to the elements of the encapsulated array (hint, look up the java.util.Random class).
Create methods called bubbleSort(), selectionSort(), and insertionSort(). They should perform the appropriate kind of sort on the data in your array.
In your NumbersDriver class, do the following:
Create a Numbers object with 10 elements.
Call the randomize method.
Display the contents of the array.
Call the bubbleSort method.
Display the contents of the array.
Call the randomize method.
Display the contents of the array.
Call the selectionSort method.
Display the contents of the array.
Call the randomize method.
Display the contents of the array.
Call the insertionSort method.
Display the contents of the array.
Clearly explain in your output what you are doing.
Class Numbers:
class Numbers{
private final int arr[];
public Numbers(int size){
arr = new int[size];
}
public void display(){
for(int i=0; i
System.out.print(arr[i]+" ");
}
}
public void setValue(int index, int value){
if(index>=arr.length)
throw new IndexOutOfBoundsException("index must be between 0 and "+arr.length);
if(value<0)
arr[index] = 0;
else if(value>100)
arr[index] = 100;
else
arr[index] = value;
}
public int getValue(int index){
if(index>=arr.length)
throw new IndexOutOfBoundsException("index must be between 0 and "+arr.length);
return arr[index];
}
public int getLength(){
return arr.length;
}
}
CLASS NumbersDriver:
public class NumbersDriver {
public static void main(String[] args) {
Numbers nums = new Numbers(10);
System.out.println("The numbers in array is: ");
nums.display();
for(int i=0; i<10; i++){
nums.setValue(i, i*10);
}
int len = nums.getLength();
System.out.println(" First value: "+nums.getValue(0));
System.out.println("Last value: "+nums.getValue(len-1));
System.out.println("Length of array: "+len);
System.out.println("The numbers in array is: ");
nums.display();
System.out.println();
}
}
Analyzing Data And Making Decisions Statistics For Business Microsoft Excel 2010 Updated
ISBN: 9780132924962
2nd Edition
Authors: Judith Skuce