Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add the following methods to the code below An instance method called randomize(

ID: 3582595 • Letter: A

Question

Add the following methods to the code below

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 selctionSort 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.

NumbersDriver.java


public class NumbersDriver {

  
   public static void main(String[] args) {
      
       Numbers obj = new Numbers (10);
       obj.display();
       for(int i=0; i<10; i++){
           obj.setValue(i, i);
       }
       System.out.println("First value: "+obj.getValue(0));
       System.out.println("Last value: "+obj.getValue(9));
       System.out.println("Length is "+obj.getLength());
       obj.display();
   }

}

Numbers.java

public class Numbers {
   private int array[];
   public Numbers(int length){
       array = new int[length];
   }
   public void display(){
       for(int i=0; i<array.length; i++){
           System.out.println(array[i]);
       }
   }
   public void setValue(int index, int value){
       try{
       if(value < 0 ){
           value = 0;
       }
       else if(value > 100){
           value = 100;
       }
       array[index] =value;
       }
       catch(ArrayIndexOutOfBoundsException e){
           System.out.println("Invalid index position.");
       }
   }
   public int getValue(int index){
       try{
           return array[index];
       }
       catch(ArrayIndexOutOfBoundsException e){
           System.out.println("Invalid index position.");
           return -1;
       }
      
   }
   public int getLength(){
       return array.length;
   }
}

Explanation / Answer

import java.util.*;
public class Numbersdriver {
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Random r=new Random();
       Scanner sc=new Scanner(System.in);
       int ch;
       Numbers obj = new Numbers (10);
   obj.display();
   int n=100-1+1;
   for(int i=0; i<10; i++){
       int g= r.nextInt()%n;
   obj.setValue(i, g);
   }
   System.out.println("First value: "+obj.getValue(0));
   System.out.println("Last value: "+obj.getValue(9));
   System.out.println("Length is "+obj.getLength());
   int array[]= obj.display();
   while(true){
       System.out.println("enter choice:");
       System.out.println("1-bubble sort:");
       System.out.println("2-selection sort:");
       System.out.println("3-insertion sort:");
       System.out.println("4-exit :");
       ch=sc.nextInt();
       switch(ch){
       case 1:int arr1[]=obj.bublesort(array);
       for(int i=0;i<arr1.length;i++)
           System.out.print(arr1[i]+",");
       break;
       case 2:int arr2[]=obj.selectiosort(array);
   for(int i=0;i<arr2.length;i++)
       System.out.print(arr2[i]+",");
   break;
       case 3:int arr3[]=obj.insertionsort(array);
   for(int i=0;i<arr3.length;i++)
       System.out.print(arr3[i]+",");
   break;
       case 4:System.exit(0);
       }
   }
   }
   }

class Numbers {
   private int array[];
   public Numbers(int length){
   array = new int[length];
   }
   public int[] display(){
   for(int i=0; i<array.length; i++){
   System.out.println(array[i]);
   }
   return array;
   }
   public void setValue(int index, int value){
   try{
   if(value < 0 ){
   value = 0;
   }
   else if(value > 100){
   value = 100;
   }
   array[index] =value;
   }
   catch(ArrayIndexOutOfBoundsException e){
   System.out.println("Invalid index position.");
   }
   }
   public int getValue(int index){
   try{
   return array[index];
   }
   catch(ArrayIndexOutOfBoundsException e){
   System.out.println("Invalid index position.");
   return -1;
   }
  
   }
   public int getLength(){
   return array.length;
   }
   public int[] bublesort(int[] array){
       int temp=0;
       for(int i=0;i<array.length;i++){
           for(int j=i+1;j<array.length;j++){
               if(array[i]>array[j]){
                   temp=array[j];
                   array[j]=array[i];
                   array[i]=temp;
               }
           }
                 
       }
       return array;
   }
   public int[] selectiosort(int[] array){
       for (int i = 0; i < array.length - 1; i++)
       {
       int ind = i;
       for (int j = i + 1; j < array.length; j++){
       if (array[j] < array[ind]){
       ind = j;
       }
       }
       int small = array[ind];   
       array[ind] = array[i];
       array[i] = small;
       }   
       return array;
   }
   public int[] insertionsort(int[] array){
       int lim = array.length;
   for (int j = 1; j < lim; j++) {
   int k = array[j];
   int i = j-1;
   while ( (i > -1) && ( array [i] > k ) ) {
   array [i+1] = array [i];
   i--;
   }
   array[i+1] = k;
   }
       return array;
   }
   }

output:

0
0
0
0
0
0
0
0
0
0
First value: 0
Last value: 0
Length is 10
0
0
75
20
0
73
11
0
84
0
enter choice:
1-bubble sort:
2-selection sort:
3-insertion sort:
4-exit :
1
0,0,0,0,0,11,20,73,75,84,enter choice:
1-bubble sort:
2-selection sort:
3-insertion sort:
4-exit :
2
0,0,0,0,0,11,20,73,75,84,enter choice:
1-bubble sort:
2-selection sort:
3-insertion sort:
4-exit :
3
0,0,0,0,0,11,20,73,75,84,enter choice:
1-bubble sort:
2-selection sort:
3-insertion sort:
4-exit :
4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote