I've been given an assignment that aims to test the validity of a calculator. They have given us a default class to go by which looks like this:
public class Calculator {
Double x;
/*
* Chops up input on ' ' then decides whether to add or multiply.
* If the string does not contain a valid format returns null.
*/
public Double x(String x){
return new Double(0);
}
/*
* Adds the parameter x to the instance variable x and returns the answer as a Double.
*/
public Double x(Double x){
System.out.println("== Adding ==");
return new Double(0);
}
/*
* Multiplies the parameter x by instance variable x and return the value as a Double.
*/
public Double x(double x){
System.out.println("== Multiplying ==");
return new Double(0);
}
}
You are suppose to expand upon this class that aims to be a test harness. The following instructions to do so are as follows:
- Create a method called testParser().
- Tests that x("12 + 5") returns a Double with the value 17.
- Tests that x("12 x 5") returns a Double with the value 60.
- Tests that x("12 [ 3") returns null because [ is not a valid operator.
Here are the current changes I have made:
public class TestCalculator {
Double x;
/*
* Chops up input on ' ' then decides whether to add or multiply.
* If the string does not contain a valid format returns null.
*/
public Double x(String x){
return new Double(0);
}
public void testParsing() {
}
/*
* Adds the parameter x to the instance variable x and returns the answer as a Double.
*/
public Double x(Double x){
System.out.println("== Adding ==");
x("12 + 5");
return new Double(0);
}
/*
* Multiplies the parameter x by instance variable x and return the value as a Double.
*/
public Double x(double x){
System.out.println("== Multiplying ==");
x("12 x 5");
return new Double(0);
}
}
What I am mostly confused about is how I am able to call the actual methods as they haven't been given any unique name to call and you cannot change the name of the methods because that changes the data type. Also why are they using a String data type to add and multiply numbers? Any help on how to begin to write the testParsers() method would be very helpful. Thanks.