0

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:

  1. Create a method called testParser().
  2. Tests that x("12 + 5") returns a Double with the value 17.
  3. Tests that x("12 x 5") returns a Double with the value 60.
  4. 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.

Taylor
  • 39
  • 1
  • 8
  • 3
    Well you can effectively specify which method to use by knowing about overload resolution. If you pass in an argument which is a `String`, it'll use the overload accepting a string, etc. As for "why are they using a String data type to add and multiply numbers?" - surely that's a question you should ask whoever's assigning the question, not us. – Jon Skeet Nov 09 '16 at 18:52
  • It seems you are starting out both in java and on this website, you should have a look at other questions like yours, here is one that will probably interest you : http://stackoverflow.com/questions/21058166/is-it-possible-multiple-methods-with-the-same-name-but-different-parameters-in-a – Raphaël Nov 09 '16 at 19:09

2 Answers2

1

What you are dealing with is method overloading. You have 3 methods with the same name, however they have different method signatures. To call a specific method, you just pass in the appropriate parameters. In this case you do:

 Calculator c = new Calculator();
 String string = "b";
 Double doubleObject = 1;
 double doublePrimitive = 2;

c.x(string);
c.x(doubleObject);
c.x(doublePrimitive);

Java will call the right method based on the parameters that are passed in.

SusanW
  • 1,550
  • 1
  • 12
  • 22
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • This isn't so much polymorphism (to be one of many types), but is better described as operating overloading. – Obicere Nov 09 '16 at 19:08
  • @Obicere I made the change. Thx. – BlackHatSamurai Nov 09 '16 at 19:09
  • So if for example I call "c.x(doubleObject)", this would then return the adding method? – Taylor Nov 09 '16 at 20:36
  • @Taylor Yes, it would call the method with the signature `public Double x(Double x)`. This is because `Double` is an object data type and `double` is a primitive data type. They are different, so they will call different methods, if they methods exist. I hope this makes sense. – BlackHatSamurai Nov 09 '16 at 21:16
0
 Calculator c = new Calculator();
 String p1 = "a";
 Double p2 = 1;
 double p3 = 2;

c.x(p1);
c.x(p2);
c.x(p3);

All 3 call will invoke different methods.

talex
  • 17,973
  • 3
  • 29
  • 66