Question

In: Computer Science

Provide the Java code to compute the sum, average, maximum number and minimum number if I...

Provide the Java code to compute the sum, average, maximum number and minimum number if I have a string sentence of double numbers. Assume that the length of the string sentence is not known. It can be of any length.

To split a string based on the comma character use the following.

String sentence = "A,B,C,D,E";

String[] stringsArray = receivedSentence.split(",");

Then stringsArray is an array of five elements such that:

stringsArray[0] = 'A'

stringsArray[1] = 'B'

stringsArray[2] = 'C'

stringsArray[3] = 'D'

stringsArray[4] = 'E'

stringsArray.length will equal 5.

Solutions

Expert Solution

package newone;

public class doubleStats {

   public static void main(String[] args) {
       String sentence = "1.1,4.5,6,7,12,-56,34.34,562.4,322";

       String[] stringsArray = sentence.split(",");
       double[] arr = new double[stringsArray.length];
       double max = Double.parseDouble(stringsArray[0]);
       double min = Double.parseDouble(stringsArray[0]);
       double n=0,total=0,avg;
       for(String s:stringsArray) {
           n+=1;
           double num = Double.parseDouble(s);
           total+=num;
           if(num>max)
               max=num;
           if(num<min)
               min=num;
       }
       avg = total/n;
       System.out.println("Total: "+total);
       System.out.println("Average: "+avg);
       System.out.println("Maximum: "+max);
       System.out.println("Minimum: "+min);

   }

}


Related Solutions

write a java code Write a generic program to compute the sum of 30 terms of...
write a java code Write a generic program to compute the sum of 30 terms of the following series. (181 - 5)/31 + (186 + 9)/34 - (191 - 13)/37 + (196 + 17)/40 + . . . . . 1 5 11 Note: the 3rd, 6th, 9th term etc, has a negative sign in front of parenthesis. User Input: None Expected output: Term Ratio Sum 1 5.677 5.677 2 5.735 11.413 3 -4.811 6.602
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number...
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number of input integers or float numbers. In other words, calls like this should all work: flexible_sum(x1, x2) # sum of two integer or float numbers or strings x1 and x2 flexible_sum(x1, x2, x3) # sum of 3 input parameters and can also handle a single input parameter, returning it unchanged and with the same type, i.e.:   flexible_sum(1) returns 1 and can accept an arbitrary...
Complete the following Java code. Compute the average of all the numbers in nums and store...
Complete the following Java code. Compute the average of all the numbers in nums and store it in a variable called average      public static void main(String[] args){                         int[] nums = <some array values>;
I need to write a c/c++ code to find the maximum sum in those possible combinations...
I need to write a c/c++ code to find the maximum sum in those possible combinations of arrays. There are N arrays with S elements. ex2: 4 arrays with 3 elements.We want to find the greater number in the same column of two arrays(may possible be three or four...arrays), and sum those greater number to find the greatest sum in all of the combinations in those 4 arrays, like: A: [50, 60, 70] B: [80, 40, 20] C: [30, 100,...
(in java) Find the maximum value and minimum value in milesTracker. Assign the maximum value to...
(in java) Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program: Min miles: -10 Max miles: 40 given code below (please bold the solution, thank you!) import java.util.Scanner; public class ArraysKeyValue { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_ROWS = 2; final int NUM_COLS = 2; int [][] milesTracker = new int[NUM_ROWS][NUM_COLS]; int...
MIPS code to calculate The 1- minimum 2- maximum 3- mid value of three number, user...
MIPS code to calculate The 1- minimum 2- maximum 3- mid value of three number, user will enter the numbers
Language: Java So I am trying to find the MAJOR and MINOR diagonal SUM and AVERAGE...
Language: Java So I am trying to find the MAJOR and MINOR diagonal SUM and AVERAGE of a 2d matrix using only ONE class. However, the output gives me the incorrect calculations. This is my class: public static void MajorAndMinorDiagonalSumAndAvg (Scanner user, int rows, int coluumn, int [][] array) { double majorarray = 0; double majorarraycount = 0; double minorarray = 0; double minorarraycount = 0; for (int i = 0; i<array.length; i++) for(int j =0; j<array[i].length; j++) { majorarray...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
In Java!! Problem 2 – Compute the sum of the series (19%) The natural logarithm of...
In Java!! Problem 2 – Compute the sum of the series (19%) The natural logarithm of 2, ln(2), is an irrational number, and can be calculated by using the following series: 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 - 1/8 + ... 1/n The result is an approximation of ln(2). The result is more accurate when the number n goes larger. Compute the natural logarithm of 2, by adding up to n terms in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT