Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are not allowed to use the Scanner class in this assignment and any assignme

ID: 3803729 • 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.

Assignment.java 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.

Input

Test Case #1


Test Case #2


Test Case #3


Test Case #4

Output

Test Case #1


Test Case #2


Test Case #3


Test Case #4

Error Handling

Your program is expected to be robust enough to pass all test cases.

Explanation / Answer

import java.io.*;
public class mathFunctions {
  
   public static int findMin(int[] numbers, int startIndex, int endIndex)
   {
       if(startIndex>endIndex) return 1000000000; // return a large number
       if(startIndex==endIndex) return numbers[startIndex];
       int mid = (endIndex + startIndex)/2;
      
       //recurse
       int x = findMin(numbers,startIndex,mid);
       int y = findMin(numbers,mid+1,endIndex);
       if(x<y) return x;
       else return y;
   }
  
   public static int computeSumAtEvenIndexes(int[] numbers, int startIndex, int endIndex)
   {
       if(startIndex>endIndex) return 0; // return zero
       if(startIndex==endIndex)
       {
           if(startIndex%2==0)
               return numbers[startIndex];
       }
       int mid = (endIndex + startIndex)/2;
       return computeSumAtEvenIndexes(numbers,startIndex,mid)+computeSumAtEvenIndexes(numbers,mid+1,endIndex);
   }
  
   public static int countDivisibleBy3(int[] numbers, int startIndex, int endIndex)
   {
       if(startIndex>endIndex) return 0;
       if(startIndex==endIndex)
       {
           if(numbers[startIndex]%3==0) return 1;
       }
       int mid = (endIndex + startIndex)/2;
       return countDivisibleBy3(numbers,startIndex,mid)+countDivisibleBy3(numbers,mid+1,endIndex);
   }
     
   public static int findMaxOfLessThanFirst(int[] numbers, int startIndex, int endIndex, int firstNumber)
   {
       if(startIndex>endIndex) return -1000000000; // return a large negative number
       if(startIndex==endIndex)
       {
           if(numbers[startIndex]<firstNumber) return numbers[startIndex];
       }
       int mid = (endIndex + startIndex)/2;
       int x = findMaxOfLessThanFirst(numbers,startIndex,mid,firstNumber);
       int y = findMaxOfLessThanFirst(numbers,mid+1,endIndex,firstNumber);
       if(x>y) return x;
       else return y;
   }
  
   public static void main(String [] args) throws Exception
   {
       int arr[] = new int[100];
       BufferedReader bufferedReader = new BufferedReader(new FileReader("numbers.txt"));
       String line = null;
       int i = 0;
       while ((line = bufferedReader.readLine()) != null)
   {
   arr[i++] = Integer.parseInt(line);
   if(arr[i-1]==0) break;
   }
       bufferedReader.close();
       System.out.println("The minimum number is " + findMin(arr, 0, i-1));
       System.out.println("The sum of numbers at even indexes is " + computeSumAtEvenIndexes(arr, 0, i-1));
       System.out.println("The count of numbers that are divisible by 3 is " + countDivisibleBy3(arr, 0, i-1));
       System.out.println("The maximum number among numbers that are less than the first number is " + findMaxOfLessThanFirst(arr, 0, i-1, arr[0]) );
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote