NEED ANSWER ASAP PLEASE Getting error when trying to run pro
NEED ANSWER ASAP PLEASE!!
Getting error when trying to run program because missing main and I cant get the format right to complete the program. Can anyone please write what I need to make this run ?
package calcavgdropsmallest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CalcAvgDropSmallest {
public static ArrayList<Double> input(){
Scanner in = new Scanner(System.in);
System.out.println(\"Enter numbers separated by spaces\");
String[] numbers = in.nextLine().split(\" \");
ArrayList<Double> arr = new ArrayList<Double>();
for(int i = 0; i < numbers.length; i++){
arr.add(Double.parseDouble(numbers[i]));
}
return arr;
}
public static int valueDropped(){
Scanner in = new Scanner(System.in);
System.out.print(\"Enter the numbers to drop: \");
int numbers = in.nextInt();
return numbers;
}
public static void print(ArrayList<Double> arr, int drop, double avg){
System.out.print(\"Given the numbers \");
for(int i = 0; i < arr.size(); i++){
System.out.printf(\"%.2f,\", arr.get(i));
if(i == arr.size() - 2) System.out.printf(\" and \");
}
System.out.print(\"average of all the numbers except the lowest \" + drop + \" numbers is \");
System.out.printf(\"%.2f\", avg);
}
Solution
package calcavgdropsmallest;
import java.util.ArrayList;
import java.util.Scanner;
public class CalcAvgDropSmallest {
public static ArrayList<Double> input(){
Scanner in = new Scanner(System.in);
System.out.println(\"Enter numbers separated by spaces\");
String[] numbers = in.nextLine().split(\" \");
ArrayList<Double> arr = new ArrayList<Double>();
for(int i = 0; i < numbers.length; i++){
arr.add(Double.parseDouble(numbers[i]));
}
return arr;
}
public static int valueDropped(){
Scanner in = new Scanner(System.in);
System.out.print(\"Enter the numbers to drop: \");
int numbers = in.nextInt();
return numbers;
}
public static void print(ArrayList<Double> arr, int drop, double avg){
System.out.print(\"Given the numbers \");
for(int i = 0; i < arr.size(); i++){
System.out.printf(\"%.2f,\", arr.get(i));
if(i == arr.size() - 2) System.out.printf(\" and \");
}
System.out.print(\"average of all the numbers except the lowest \" + drop + \" numbers is \");
System.out.printf(\"%.2f\", avg);
}
public static double calculateAverage(ArrayList<Double> arr,int drop) {
Double sum = 0.0;
int count=0;
if(!arr.isEmpty()) {
for (Double num : arr) {
if(num==drop){
count++;
}else{
sum += num;
}
}
return sum.doubleValue() / (arr.size()-count);
}
return sum;
}
public static void main(String args[]){
ArrayList<Double> arr=input();//calling the input method to get the input from the user
int drop=valueDropped();//calling the method to get the drop value from the user
double avg=calculateAverage(arr,drop); // calling the method for calculating the average
print(arr,drop,avg); // calling the method to print all the values
}
}
--------------------------output------------------------------------------
Enter numbers separated by spaces
2 3 4 5 6
Enter the numbers to drop: 2
Given the numbers 2.00,3.00,4.00,5.00, and 6.00,average of all the numbers except the lowest 2 numbers is 4.50

