In: Computer Science
Change your program to additionally handle the operation “px” where x is a number from 10 to 90 and defines the x percentile of the list of numbers. E.g.: Input.txt: Min: 1,2,3,5,6 Max: 1,2,3,5,6 Avg: 1,2,3,5,6 P90: 1,2,3,4,5,6,7,8,9,10 Sum: 1,2,3,5,6 P70: 1,2,3 ● Your output.txt should read: The min of [1,2,3,5,6] is 1. The max of [1,2,3,5,6] is 6. The avg of [1,2,3,5,6] is 3.4. The 90th percentile of [1,2,3,4,5,6,7,8,9,10] is 9. The sum of [1,2,3,5,6] is 17. The 70th percentile of [1,2,3] is 2. ● Compile, save and run your file
PLEASE ADD THE ANSWER TO THIS CODE
import java.io.File;
import java.util.Arrays;
import java.util.Formatter;
import java.util.Iterator;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class MyFile {
public static void main(String[] args){
try {
File j = new
File("C:\\Users\\NaharaY\\Dropbox\\Jesse -23263\\Introduction to
Software Engineering\\Task 12\\input.txt");
Scanner input =
new Scanner(j);
PrintWriter pw =
new PrintWriter(new File("output.txt"));
while(input.hasNext()) {
String line =
input.next();// store the line in a variable - eg. String line =
input.next();
String[]
numOperation = line.split(":");// split the line on the
semi-colon
String operation
= numOperation[0];// assign the operation to a variable at index 0
of operation_numbers
String[] digits
= numOperation[1].split(",");// assign the numbers to a variable
using index 1 of operation_numbers - split the numbers on the
commas
int len =
digits.length;
int arr[] = new
int[len];
for(int i = 0; i
< len; i++)
arr[i] =
Integer.parseInt(digits[i].trim());
if(operation.equalsIgnoreCase("Min")) {
int min = arr[0];
for(int i = 0; i
< len; i++) {
if(min > arr[i])
min = arr[i];
}
pw.println("The
min of " + Arrays.toString(arr) + " is " + min);
}
else if
(operation.equalsIgnoreCase("Max")) {
int max = arr[0];
for(int i = 0; i
< len; i++) {
if(max < arr[i])
max = arr[i];
}
pw.println("The
max of " + Arrays.toString(arr) + " is " + max);
}
else
if(operation.equalsIgnoreCase("Avg")) {
double avg = 0.0;
for(int i = 0; i
< len; i++) {
avg += arr[i];
}
avg /=
len;
pw.println("The
avg of " + Arrays.toString(arr) + " is " + avg);
}
}
pw.close();
input.close();
}catch(FileNotFoundException e) {
System.out.println("Error");
}
}
}
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class MyFile { public static void main(String[] args) { try { File j = new File( "input.txt"); // change the path Scanner input = new Scanner(j); PrintWriter pw = new PrintWriter(new File("output.txt")); while (input.hasNextLine()) { String line = input.nextLine();// store the line in a variable - eg. String line = input.next(); String[] numOperation = line.split(":");// split the line on the semi-colon String operation = numOperation[0];// assign the operation to a variable at index 0 of operation_numbers String[] digits = numOperation[1].split(",");// assign the numbers to a variable using index 1 of // operation_numbers - split the numbers on the commas int len = digits.length; int arr[] = new int[len]; for (int i = 0; i < len; i++) arr[i] = Integer.parseInt(digits[i].trim()); if (operation.equalsIgnoreCase("Min")) { int min = arr[0]; for (int i = 0; i < len; i++) { if (min > arr[i]) min = arr[i]; } pw.println("The min of " + Arrays.toString(arr) + " is " + min); } else if (operation.equalsIgnoreCase("Max")) { int max = arr[0]; for (int i = 0; i < len; i++) { if (max < arr[i]) max = arr[i]; } pw.println("The max of " + Arrays.toString(arr) + " is " + max); } else if (operation.equalsIgnoreCase("Avg")) { double avg = 0.0; for (int i = 0; i < len; i++) { avg += arr[i]; } avg /= len; pw.println("The avg of " + Arrays.toString(arr) + " is " + avg); } else if (operation.equalsIgnoreCase("Sum")) { double sum = 0.0; for (int i = 0; i < len; i++) { sum += arr[i]; } pw.println("The sum of " + Arrays.toString(arr) + " is " + sum); } else if (operation.startsWith("P")) { int perc = Integer.parseInt(operation.substring(1).trim()); int index = (int) Math.floor(len * perc/100.0) - 1; pw.println("The " + perc + "th percentile of " + Arrays.toString(arr) + " is " + arr[index]); } } pw.close(); input.close(); } catch (FileNotFoundException e) { System.out.println("Error"); } } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.