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 <= end ; i++) sum += i; assertEquals("Failed at sum (" + start + ", " + end + ")", sum, Lab2.sum(start, end)); } @Test public void testMakeString1() { char init = '*'; int n = 5; String result = ""; for (int i = 0 ; i < n ; i++) result += init; assertEquals("Failed at makeString(" + init + ", " + n + ")", result, Lab2.makeString(init, n)); } @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)); } }
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 <code> start </code> to <code> end </code>.
*
* @pre <code> start </code> and <code> end </code> 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 <code> n </code> 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 <code> open </code> and <code> close </code>
* @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 <code> open </code> and <code> close </code>.
* @pre The given str contains only one <code> open </code> and one <code> close </code> 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 <= end ; i++)
sum += i;
assertEquals("Failed at sum (" + start + ", " + end + ")", sum, Lab2.sum(start, end));
}
@Test
public void testMakeString1() {
char init = '*';
int n = 5;
String result = "";
for (int i = 0 ; i < n ; i++)
result += init;
assertEquals("Failed at makeString(" + init + ", " + n + ")", result, Lab2.makeString(init, n));
}
@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));
}
}
Step by step
Solved in 3 steps with 1 images