Assignment9java You are not allowed to use the Scanner class
Assignment9.java
You are not allowed to use the Scanner class in this assignment and any assignment after this one. You will need to use InputStreamReader andBufferedReader (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, compute the sum of odd numbers, count negative numbers, and compute the sum of numbers in the array are less than the last number entered right before 0. (You can assume that a user will enter at least one non-zero number before 0 is entered.) using recursion. Thus you will create recursive methodsfindMin, computeSumOfOdd, countNagative, and sumLessThanLast 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 computeSumOfOdd(int[] numbers, int startIndex, int endIndex)
public static int countNegative(int[] numbers, int startIndex, int endIndex)
public static int sumLessThanLast(int[] numbers, int startIndex, int endIndex, int lastNumber)
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 sum of odd numbers is 0
The count of negative numbers is 0
The sum of numbers that are less than the last 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.
Solution
Code:
package assign9;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.text.DecimalFormat;
public class Assign9 {
 public static void main (String[] args) throws IOException
 {
 // Initialize array
 int[] numbers = new int[100];
 int i = -1;
 String line;
InputStreamReader isr = new InputStreamReader(System.in);
 BufferedReader stdin = new BufferedReader(isr);
// read numbers from standered input
 do
 {
 i++;
 //read as string
 line = stdin.readLine();
 line = line.trim();
 //parse integer
 numbers[i] = Integer.parseInt(line);
} while ( numbers[i] != 0.0 );
 //i+1 contains numbers read
int end=i;
if (end%2 == 0)
 end--;
double oddSum = computeSumAtOdd(numbers, 0, end);
 double min = findMin(numbers, 0, i);
 double negSum = computeNegativeSum(numbers, 0, i);
 int sum = sumLessThanLast(numbers, 0, i,numbers[i]);
//Using formats to print out the results
 DecimalFormat fmt1 = new DecimalFormat (\"0.00#\");
 DecimalFormat fmt2 = new DecimalFormat (\"0.00\");
 DecimalFormat fmt3 = new DecimalFormat (\"0.###\");
System.out.println(\"The minimum number is \" + fmt1.format(min));
 System.out.println(\"The sum of the numbers at odd indexes is \"
 + fmt2.format(oddSum));
 System.out.println(\"The sum of the negative numbers is \"
 + fmt3.format(negSum));
 System.out.println(\"The sum of numbers that are less than the last is \" + sum);
 }
 public static double computeSumAtOdd(int[] elements, int startIndex, int endIndex)
 {
 if (endIndex == 1)
 {
 return elements[1];
 }
 else
 {
    //odd index
 if (endIndex%2 != 0)
 return elements[endIndex] + computeSumAtOdd(elements, startIndex, endIndex-1);
 else
 return computeSumAtOdd(elements, startIndex, endIndex-1);
 }
 }
 public static double findMin(int[] elements, int startIndex, int endIndex)
 {
 if (endIndex == startIndex)
 {
 return elements[0];
 }
 else
 {
 double previousMin = findMin(elements, startIndex, endIndex-1);
 if (previousMin > elements[endIndex])
 return elements[endIndex];
 else
 return previousMin;
 }
 }
 public static double computeNegativeSum(int[] elements, int startIndex, int endIndex)
 {
 if (endIndex == startIndex)
 {
 if (elements[0] < 0)
 return elements[0];
 else
 return 0;
 }
 else
 {
 double previousSum = computeNegativeSum(elements, startIndex, endIndex-1);
 if (elements[endIndex] < 0)
 return (elements[endIndex] + previousSum);
 else
 return previousSum;
}
 }
public static int countPositive(int[] elements, int startIndex, int endIndex)
 {
 if (startIndex == endIndex)
 {
 if (elements[0] > 0)
 return 1;
 else
 return 0;
 }
 else
 {
 int previousCount = countPositive(elements, startIndex, endIndex-1);
 if (elements[endIndex] > 0)
 return (1 + previousCount);
 else
 return previousCount;
 }
}
 public static int sumLessThanLast(int[] elements, int startIndex, int endIndex, int lastNumber)
 {
 int last=lastNumber;
 if (startIndex == endIndex)
 {
 if(elements[endIndex]<lastNumber)
 return (elements[endIndex]);
 else
 return 0;
 }
 else
 {
 int sum = sumLessThanLast(elements, startIndex, endIndex-1,last);
 
 if (elements[endIndex] < lastNumber)
 {
 
 return (elements[endIndex] + sum);
 }
 else
 return sum;
 }
   
 }
   
 }
Output:
run:
2
3
-2
4
0
The minimum number is -2.00
The sum of the numbers at odd indexes is 7.00
The sum of the negative numbers is -2
The sum of numbers that are less than the last is -2
BUILD SUCCESSFUL (total time: 14 seconds)




