Question
I have completed Part 1 of this assignment in JAVA, but I need help with Part 2 please.... My code for part one is posted
I have completed Part 1 of this assignment in JAVA, but I need help with Part 2 please.... My code for part one is posted under the questions.
Part 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.
Part 2. I need to 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.
package lab3.problem1;
import java.util.Scanner;
public class Lab3Problem1 {
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 or 0 to quit: "); while(size<10){
String no = obj.nextLine(); if(no.equals("0")){ 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)); }
System.out.println("Enter the element"); int el = obj.nextInt(); int start =0; int end = size; int mid = (start+end)/2;
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started