Question

Need help figuring this out Write a program that reads in a sequence of numbers (doubles)...

Need help figuring this out

Write a program that reads in a sequence of numbers (doubles) from standard input until 0 is read, and stores them in an array, This part is done using iteration (loop). You may assume that the maximum size of the array will not be more than 100.

Your program computes the maximum number stored in the array, the count of negative numbers, and computes the sum of positive numbers, using recursion.

You need to have the following recursive methods findMax,  countNegative, and computeSumPositive in the class and they will be called in the main method.

Specifically, the following recursive methods must be implemented (You should not use loops to implement the following methods):

           public static double findMax(double[] numbers, int count)

     //finds the maximum number in the array, count is the number of numbers stored in the array

     public static int countNegative(double[] numbers, int count) -> returns the count of negative numbers

     public static double computeSumPositive(double[] numbers, int count) -> returns the sum of positive numbers

Do not implement using loops, do not use any static variables either.

The program should output the results of those calculations to standard output. Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted in the following format:

The maximum number is 0.##
The total number of negative numbers is 0
The sum of positive numbers is 0.##

To format numbers as above, you should use the DecimalFormat class (java.text package).

Do not prompt the user for the numbers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear student , please find the solution below.

Please feel free to ask any doubt in comment section.

import java.util.Scanner;

public class NOV11 {

   //Maximum number stored in the array
   public static double findMax(double[] numbers, int len) {
       if (len == 1) {
           return numbers[0];
       } else {
           return Math.max(numbers[len - 1], findMax(numbers, len - 1));
       }
   }

   // Count of negative numbers
   public static int countNegative(double[] arr, int n, int count) {

       if (n < 0) {
           return count;
       }

       // If current element is negative then increase the count
       if ((arr[n]) < 0) {
           count++;
       }

       return countNegative(arr, n - 1, count);
   }

   // Computes the sum of positive numbers, using recursion
   public static double computeSumPositive(double[] arr, int len) {

       if (len == 0)
           return 0;
       else {
           // Using ternary operator ,finding +ve numbers
           return ((arr[len - 1] >= 0 ? arr[len - 1] : 0) + computeSumPositive(
                   arr, len - 1));
       }
   }

   // Driver Code
   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);
       double[] userInputArr = new double[100];
       int i = 0;
       double val;

       for (i = 0; i < 100; i++) {

           val = in.nextDouble();

           if (val == 0d) {
               break;
           } else {
               userInputArr[i] = val;
           }
       }
      
       // Closing scanner class
       in.close();

       System.out.println("Sum of positive numbers : "
               + computeSumPositive(userInputArr, userInputArr.length));
       System.out.println("Count of negative numbers : "
               + countNegative(userInputArr, userInputArr.length - 1, 0));
       System.out.println("Max Number : "
               + findMax(userInputArr, userInputArr.length));
   }
}

Add a comment
Know the answer?
Add Answer to:
Need help figuring this out Write a program that reads in a sequence of numbers (doubles)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program Description: Assignment #9 will be the construction of a program that reads in a sequence...

    Program Description: Assignment #9 will be the construction of a program that reads in a sequence of integers from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers. Then compute the minimum number, count odd integers, compute the sum of numbers that are larger than the first number in...

  • Assignment #9 will be the construction of a program that reads in a sequence of integers...

    Assignment #9 will be the construction of a program that reads in a sequence of integers from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers. Then compute the minimum number, compute the largest number among the numbers that are divisible by 2, count even numbers, and compute the...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • (Count positive and negative numbers and compute the average of numbers) Write a program that reads...

    (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link...

    Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link to the file: CountAndAverageNumbers.javaPreview the document. Make sure you include comments in your code where errors were corrected. If you do not flag each error with a comment, points will be deducted. Upload the corrected .java file here. The output of the corrected file looks like the following: Debug4Output.PNG This program reads an unspecified number of integers, determines how many positive and negative values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT