Question: Write two recursive methods and name both methods as substringRecursive. Both methods are similar to the substring method in the String class, but they need

Write two recursive methods and name both methods as substringRecursive. Both methods are similar to the substring method in the String class, but they need to be written as recursive methods. One version has two parameters, a string and the beginning index. It returns the substring from the beginning index to the very end. Another version has three parameters, a string, the beginning index, and the ending index. It returns the substring from the beginning index up to the ending index (excluding the ending index). Both methods need to be recursive. You are not allowed to use the substring method in the String class. You also need to write a main method to test the two recursive methods. Your main method should print a menu such as the following: 1. Test two-parameter substringRecursive method 2. Test three-parameter substringRecursive method 3. Exit The menu should be repeated until user selects 3 to exit. For each testing, your program should ask user to enter the input string, the beginning index, and the ending index (for the three- parameter method only), each on a separate line, and then print the output string after calling the corresponding method. Place all three methods in the same class. Name your class as RecursionTest. Place your class in a named package and name your package as assg5_yourPirateId. Part 2 (30 points) You need to write a program to perform JUnit testing on the ComplexNum class printed below. Use JUnit 4 for this assignment. You need to write the TestComplexNum class which includes methods to test all the constructors and methods in your ComplexNum class. Make sure you also test all the different cases for each method. For most of the test methods, you can compare the actual result with the expected result, or to test if a condition is true or false.

You may include the setUp method (i.e., the @Before method) in your TestComplexNum class. The setUp method will be called before EACH test method is executed. Therefore you should only put the necessary code in the setUp method.

ComplexNum:

public class ComplexNum {

double real, imaginary;

public ComplexNum() {

this.real = 0;

this.imaginary = 0;

}

public ComplexNum(double real) {

this.real = real;

this.imaginary = 0;

}

public ComplexNum(double real, double imaginary) {

this.real = real;

this.imaginary = imaginary;

}

public double getReal() {

return real;

}

public void setReal(double real) {

this.real = real;

}

public double getImaginary() {

return imaginary;

}

public void setImaginary(double imaginary) {

this.imaginary = imaginary;

}

public ComplexNum add(ComplexNum a) {

return new ComplexNum(this.real + a.real, this.imaginary + a.imaginary);

}

public ComplexNum sub(ComplexNum a) {

return new ComplexNum(this.real - a.real, this.imaginary - a.imaginary);

}

public ComplexNum mul(ComplexNum a) {

return new ComplexNum(this.real * a.real - this.imaginary * a.imaginary, this.real * a.imaginary + this.imaginary * a.real);

}

public ComplexNum neg() {

return new ComplexNum(-this.real, -this.imaginary);

}

public String toString() {

return real + "+" + imaginary + "i";

}

public boolean equals(Object obj) {

ComplexNum c2 = (ComplexNum) obj;

return this.real == c2.real && this.imaginary == c2.imaginary;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!