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