Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Minimal Submitted Files You are required, but not limited, to turn in the follow

ID: 3681929 • Letter: M

Question

Minimal Submitted Files

You are required, but not limited, to turn in the following source files:

Assignment9.java

Requirements to get full credits in Documentation

The assignment number, your name, student ID, lecture number/time, and a class description need to be included at the top of each file/class.

A description of each method is also needed.

Some additional comments inside of methods (especially for the "main" method) to explain codes that are hard to follow should be written.

You can look at the Java programs in the text book to see how comments are added to programs.

You are not allowed to use the Scanner class in this assignment and any assignment after this one. You will need to use InputStreamReader and BufferedReader (they are in java.io package) to process input and also take care of IOException.

New Skills to be Applied

In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:

Recursion
One-dimensional arrays

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 even numbers, compute the sum of negative numbers, and count how many numbers in the array are greater than the number at the index 0, using recursion. Thus you will create recursive methods findMin, countEven computeSumOfNegative, and countGreaterThanFirst in Assignment9 class and they will be called by a main method.

Specifically, the following recursive methods must be implemented (These methods should not contain any loop):

    public static int findMin(int[] numbers, int startIndex, int endIndex)

    public static int countEven(int[] numbers, int startIndex, int endIndex)

    public static int computeSumOfNegative(int[] numbers, int startIndex, int endIndex)

    public static int countGreaterThanFirst(int[] numbers, int startIndex, int endIndex, int firstNumber)

If these methods are implemented using a Loop or any Static Variable, points will be deducted (from the test cases) even if your program passes test cases. DO NOT use any Static Variables.

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 minimum number is 0

The total count of even numbers is 0

The sum of negative numbers is 0

The total count of numbers that are greater than the first is 0

Note that the result values will be different depending on test cases (not always 0).

Do not output a prompt to query for the numbers. The number 0 is included in the sequence of numbers and should be included in all of your calculations.

Explanation / Answer

Please find the required solution:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Assignment9 {

   public static void main( String[] args )
   {
       //declaration of array
       int[] numbers = new int[100];

       //read input using buffered reader
       BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
       int i = 0;
       try
       {
           do
           {
               System.out.println("enter the number:");
               //store input to array
               numbers[i] = Integer.parseInt(bReader.readLine());
           } while( numbers[i++] != 0 );//read data until user enter 0(sentinel condition)
           bReader.close();
       }
       catch( IOException e )
       {
           e.printStackTrace();
       }

       //print the results upon invoking the recursive methods
       System.out.println("Minimum number is :" + findMin(numbers, 0, i));
       System.out.println("Count of even numbers:" + countEven(numbers, 0, i));
       System.out.println("Sum of negative numbers:" + computeSumOfNegative(numbers, 0, i));
       System.out.println("CountGreaterThanFirst:" + countGreaterThanFirst(numbers, 1, i, numbers[0]));
   }

   /**
   * Recursive method to minimum of the given numbers
   */

   public static int findMin( int[] numbers, int startIndex, int endIndex )
   {
       int op1 = numbers[startIndex];
       int op2;

       //recursively invoke the method until the last index
       if( startIndex < endIndex - 1 )
           op2 = findMin(numbers, startIndex + 1, endIndex);
       else
       {
           //termination condition of recursion
           op2 = numbers[endIndex - 1];
           return op2;
       }

       //find the minimum of given numbers
       if( op1 < op2 )
           return op1;
       else
           return op2;
   }

   /**
   * Recursive method to count the number of even numbers
   */
   public static int countEven( int[] numbers, int startIndex, int endIndex )
   {
       int op1 = numbers[startIndex];
       int op2;

       //recursively invoke the method until the last index
       if( startIndex < endIndex - 1 )
           op2 = countEven(numbers, startIndex + 1, endIndex);
       else
       {
           //termination condition of recursion
           op2 = numbers[endIndex - 1];
           if( op2 % 2 == 0 )
               return 1;
           else
               return 0;
       }
       //compute the even numbers
       if( op1 % 2 == 0 )
           return 1 + op2;
       else
           return op2;
   }

   /**
   * Recursive method to find the sum of negative numbers
   */
   public static int computeSumOfNegative( int[] numbers, int startIndex, int endIndex )
   {
       int op1 = numbers[startIndex];
       int op2;

       //recursively call the method until the last index
       if( startIndex < endIndex - 1 )
           op2 = computeSumOfNegative(numbers, startIndex + 1, endIndex);
       else
       {
           //termination condition of recursion
           op2 = numbers[endIndex - 1];
           if( op2 < 0 )
               return op2;
           else
               return 0;
       }
       //compute the sum
       if( op1 < 0 )
           return op1 + op2;
       else
           return op2;
   }

   /**
   * Recursive method to find the count of numbers greater than first number
   */
   public static int countGreaterThanFirst( int[] numbers, int startIndex, int endIndex, int firstNumber )
   {
       int op1 = numbers[startIndex];
       int op2;

       //recursively invoke the method until the last index
       if( startIndex < endIndex - 1 )
           op2 = countGreaterThanFirst(numbers, startIndex + 1, endIndex, firstNumber);
       else
       {
           //termination condition of recursion
           op2 = numbers[endIndex - 1];
           if( op2 > firstNumber )
               return 1;
           else
               return 0;
       }

       //find the count
       if( op1 > firstNumber )
           return 1 + op2;
       else
           return op2;
   }//end of method

}//end of class

Sample output:

enter the number:
13
enter the number:
14
enter the number:
15
enter the number:
-23
enter the number:
-25
enter the number:
17
enter the number:
56
enter the number:
18
enter the number:
0
Minimum number is :-25
Count of even numbers:4
Sum of negative numbers:-48
CountGreaterThanFirst:5