Assignment 9 will be the construction of a program that read
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 methods findMin, 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
PROGRAM CODE:
package Simple;
import java.util.Scanner;
public class MyMainClass {
public static int findMin(int[] numbers, int startIndex, int endIndex)
{
//checking which is smaller and returning that
if(numbers[startIndex]<numbers[endIndex])
return numbers[startIndex];
else
return numbers[endIndex];
}
public static int computeSumOfOdd(int[] numbers, int startIndex, int endIndex)
{
//if the number is not divisible by two then it is odd. returning the number if it is odd
if(numbers[startIndex]%2 != 0)
return numbers[startIndex];
else
return 0; //return 0 if not odd
}
public static int countNegative(int[] numbers, int startIndex, int endIndex)
{
if(numbers[startIndex]<0)
return 1; // returning 1 if the number is negative
else
return 0; // 0 if not negative
}
public static int sumLessThanLast(int[] numbers, int startIndex, int endIndex, int lastNumber)
{
if(numbers[startIndex]<lastNumber)
return numbers[startIndex]; // returning the number if it less than the last number
else
return 0; //returning 0 otherwise
}
public static void main(String[] args) {
int[] array = new int[100];
int[] temp = new int[100];// temporary array to hold the same values - for calculation purpose
int LastNum = 0, minNum= 0, sumOfOdd= 0, negativeCount = 0, sumLessThanLast= 0;
for(int i=0; i<100; i++)
{
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter a Number: \");
int num = sc.nextInt();
temp[i] = num;
array[i] = num;
if(num == 0)
{
LastNum = array[i-1];
break;
}
}
for(int i=0; i<array.length; i++)
{
//recursion for finding minimum number
if(i>0)
{
int minNumber = findMin(array, i-1, i);
//storing the minimum number in minNum
if(minNumber<minNum)
{
minNum = minNumber;
}
}
// recursion for sum of odd numbers
sumOfOdd = sumOfOdd + computeSumOfOdd(array, i, array.length);
//recursion for negative count
negativeCount = negativeCount + countNegative(array, i, array.length);
//recursion for sum less than the last
sumLessThanLast = sumLessThanLast + sumLessThanLast(array, i, array.length, LastNum);
}
//printing to screen
System.out.println(\"The minimum number is \" + minNum);
System.out.println(\"The sum of odd numbers is \"+ sumOfOdd);
System.out.println(\"The count of negative numbers is \" + negativeCount);
System.out.println(\"The sum of numbers that are less than the last is \" + sumLessThanLast);
}
}
OUTPUT:
Enter a Number:
20
Enter a Number:
32
Enter a Number:
-3
Enter a Number:
67
Enter a Number:
33
Enter a Number:
-2
Enter a Number:
-2
Enter a Number:
0
The minimum number is -3
The sum of odd numbers is 97
The count of negative numbers is 3
The sum of numbers that are less than the last is -3



