Question

In: Computer Science

Change your program to additionally handle the operation “px” where x is a number from 10...

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");
           }
   }
}

Solutions

Expert Solution

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.


Related Solutions

a. The demand for good x is given by x∗ = 60−4Px +2M +Py, where Px...
a. The demand for good x is given by x∗ = 60−4Px +2M +Py, where Px is the price of good x, Py is the price of good y, and M is income. Find the own-price elasticity of demand for good x when Px =20,Py =20, and M =100. Is x an ordinary or giffen good? Explain. b. The demand for good x is given by x∗ = 60−4Px +2M +Py, where Px is the price of good x, Py...
If the demand for good X is QdX = 10 + aX PX + aY PY...
If the demand for good X is QdX = 10 + aX PX + aY PY + aMM. If M is the income and aM is positive, then: Goods y and x are complements Good x is an interior good Goods y and x are not related goods Goods y and x are interior goods Good x is a normal goods
Qx=100−10Px−2Py+0.1M+0.2A where Qx is unit sales of good x, Px is the price of good x,...
Qx=100−10Px−2Py+0.1M+0.2A where Qx is unit sales of good x, Px is the price of good x, Py is the price of good y, M is per-family money income in thousands of dollars, and A is the level of advertising expenditures in thousands of dollars. Suppose that Px=$2,Py=$3,M=$50,andA=$20Px=$2,Py=$3,M=$50,andA=$20. Calculate the price elasticity of demand. • Silkwood Enterprises specializes in gardening supplies. The demand for its new brand of fertilizer is given by the equation Q=120−4P a. Silkwood is currently charging $10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above...
QD=8000-2PX+0.4I+2PY-4PZ Where QD = quantity demanded of good X PX = price of good X I...
QD=8000-2PX+0.4I+2PY-4PZ Where QD = quantity demanded of good X PX = price of good X I = consumer income, in thousands PY = price of good Y PZ = price of good Z a. Based on the demand curve above, is X a normal or an inferior good? b. Based on the demand curve above, what is the relationship between good X and good Y? c. Based on the demand curve above, what is the relationship between good X and...
Describe in your own words the standard normal distribution. Additionally, give a real-life example of where...
Describe in your own words the standard normal distribution. Additionally, give a real-life example of where we may use the standard normal distribution.
Number of Police Officers (X)    Number of Crimes (Y) 0    10 20    10...
Number of Police Officers (X)    Number of Crimes (Y) 0    10 20    10    30    40 a. Calculate the correlation coefficient and the covariance b. Calculate the regression coefficient of the slope. c. State the regression equation. d. Interpret the meaning of the intercept and slopes in this equation. Be sure to explain using units. e. Predict the number of crimes when the number of police officers is 7.
The private marginal benefit for commodity X is given by 10 – X where X is...
The private marginal benefit for commodity X is given by 10 – X where X is the number of units consumed. The private marginal cost of producing X is constant at $5. For each unit of X produced, an external cost of $2 is imposed on members of society. a. In the absence of any government intervention, how much X is produced? b. What is the efficient level of production of X? c. What is the gain to society of...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5?...
Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5? Write a program to perform the summation of 10 even number starting from 2? Write a program to perform the summation of 10 odd number starting from 2? Write a program to perform the summation of 10 number starting from 2 and increment is given by user? Write a program to combine all operations from 1 to 4 in a single program using ‘Switch’...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT