Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab2.java includes the following code public final class Lab2 { /** * This is empty by design, Lab2 cannot be instantiated */ private Lab2() {

image text in transcribedimage text in transcribedimage text in transcribed

Lab2.java includes 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 includes the following code:

package Lab2;

import static org.junit.Assert.*;

import org.junit.Test; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING) 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. If you look at the location in which you stored the documentation, you'll see there is a file called index.html. Clicking on this file, shows the documentation of the project in your browser. 4. Programming Task 1 For this task, we are asking you to implement a recursive method that finds the sum of n consecutive integer number, starting from integer start and ends with integer end. For example, if start = 0 and end = 5, we expect the method to return 15 (i.e. 0+1+2+3+4+5). The name of the method is sum and the header of the method was written for you in Lab2.java. 5. Programming Task 2 For this task, you are expected to implement a recursive function that creates a string of length n using the given character. For example, if the given character is *, and n = 5, the output should be ***** Please note that a string with length zero is also possible. To concatenate to a string, the simple '+' operator should be used. The name of the method is makeString and the header of the method was written for you in Lab2.java. 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 Hello" 7. Programming Task 4 In this task, you are required to write a recursion that gets a string and two characters and returns the substring that is enclosed in two given characters. You can assume that the given string includes only one instance of each enclosing characters. For example, where the input string is This is (quite} an example! and the first enclosing character is '[' and the second is '}', it should return quite. The name of the method is getSubstring and the header of the method was written for you in Lab2.java. 8. Programming Task 5 For this task, you need to write a recursion that converts a positive integer to its binary equivalence. This method gets one integer input. A binary form of an integer is calculated by repeatedly dividing the integer number (and later, its quotient) by 2. You keep doing the division until the quotient is zero. The reminders of the divisions, from the last division to the first, form the binary representation of the integer number. Let's see an example in which 23 is converted to a binary number. R stands for the remainder. 23 / 2 = 11 R 1 11 / 2 = 5 R 1 5/2 = 2 R1 2 / 2 = 1 R 0 1/2 = 0 R 1 Therefore, the binary representation of 23 is "10111

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

7 How do I can statements help learners understand what they own?

Answered: 1 week ago