You are not allowed to use the Scanner class in this assignment and any assignme
ID: 3806859 • Letter: Y
Question
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, compute the sum at even indexes (0, 2, 4, ...) count numbers that are divisible by 3 and compute the maximum number among the numbers that are less than the first number (You can assume that a user will enter at least one number before 0 is entered, thus at least two numbers will be entered including 0.) using recursion. Thus you will create recursive methods findMin,computeSumAtEvenIndexes, countDivisibleBy3, and findMaxOfLessThanFirst 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 computeSumAtEvenIndexes(int[] numbers, int startIndex, int endIndex)
public static int countDivisibleBy3(int[] numbers, int startIndex, int endIndex)
public static int findMaxOfLessThanFirst(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 sum of numbers at even indexes is 0
The count of numbers that are divisible by 3 is 0
The maximum number among numbers that are less than the first number 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.
test case :
Output:
Explanation / Answer
public class RecursiveFunctions {
//This method divides the array into two parts till only one number remains
public static int findMin(int[] numbers, int startIndex, int endIndex)
{
if(startIndex==endIndex) //Boundary condition
return numbers[startIndex];
int mid,m1,m2;
mid = (startIndex+endIndex)/2;
m1 = findMin(numbers,startIndex,mid);
m2 = findMin(numbers,mid+1,endIndex);
return m2<m1?m2:m1;
}
//StartIndex is incremented by 2 so that once a even index is encountered, only even index will be passed again
public static int computeSumAtEvenIndexes(int[] numbers, int startIndex, int endIndex)
{
if(startIndex>=endIndex) //Boundary condition
{
if(startIndex%2==0)
return numbers[startIndex];
else
return 0;
}
else
{
if(startIndex%2==0)
return numbers[startIndex]+computeSumAtEvenIndexes(numbers,startIndex+2,endIndex);
}
return 0;
}
//keeps checking all the number till there is only one number to process
public static int countDivisibleBy3(int[] numbers, int startIndex, int endIndex)
{
if(numbers[startIndex]%3==0)
{
if(startIndex!=endIndex)
return 1 + countDivisibleBy3(numbers,startIndex+1,endIndex);
else
return 1; //Boundary condition
}
return countDivisibleBy3(numbers,startIndex+1,endIndex);
}
//This method divides the array into two parts till only one number remains, this number is checked with firstNumber
public static int findMaxOfLessThanFirst(int[] numbers, int startIndex, int endIndex, int firstNumber)
{
if(startIndex==endIndex) //Boundary condition
{
if(numbers[startIndex]<firstNumber)
return numbers[startIndex];
else
return 0;
}
int mid,m1,m2;
mid = (startIndex+endIndex)/2;
m1 = findMaxOfLessThanFirst(numbers,startIndex,mid,firstNumber);
m2 = findMaxOfLessThanFirst(numbers,mid+1,endIndex,firstNumber);
return m2>m1?m2:m1;
}
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] numbers = new int[100]; //Initializing the array
int num=1,i=0;
//Loop till input number is not 0
while(num!=0)
{
num = Integer.parseInt(br.readLine());
numbers[i++]=num;
}
//Calling the required methods
System.out.println("The minimum number is "+findMin(numbers, 0, i-1));
System.out.println("The sum of numbers at even indexes is "+computeSumAtEvenIndexes(numbers, 0, i-1));
System.out.println("The count of numbers that are divisible by 3 is "+countDivisibleBy3(numbers, 0, i-1));
System.out.println("The maximum number among numbers that are less than the first number is "+findMaxOfLessThanFirst(numbers, 1, i-1, numbers[0]));
}
}
Sample Output
10
2
3
4
5
8
9
0
The minimum number is 0
The sum of numbers at even indexes is 27
The count of numbers that are divisible by 3 is 3
The maximum number among numbers that are less than the first number is 9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.