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

JAVA PROGRAMMING. Storage class with numbers that can be sorted and searched wit

ID: 3745586 • Letter: J

Question

JAVA PROGRAMMING.

Storage class with numbers that can be sorted and searched with your program.

End User will input five random numbers in a Main class to be stored in a array.

Place a Storage object inside Main to store the numbers array in a field of the database using its constructor.

Data Class configs:

Within the Storage class, use a quick sort method that returns the data in ascending and descending. Call the method from the main and print the result.

Within the Storage class, use methods that will return min, max, and average. Call them from the main class and print.

Lastly in the Storage class, use a method that will search for a value via binary search and returns the index of the value. Call the method from the main class and print.

Utility Class:

Use Static Quick Sort method that returns a sorted array. Method will be called from the main class. Print results in ascending and descending order. The print statements should indicate the sorting algorithm that was used.

Explanation / Answer

Storage.java

/**

* Stores numbers that can be sorted and searched . *

* @author

*/

public class Storage {

static int [] array;

static int[] sortedArrayAsc=null;

static int[] sortedArrayDesc;

/**

* Constructor to store the array of numbers

* @param arr integer array

*/

Storage(int[] arr) {

array=arr;

}

/**

* Prints the main array

*/

void print() {

for(int i=0;i<array.length;i++)

System.out.print(array[i]+" ");

System.out.println(" ");

}

/**

* Find the min value in main array

* @return min value

*/

int min() {

int min=array[0];

for(int i=1;i<array.length;i++)

if(min>array[i])

min=array[i];

return min;

}

/**

* Find the max value in main array

* @return max value

*/

int max() {

int max=array[0];

for(int i=1;i<array.length;i++)

if(max<array[i])

max=array[i];

return max;

}

/**

* Find the average value in main array

* @return avg value

*/

float average() {

int sum=0;

for(int i=0;i<array.length;i++)

sum+=array[i];

float average=sum/array.length;

return average;

}

/**

* Does a binary search and returns the location of the item.

* Binary search done on sorted array

* @param key item to be searched

* @return location of item

*/

int search(int key) {

if(sortedArrayAsc==null)//binary search can be done on sorted array only

sortedArrayAsc=sort(1);

//start and last index

int start = 0, last = sortedArrayAsc.length - 1;

while (start <= last)

{

int mid = start + (last-start)/2; //get the mid index

  

// Check if key is present at mid

if (sortedArrayAsc[mid] == key)

return mid;

  

// If key greater,go to right half

if (sortedArrayAsc[mid] < key)

start = mid + 1;

  

// If key is smaller, go to left half

else

last = mid - 1;

}

  

// Element not present

return -1;

  

}

/**

* Sort the array

* @param sortoption option in which array has to be sorted , 1=ascending ,2=descending

* @return sorted array

*/

public static int[] sort(int sortoption) {

if(sortoption==1)//ascending

{

sortedArrayAsc=new int[array.length];

for(int i=0;i<array.length;i++)//copies unsorted array

sortedArrayAsc[i]=array[i];

quickSort(sortedArrayAsc,0, (array.length-1));

return sortedArrayAsc;

}else//descending

{

sortedArrayDesc=new int[array.length];

for(int i=0;i<array.length;i++)

sortedArrayDesc[i]=array[i];

quickSort1(sortedArrayDesc,0, (array.length-1));

return sortedArrayDesc;

}

  

}

  

//recursevily called fora sorting ascending

private static void quickSort(int[] arr, int low, int high) {

if (arr == null || arr.length == 0)//array is of zero length or null

return;

if (low >= high)

return;

// middle value

int middle = low + (high - low) / 2;

int midval = arr[middle];

// make left < midvalue and right > midvalue

int i = low, j = high;

while (i <= j) {

while (arr[i] < midval) {

i++;

}

while (arr[j] > midval) {

j--;

}

if (i <= j) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

i++;

j--;

}

}

// recursively sort two sub parts

if (low < j)

quickSort(arr, low, j);

if (high > i)

quickSort(arr, i, high);

}

//recursevily called fora sorting descending

