Important: This is an individual assignment. Please do not collaborate. Minimal
ID: 3641229 • Letter: I
Question
Important: This is an individual assignment. Please do not collaborate.
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, the sum of negative numbers in the array, the sum of numbers at odd indexes (1,3, 5,...), and count the number of even numbers within the array, using recursion. Thus you will create recursive methods findMin, computeNegativeSum, computeSumAtOdd, and countEven 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 computeNegativeSum(int[] numbers, int startIndex, int endIndex)
public static int computeSumAtOdd(int[] numbers, int startIndex, int endIndex)
public static int countEven(int[] numbers, int startIndex, int endIndex)
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 the negative numbers is 0
The sum of the numbers at odd indexes is 0
The total count of even integers 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.
Input
Explanation / Answer
please rate - thanks
sorry, min I can only do rearranging the numbers, so if you rearrage the order of the calls sum at odd index won't work. the others all work
import java.io.*;
public class recursivemethods3
{public static void main(String[] args) throws IOException
{BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String input;
int i=-1,max=100;
int []a=new int[max];
do
{i++;
System.out.print("Enter element "+(i+1)+"(0 to exit): ");
input=in.readLine();
a[i]=Integer.parseInt(input);
}while(a[i]!=0&&i<max-1);
System.out.println("Your numbers are:");
for(int j=0;j<=i;j++)
System.out.print(a[j]+" ");
System.out.println();
System.out.println("Negative sum: "+computeNegativeSum(a,0,i));
System.out.println("Sum at odd: "+computeSumAtOdd(a,0,i));
System.out.println("Number of even numbers: "+countEven(a,0,i));
System.out.println("Smallest number: "+findMin(a,0,i));
}
public static int findMin(int[] numbers, int startIndex, int endIndex)
{if(startIndex==endIndex-1)
return numbers[startIndex];
else
{if(numbers[startIndex]>numbers[endIndex-1])
{int n=numbers[endIndex-1];
numbers[endIndex-1]=numbers[startIndex];
numbers[startIndex]=n;
}
return findMin(numbers,startIndex,endIndex-1);
}
}
public static int computeNegativeSum(int[] numbers, int startIndex, int endIndex)
{if(startIndex==endIndex)
return 0;
else
if(numbers[startIndex]<0)
return numbers[startIndex]+computeNegativeSum(numbers,startIndex+1,endIndex);
else
return computeNegativeSum(numbers,startIndex+1,endIndex);
}
public static int computeSumAtOdd(int[] numbers, int startIndex, int endIndex)
{
if(startIndex>=endIndex)
return 0;
else
{
if(startIndex%2==1)
return numbers[startIndex]+computeSumAtOdd(numbers,startIndex+1,endIndex);
else
return computeSumAtOdd(numbers,startIndex+1,endIndex);
}
}
public static int countEven(int[] numbers, int startIndex, int endIndex)
{if(startIndex==endIndex)
return 0;
else
if(numbers[startIndex]%2==0)
return 1+countEven(numbers,startIndex+1,endIndex);
else
return countEven(numbers,startIndex+1,endIndex);
}
}