Question
Lab2.java contains the following code: public final class Lab2 { /** * This is empty by design, Lab2 cannot be instantiated */ private Lab2() {
Lab2.java contains the following code:
public final class Lab2 { /** * This is empty by design, Lab2 cannot be instantiated */ private Lab2() { // empty by design }
/** * Returns the sum of a consecutive set of numbers from start
to end
. * * @pre start
and end
are small enough to let this * method return an int. This means the return value at most requires 4 bytes and * no overflow would happen. * * @param start is an integer number * @param end is an integer number * @return the sum of start + (start + 1) + .... + end */ public static int sum(int start, int end) {
//Insert your code here and change the return statement to fit your implementation. return 0; }
/** * This method creates a string using the given char * by repeating the character n
times. * * @param first is the given character by which the string is made. * @param n is the number of characters in the final string * @return a string made of the given character. * * @pre n is not negative. */ public static String makeString(char first, int n) { //Insert your code here and change the return statement to fit your implementation. return "";
}
/** * This method gets two strings and interlace a string using the * these two input strings. The number of words in the returned string is as much * as the value of the third input parameter. * * @param first is the string that is used in the even position of the returned result [ asuming * that the position starts from zero] * @param second is the string that is used in the odd position of the returned result * @param n is the number of words in the returned result. * @return returns a string made of the first and the second input parameter. */ public static String interlace(String first, String second, int n) {
//Insert your code here and change the return statement to fit your implementation. return "";
}
/** * This method returns a substring of the given input string that is enclosed in two * given characters. * @param str is a string that contains at least two characters including open
and close
* @param open is a character at the beginning of the string that should be returned. * @param close is a character at the end of the string that should be returned. * @return returns a string enclosed in two given characters of open
and close
. * @pre The given str contains only one open
and one close
character. */ public static String getSubstring(String str, char open, char close) { //Insert your code here and change the return statement to fit your implementation. return "";
} /** * This method converts a decimal value into its binary representation. * @param value is a positive integer number * @return the binary representation of the input. * @pre the input is a positive integer number. */ public static String decimalToBinary(int value) { //Insert your code here and change the return statement to fit your implementation. return ""; } }
Lab2Tester.java contains the following code:
public class Lab2Tester {
// sum @Test public void testSum1() { int start = 0; int end = 5; int sum= 0; for (int i = start ; i
@Test public void testMakeString1() { char init = '*'; int n = 5; String result = ""; for (int i = 0 ; i
@Test public void testInterlace6() {
String str1 = "Hello "; String str2 = "World "; String result = "Hello World Hello World Hello "; assertEquals("Failed: interlace(\"*\",\"-\",5)", result, Lab2.interlace("Hello ","World ",5)); }
@Test public void testGetSubstring1() {
String str1 = "x + y + z - ( y * z) / 3 * n "; String result = " y * z"; char open = '('; char close = ')'; assertEquals("Failed: getSubstring(\"x + y + z - ( y * z) / 3 * n \", \"(\", \")\")", result, Lab2.getSubstring("x + y + z - ( y * z) / 3 * n ", '(', ')')); }
@Test public void testDecimalToBinary4() { int decimal = 23; String binary = "10111"; assertEquals("Failed: decimalToBinary(23)", binary, Lab2.decimalToBinary(23)); }
}
1. Setup Please download Lab2.java that is attached to this description. Open eclipse. Click on File and select Import. Choose Existing Projects into Workspace and click Next. Click on Select Archive File and then Browse. Find Lab2.zip and click Finish. You should see two files, one is called Lab2.java and one Lab2Tester.java. 2. Important Notes: You are not allowed to use any regular expressions, loops, or any methods such as "contains", "replace" and so on to solve the problem. In other words, the problem should purely be solved by recursion. To practice testing, we only provided a set of incomplete test cases. You should make sure that you add enough test cases to the tester that tests your code thoroughly. Please have a look at the tester code Lab2Tester, and add more test cases to test your code thoroughly. To do this, you can copy one of the methods, change the name of the method to avoid having a duplicate method's name and change the body of the method to test your code with your selected input. 3. JavaDoc generation The javaDoc has been written for you. All you need to do is to generate it as an HTML file to make it easier for navigation. For this, right click on Lab2.java -> select export -> javaDoc -> Next. It will ask you for the location in which you want to store the documentation. Enter the path and then click on Finish. 6. Programming Task 3 For this task, you should implement a recursive function that gets 3 parameters and return a string that is made of the first two input parameters repeatedly. This means the output contains the first input followed by the second input, followed by the first input again, ..The number of repetition of the input strings is specified by the third argument of the method. Here are some examples: .. interlace ("Hello","World", 0) returns interlace ("Hello","World ", 1) returns "Hello" interlace ("Hello","World ", 2) returns "Hello World" interlace("Hello "World 3) returns "Hello World HelloStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started