I need help with this code. It involves database objects with searching and sort
ID: 3795688 • Letter: I
Question
I need help with this code. It involves database objects with searching and sorting. Make two source code files using Java (.java files). One for the main class and one for the database class. The requirements for the assignment are as follows:
°Create a main class and a database class
°Collect 5 random whole numbers from the user in the main class and store in an array. (not in ascending or descending order)
°Create an instance of the database object in your main class
°Store the numbers array in a field of the database object from the main class (using a public method from the database class)
°From the main class, call a method from the data object that returns the array sorted in ascending order and display the sorted list in the main class. The database object should sort the numbers using a bubble sort. Repeat for descending order.
°From the main class, call different methods from the database object that each return the min, average, and max respectively. Display them to the user.
°From the main class, call a method from the database object that searches for a value in the array using a binary search and returns the index of that value
Explanation / Answer
Main Class
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Collecting 5 random numbers
Scanner input = new Scanner(System.in);
System.out.println("Enter 5 random whole numbers : ");
ArrayList<Integer> values = new ArrayList<Integer>();
for(int i=0;i<5;i++){
values.add(input.nextInt());
}
// Creating instance of Database
Database database = new Database();
// Storing array to Database object
database.setValues(values);
// Calling function to sort in ascending order
int[] cruncArr = database.BubbleSortAsceMethod();
System.out.println("Sorted array in ascending order is : "+Arrays.toString(cruncArr));
// Calling function to sort in descending order
int[] cruncDesc = database.BubbleSortDescMethod();
System.out.println("Sorted array in descending order is : "+Arrays.toString(cruncDesc));
// getting min, max and average
int min = database.minValue();
int max = database.maxValue();
double average = database.calculateAverage();
System.out.println("Minimum Value : "+min);
System.out.println("Maximum Value : "+max);
System.out.println("Average Value : "+average);
// binary search
input = new Scanner(System.in);
System.out.println("Input value to be searched in an array : ");
int index = database.binarySearch(input.nextInt());
System.out.println("Index of searched value is : "+index);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
Database Class
import java.util.ArrayList;
import java.util.Collections;
public class Database {
private ArrayList<Integer> values;
public Database(){
values = new ArrayList<Integer>();
}
private int[] convertArrayListToIntArr(){
int[] arr = new int[values.size()];
for(int i = 0; i < values.size(); i++) {
arr[i] = values.get(i);
}
return arr;
}
// Bubble Sort Algorithm in Ascending Order
public int[] BubbleSortAsceMethod() {
int[] cruncArr = convertArrayListToIntArr();
int temp;
for (int i = 0; i < cruncArr.length - 1; i++) {
for (int j = 1; j < cruncArr.length - i; j++) {
if (cruncArr[j - 1] > cruncArr[j]) {
temp = cruncArr[j - 1];
cruncArr[j - 1] = cruncArr[j];
cruncArr[j] = temp;
}
}
}
return cruncArr;
}
// Bubble Sort Algorithm in Descending Order
public int[] BubbleSortDescMethod() {
int[] cruncArr = convertArrayListToIntArr();
int temp;
for (int i = 0; i < cruncArr.length - 1; i++) {
for (int j = 1; j < cruncArr.length - i; j++) {
if (cruncArr[j - 1] < cruncArr[j]) {
temp = cruncArr[j - 1];
cruncArr[j - 1] = cruncArr[j];
cruncArr[j] = temp;
}
}
}
return cruncArr;
}
// minimum value
public int minValue(){
int min = values.get(0);
for(int i = 0; i < values.size(); i++) {
int number = values.get(i);
if(number < min) min = number;
}
return min;
}
// maximum value
public int maxValue(){
int max = values.get(0);
for(int i = 0; i < values.size(); i++) {
int number = values.get(i);
if(number > max) max = number;
}
return max;
}
public double calculateAverage() {
Integer average = 0;
if(!values.isEmpty()) {
for (Integer mark : values) {
average += mark;
}
return average.doubleValue() / values.size();
}
return average;
}
public int binarySearch(int value){
return Collections.binarySearch(values,value);
}
public ArrayList<Integer> getValues() {
return values;
}
public void setValues(ArrayList<Integer> values) {
this.values = values;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------