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
(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...
(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...
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...
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...
AVL tree; Insert and Range Minimum operation in Java code.
AVL tree; Insert and Range Minimum operation in Java code.
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT