Question

Assignment #9 will be the construction of a program that reads in a sequence of integers...

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 largest number among the numbers that are divisible by 2, count even numbers, and compute the sum at indexes divisible by 3 (0, 3, 6, ...) using recursion. Thus you will create recursive methods findMin,computeMaxDivisibleBy2, countEvenNumbers, and computeSumOfNumbersAtIndexDivisibleBy3 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 computeMaxDivisibleBy2(int[] elements, int startIndex, int endIndex)

    public static int countEvenNumbers(int[] elements, int startIndex, int endIndex)

    public static int computeSumOfNumbersAtIndexDivisibleBy3(int[] elements, 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 largest integer that is divisible by 2 is 0

The count of even numbers in the sequence is 0

The sum of numbers at indexes divisible by 3 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Java Program

public class RecursionDemo {

/**

* Recursive method to find the Minimum value of an array using recursion

* @param numbers

* @param startIndex

* @param endIndex

* @return

*/

public static int findMin(int[] numbers, int startIndex, int endIndex)

{

if(startIndex == endIndex)

return numbers[startIndex];

else

return Math.min(numbers[startIndex], findMin(numbers, startIndex + 1, endIndex));

}

/**

* Recursive method to find the maximum number divisible by 2

* @param elements

* @param startIndex

* @param endIndex

* @return

*/

public static int computeMaxDivisibleBy2(int[] elements, int startIndex, int endIndex)

{

if(startIndex == endIndex)

return elements[startIndex];

else

{

if(elements[startIndex] % 2 == 0)

{

return Math.max(elements[startIndex], computeMaxDivisibleBy2(elements, startIndex + 1, endIndex));

}

else

{

return computeMaxDivisibleBy2(elements, startIndex + 1, endIndex);

}

}

}

/**

* Recursive method to count even numbers within an array

* @param elements

* @param startIndex

* @param endIndex

* @return

*/

public static int countEvenNumbers(int[] elements, int startIndex, int endIndex)

{

if(startIndex == endIndex && elements[startIndex] % 2 == 0)

return 1;

else if(startIndex == endIndex)

return 0;

int count;

if(elements[startIndex] % 2 == 0)

count = 1;

else

count = 0;

return count + countEvenNumbers(elements, startIndex + 1, endIndex);

}

/**

* Recursive methods to find the sum of numbers at index divisible by 3

* @param elements

* @param startIndex

* @param endIndex

* @return

*/

public static int computeSumOfNumbersAtIndexDivisibleBy3(int[] elements, int startIndex, int endIndex)

{

if(startIndex >= endIndex)

{

if(startIndex == endIndex && startIndex % 3 == 0)

{

return elements[startIndex];

}

else

return 0;

}

else

{

if(startIndex % 3 == 0)

{

return elements[startIndex] + computeSumOfNumbersAtIndexDivisibleBy3(elements, startIndex + 3, endIndex);

}

else

{

return computeSumOfNumbersAtIndexDivisibleBy3(elements, startIndex + 3, endIndex);

}

}

}

public static void main(String args[])

{

int arr[] = {2, 8, 10, 4, 6, 11, 18, -1, 4}; // change the array values to test the methods

int startIndex = 0;

int endIndex = arr.length - 1;

System.out.println("The minimum number is: " + findMin(arr, startIndex, endIndex));

System.out.println("The maximum number divisible by 2 is: " + computeMaxDivisibleBy2(arr, startIndex, endIndex));

System.out.println("The number of even numbers is: " + countEvenNumbers(arr, startIndex, endIndex));

System.out.println("Sum of even numbers at index divisible by 3 is: " + computeSumOfNumbersAtIndexDivisibleBy3(arr, startIndex, endIndex));

}

}

OUTPUT

1.1, problems @Javadoc ® Declaration 貝Console X <terminated> RecursionDemo (1) [Java Application] C:\Program FilesVava\jre1.8

Add a comment
Know the answer?
Add Answer to:
Assignment #9 will be the construction of a program that reads in a sequence of integers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Arizona State University - CSE205 Assignment #9 Due Date Friday, March April 3rd, 5:30pm Important: This...

    Arizona State University - CSE205 Assignment #9 Due Date Friday, March April 3rd, 5:30pm Important: This is an individual assignment. Please do not collaborate. No late assignment will be accepted. Make sure that you write every line of your code. Using code written by someone else will be considered a violation of the academic integrity and will result in a report to the Dean's office. It must be submitted on-line (Course website). Go to "GradeScope" tab on Canvas -> CSE205...

  • Create a class called Reverse that reverses an array of integers. The class should have two...

    Create a class called Reverse that reverses an array of integers. The class should have two method: - public static void reverse(int [] array) – Which calls the private recursive method, passing the array parameter and which two array elements should be swapped. - private static void reverseRecursive(int [] array, int startIndex, int endIndex) – Which swaps the two elements of the array parameter pointed to by the startIndex and endIndex parameters. The method should be called again, this time,...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • Write a program named Remainder.java then implement the following method: public static int divisibleBy(int[] arr, int...

    Write a program named Remainder.java then implement the following method: public static int divisibleBy(int[] arr, int M, int K) this method determines the number of elements in the int array (arr) that are divisible by M but not K. Then write code inside main method to test your method: your main method accepts three numbers from the command argument list, N, M, K, use the first number to create int array with size of N, assign random number between 0...

  • What this Assignment Is About: Review on Java I topics, such as primitive data types, basic...

    What this Assignment Is About: Review on Java I topics, such as primitive data types, basic I/O, conditional and logical expressions, etc. Review on Java loops. Documentation Requirements to get full credits in Documentation The assignment number, your name, StudentID, 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 a "main" method) to explain code that are...

  • The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13,...

    The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … The next number is found by adding up the two numbers before it. For example, the 2 is found by adding the two numbers before it (1+1). The 3 is found by adding the two numbers before it (1+2). The 5 is found by adding the two numbers before it (2+3), and so on! Each number in the sequence is called...

  • this program is in C. Write a program that computes the number of elements in an...

    this program is in C. Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2,2,4}). Sample execution of the program for this array...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT