216k views
3 votes
In this problem, there 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 odd integers, compute the sum of numbers that are larger than the first number in the array, and compute the largest even integer in the sequence using recursion. Thus, you will create recursive methods findMin, countOddNumbers, computeLargestEven, and sumOfNumbersLargerThanFirst 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 countOddNumbers(int[] elements, int startIndex, int endIndex)
public static int computeLargestEven(int[] elements, int startIndex, int endIndex)
public static int sumOfNumbersLargerThanFirst(int[] elements, int startIndex, int endIndex, int firstNumber)

If these methods are implemented using a Loop or any Static Variable, points will be deducted 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 count of odd integers in the sequence is 0
The largest even integer in the sequence is 0
The sum of numbers larger 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.

1 Answer

7 votes

Answer:

Check the explanation

Step-by-step explanation:

package util;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Scanner;

public class Assignment9 {

public Assignment9() {

List<Integer> numList = new ArrayList<Integer>();

Scanner sc = new Scanner(System.in);

System.out.println("Please enter the numbers , press 0 to stop");

int x = 0;

do {

x = sc.nextInt();

numList.add(x);

} while (x != 0);

int size = numList.size();

Integer[] numArray = new Integer[size];

Iterator<Integer> it = numList.iterator();

int i = 0;

while (it.hasNext()) {

numArray[i] = it.next();

i++;

}

int min = findMin(numArray, 0, 2);

System.out.println("The minimum number is " + min);

int sum = computeNegativeSum(numArray, 0, 2);

System.out.println("The sum of the negative numbers is " + sum);

int sumOdd = computeSumAtOdd(numArray, 0, 3);

System.out

.println("The sum of the numbers at odd indexes is " + sumOdd);

int countEven = computeCountEven(numArray, 0, 3);

System.out.println("The total count of even integers is " + countEven);

}

/**

* This method is used to compute the minimum number

* "at"param NumArray

* "at"param startIndex

* "at"param endIndex

* "at"return

*/

public int findMin(Integer[] NumArray, int startIndex, int endIndex) {

if (startIndex == endIndex)// base case50.

{

return NumArray[startIndex]; // return value is there is only one

// entry

} else if (findMin(NumArray, startIndex, endIndex - 1) < NumArray[endIndex]) {

return findMin(NumArray, startIndex, endIndex - 1);

} else {

return NumArray[endIndex];

}

}

/**

* This method is used to find the sum of negative numbers in the array

* "at"param NumArray

* "at"param startIndex

* "at"param endIndex

* "at"return

*/

public int computeNegativeSum(Integer[] NumArray, int startIndex,

int endIndex) {

if (startIndex == endIndex) {

if (NumArray[startIndex] > 0) {

return 0;

} else {

return NumArray[startIndex];

}

} else if (NumArray[endIndex] < 0) {

return computeNegativeSum(NumArray, startIndex, endIndex - 1)

+ NumArray[endIndex];

} else {

return computeNegativeSum(NumArray, startIndex, endIndex - 1); // if

}

}

/**

* This method is used to find the sum of numbers at odd indexes (1,3, 5,...),

*

* "at"param NumArray

* "at"param startIndex

* "at"param endIndex

* "at"return

*/

public int computeSumAtOdd(Integer[] NumArray, int startIndex, int endIndex) {

if (startIndex == endIndex) {

if (startIndex % 2 == 1) {

return NumArray[startIndex];

} else {

return 0;

}

} else {

if (endIndex % 2 == 1) {

return computeSumAtOdd(NumArray, startIndex, endIndex - 1)

+ NumArray[endIndex];

} else {

return computeSumAtOdd(NumArray, startIndex, endIndex - 1);

}

}

}

/**

* This method is used to find the number of even numbers within the array

*

* "at"param NumArray

* "at"param startIndex

* "at"param endIndex

* "at"return

*/

public int computeCountEven(Integer[] NumArray, int startIndex, int endIndex) {

if (startIndex == endIndex) {

if (NumArray[startIndex] % 2 == 0) {

return NumArray[startIndex];

} else {

return 0;

}

} else if (NumArray[endIndex] % 2 == 0) {

return computeCountEven(NumArray, startIndex, endIndex - 1)

+ NumArray[endIndex];

} else {

return computeCountEven(NumArray, startIndex, endIndex - 1); // if

}

}

public static void main(String args[]) {

Assignment9 assignment9 = new Assignment9();

}

}

Sample Output is :

*************************

Please enter the numbers , press 0 to stop

5

7

3

2

4

-7

-3

0

The minimum number is 3

The sum of the negative numbers is 0

The sum of the numbers at odd indexes is 9

The total count of even integers is 2

User Jolinda
by
6.0k points