Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Language... has to be able to pass JUNit test. Problem 1: (70 points) Create a program named RecursiveMethods.java and implement the following methods: public

Java Language... has to be able to pass JUNit test.
Problem 1: (70 points)
Create a program named RecursiveMethods.java and implement the following methods:
public static int smallest(int[] arr) (20 points)
This method finds and returns the smallest value in the int array. (To get credit, it MUST be implemented as a recursive method. No credit if implemented with loop)
public static int smallest(int[][] arr) (20 points)
This method finds and returns the smallest value in the 2D int array. (To get credit, it MUST be implemented as a recursive method. No credit if implemented with loop)
public static String repeat(String s, int n) (30 points)
It accepts a string s and an integer n as parameters and that returns a String consisting of n copies of s. For example:
Call Value Returned
repeat("hello", 3) "hellohellohello"
repeat("this is fun", 1) "this is fun"
repeat("wow", 0) ""
repeat("hi ho! ", 5) "hi ho! hi ho! hi ho! hi ho! hi ho! "
You should solve this problem by concatenating String objects using the + operator. String concatenation is an expensive operation, so it is best to minimize the number of concatenation operations you perform. For example, for the call repeat("foo", 500), it would be inefficient to perform 500 different concatenation operations to obtain the result. Most of the credit (25 points) will be awarded on the correctness of your solution independent of efficiency. The remaining credit (5 points) will be awarded based on your ability to minimize the number of concatenation operations performed.
Your method should throw an IllegalArgumentException if passed any negative value for n. You are not allowed to construct any structured objects other than Strings (no array, List, Scanner, etc.) and you may not use any loops to solve this problem; you must use recursion.
Then use the following code to test your methods (copy/paste to main method)
int[] arr={2,4,3,89,0, -9};
System.out.println(smallest(arr));
int[][] ar={{1,2,3,4,1,0},{0,-8,-90}};
System.out.println(smallest(ar));
System.out.println(repeat(args[0], Integer.parseInt(args[1])));

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

14. Simplify144x2 O A. 2x2 O B. 12x O C. 8/ 6x O D. 8x6

Answered: 1 week ago

Question

How can you defend against SQL injection attacks?

Answered: 1 week ago