- #1
smilesofmiles
- 19
- 0
Hello mathhelpboards community! Please help! Thank you. :-)
I need my code to print out the user's original array that is to say the numbers in the way the user entered them. I tried making a copy of the original array and then did all of the arithmetic on it. I sorted the array copy in another method and used it to find the averages. I expected that this would allow me to keep the original numbers in tact. However, when I call the last method PrintResults to print the original array I get back the sorted array (the array copy). How do I fix this?
I need my code to print out the user's original array that is to say the numbers in the way the user entered them. I tried making a copy of the original array and then did all of the arithmetic on it. I sorted the array copy in another method and used it to find the averages. I expected that this would allow me to keep the original numbers in tact. However, when I call the last method PrintResults to print the original array I get back the sorted array (the array copy). How do I fix this?
Code:
package calcavgdropsmallest;import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
import java.util.List;
public class CalcAvgDropSmallest {
private static DecimalFormat df2 = new DecimalFormat(".00");
public static ArrayList<Double> getArrayNumbers(){
Scanner input = new Scanner(System.in);
System.out.println("Please enter five to ten numbers separated by spaces on one line.");
ArrayList<Double> userNumbers = new ArrayList<>();
ArrayList<Double> numCopy = new ArrayList<>(userNumbers); //Created a copy of the array.
String numbers = input.nextLine();
String[] strHolder = numbers.split("\\s+");
for(int counter = 0; counter < strHolder.length; counter++){
userNumbers.add(counter, Double.parseDouble(strHolder[counter])); //Adding numbers to the array
}
return userNumbers;
}
public static int getLowestNumber(){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the amount of lowest numbers to drop.");
int n = input.nextInt();
return n;
}
public static double calculateAverage(ArrayList<Double> numCopy, int n){
double average;
double sum = 0;
Collections.sort(numCopy, Collections.reverseOrder()); //Sorting the array list in reverse.
for(int counter = 0; counter < numCopy.size() - n; counter++){
sum = sum + numCopy.get(counter);
}
average = sum / (numCopy.size() - n); //Finding the average of the array copy
return average;
}
public static void printResults(ArrayList<Double> userNumbers, double average, int n){
System.out.print("Given the numbers ");
for(int counter = 0; counter < userNumbers.size(); counter++){ //This should print out the original array.
if(counter == userNumbers.size() - 1 ){
System.out.print("and " + userNumbers.get(counter) + ", " );
}
else{
System.out.print(df2.format(userNumbers.get(counter)) + ", ");
}
}
System.out.println("the average of all the numbers except the lowest " + n + " is " + df2.format(average) + ".");
}
public static void main(String args[]){
ArrayList<Double> userNumbers = getArrayNumbers();
int n = getLowestNumber();
double average = calculateAverage(userNumbers, n);
printResults(userNumbers, average, n);
}
}