Write a static method called runningSum that accepts as a pa

Write a static method called runningSum that accepts as a parameter a Scanner holding a sequence of real numbers and that outputs the running sum of the numbers followed by the maximum running sum. For example if the Scanner contains the following data: 3.25 4.5 - 8.25 7.25 3.5 4.25 - 6.5 5.25.

program needs to be in Java

Solution

RunningSumTest.java

import java.util.Scanner;


public class RunningSumTest {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter sequence of real numbers: \");
       runningSum(scan);
   }
   public static void runningSum (Scanner scan){
       String s = scan.nextLine();
       String nums[] = s.split(\" \");
       double d[] = new double[nums.length];
       for(int i=0; i<d.length; i++){
           d[i] = Double.parseDouble(nums[i]);
       }
       double max = d[0];
       double sum = 0;
       for(int i=0; i<d.length; i++){
           sum = sum +d[i];
           if(max < d[i]){
               max = d[i];
           }
       }
       System.out.println(\"The running sum of the numbers: \"+sum);
       System.out.println(\"Maximum running sum: \"+max);
   }

}

Output:

Enter sequence of real numbers:
3.25 4.5 8.25 7.25 3.5 4.25 -6.5 5.25
The running sum of the numbers: 29.75
Maximum running sum: 8.25

Write a static method called runningSum that accepts as a parameter a Scanner holding a sequence of real numbers and that outputs the running sum of the numbers

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site