Question: Please write in Java with comments! Need help with part 2. I already completed part 1 and will include the code I have so far.

Please write in Java with comments!

Need help with part 2. I already completed part 1 and will include the code I have so far.

1)

Problem 1: Ask for keyboard input of a decimal number. Use a loop to keep asking for input of up to ten numbers. Exit the loop if ten numbers are entered or if a sentinel value for QUIT is entered. Store the numbers in an array. Sort the array according to ascending number. Get the average for all the numbers in the array. Print out the average. Calculate the distance from the average for each number in the array and print out the difference. Use a binary search method to search for a number in the array and display to the console.

2)

For the array in problem 1, create an ArrayMethods class where you will add static methods to manipulate your array. Add a method called getMin() that returns the lowest value in the array or -1 if the array is empty, and another method called removeMin() so that the item with the lowest value is not only returned by the method, but also removed from the array. Add some code in main() to test these two methods. You can assume that all the values entered are positive numbers.

Problem 1 code I have so far:

import java.util.Scanner;

class Main{

public static void main(String[] args) {

Scanner obj = new Scanner(System.in);

int arr[] = new int[10];

int size = 0;

System.out.println("Enter numbers");

while(size<10){

// System.out.println("Enter a number");

String no = obj.nextLine();

if(no.equals("QUIT")){

break;

}

arr[size++] = Integer.parseInt(no);

}

for(int i=0;i

for(int j=i+1;j

if(arr[i]>arr[j]){

int t = arr[i];

arr[i] = arr[j];

arr[j] = t;

}

}

}

System.out.println("After sorting");

for(int i=0;i

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

}

System.out.println();

double sum = 0;

for(int i=0;i

sum += arr[i];

}

double avg = sum/size;

System.out.println("Average is "+avg);

System.out.println("Difference to average is : ");

for(int i=0;i

System.out.println(arr[i]+" "+Math.abs(arr[i]-avg));

}

//Perform binary search

System.out.println("Enter the element");

int el = obj.nextInt();

int start =0;

int end = size;

int mid = (start+end)/2;

//Dp until start

while(start<=end){

if(arr[mid]==el){

System.out.println("Found");

break;

}

if(arr[mid]>el){

end = mid-1;

}

else{

start = mid+1;

}

mid = (start+end)/2;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!