private static void quickSort1(int[] arr, int low, int high) {

if (arr == null || arr.length == 0)//array is of zero length or null

return;

if (low >= high)

return;

// middle value

int middle = low + (high - low) / 2;

int midval = arr[middle];

// make left < midvalue and right > midvalue

int i = low, j = high;

while (i <= j) {

while (arr[i] > midval) {

i++;

}

while (arr[j] < midval) {

j--;

}

if (i <= j) {

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

i++;

j--;

}

}

// recursively sort two sub parts

if (low < j)

quickSort1(arr, low, j);

if (high > i)

quickSort1(arr, i, high);

}

  

}

MainClass.java

import java.util.Scanner;

public class MainClass {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int[] array=new int[5];//create an array
int choice;
//get the numbers from end user
for(int i=0;i<5;i++)
{
System.out.print(" Enter Number"+i+":");
array[i]=in.nextInt();   
}
System.out.println(" ");
Storage storage=new Storage(array);//pass to the new storage instance created
while(true)
{
//print menu
System.out.println("1.Print");
System.out.println("2.Sort Ascending");
System.out.println("3.Sort Descending");
System.out.println("4.Min");
System.out.println("5.Max");
System.out.println("6.Average");
System.out.println("7.Search");
System.out.println("0.Exit");
System.out.print("Enter the operation to be performed:");
choice=in.nextInt();
switch(choice)
{
case 0:
System.exit(0);
break;
case 1:
storage.print();
break;
case 2:
//ascending order
print(Storage.sort(1),1);//method called directly using classname since it is a staic method
break;
case 3:
//descending order
print(Storage.sort(2),2);
break;
case 4:
System.out.println("Min Value:"+storage.min());
break;
case 5:
System.out.println("Max Value:"+storage.max());
break;
case 6:
System.out.println("Average:"+storage.average());
break;
case 7:
System.out.print("Enter the value to search:");
int key=in.nextInt();
int loc=storage.search(key);//returns the loc of searcheditem
if(loc>=0)//item present
System.out.println("Location:"+storage.search(key));
else//loc=-1
System.out.println(" Item not found.");
break;
default:
System.out.println("Enter correct option(0-7");
}
}
}
  
  
/**
* //print the sorted array
* @param arr array to be printed
* @param sortMethod method of sorting used
*/
static void print(int arr[],int sortMethod) {
if(sortMethod==1)
System.out.print("Ascending Order:");
else
System.out.print("Descending Order:");
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println(" ");
}
  
}
  
Sample Output

run:

Enter Number0:23

Enter Number1:465

Enter Number2:67

Enter Number3:23

Enter Number4:26


1.Print
2.Sort Ascending
3.Sort Descending
4.Min
5.Max
6.Average
7.Search
0.Exit
Enter the operation to be performed:1
23 465 67 23 26

1.Print
2.Sort Ascending
3.Sort Descending
4.Min
5.Max
6.Average
7.Search
0.Exit
Enter the operation to be performed:2
Ascending Order:23 23 26 67 465

1.Print
2.Sort Ascending
3.Sort Descending
4.Min
5.Max
6.Average
7.Search
0.Exit
Enter the operation to be performed:3
Descending Order:465 67 26 23 23

1.Print
2.Sort Ascending
3.Sort Descending
4.Min
5.Max
6.Average
7.Search
0.Exit
Enter the operation to be performed:4
Min Value:23
1.Print
2.Sort Ascending
3.Sort Descending
4.Min
5.Max
6.Average
7.Search
0.Exit
Enter the operation to be performed:5
Max Value:465
1.Print
2.Sort Ascending
3.Sort Descending
4.Min
5.Max
6.Average
7.Search
0.Exit
Enter the operation to be performed:6
Average:120.0
1.Print
2.Sort Ascending
3.Sort Descending
4.Min
5.Max
6.Average
7.Search
0.Exit
Enter the operation to be performed:7
Enter the value to search:23
Location:0
1.Print
2.Sort Ascending
3.Sort Descending
4.Min
5.Max
6.Average
7.Search
0.Exit
Enter the operation to be performed:0
BUILD SUCCESSFUL (total time: 37 seconds)