-1

I have the following code:

package com.fdm.calculator;

public class Calculator {

    public double evaluate(String expression){
        return Double.parseDouble(expression);

    }
}

How do I invoke this method. For example I want to do calculator.evaluate("5"), which should return 5.0.

I want the output on the screen.

I am also running a j unit test case along side this, so I am not sure how that changes things.

EpicPandaForce
  • 71,034
  • 25
  • 221
  • 371
user3809938
  • 858
  • 2
  • 10
  • 28

6 Answers6

2
Calculator calculator = new Calculator();
System.out.println(calculator.evaluate(5));

This is assuming that you have a default constructor. Also, you could make the method static and just call it like this:

System.out.println(Calculator.evaluate(5));
Matt L
  • 127
  • 9
0

You have to make the method static if you want to use the method as Calculator.evaluate("5"):

public static double evaluate(String expression){
    return Double.parseDouble(expression);
}
jh314
  • 24,533
  • 14
  • 58
  • 79
  • 1
    Because he's wants to call it as `calculator.evaluate()`, not something like `Calculator c = new Calculator(); c.evaluate()` – Kevin L Aug 12 '14 at 14:01
  • From the question, it looks like he wanted to invoke method by doing `Calculator.evaluate()` – jh314 Aug 12 '14 at 14:04
0

You can make it static or would have to create an instance of the Calculator obj (assuming you have a constructor), like so:

Calculator c = new Calculator();
c.evaluate("5")

To print you'd do if its static:

System.out.println(Calculator.evaluate("5"));

For ref: I want to know the difference between static method and non-static method

Community
  • 1
  • 1
olive_tree
  • 1,238
  • 14
  • 22
0

Where you use this Calculator class, you need an instance of it and then call the method on it:

public class MyClass
{
    public void methodExample()
    {
        Calculator calculator = new Calculator();
        double number = calculator.evaluate("5.0");
        System.out.println("" + number);
    }


    public static void main(String[] args)
    {
        MyClass myClass = new MyClass();
        myClass.methodExample();
    }
}
EpicPandaForce
  • 71,034
  • 25
  • 221
  • 371
  • Seems a bit convoluted to create another method just to invoke the `Calculator` class. – Duncan Jones Aug 12 '14 at 14:04
  • I just wanted to make it as non-static as possible because that's a terrible plan for unit testing and I'm not sure why others are recommending it. – EpicPandaForce Aug 12 '14 at 14:04
  • @Duncan I just figured maybe he would want to run it from elsewhere and not necessarily the main... not sure, it seemed like a more reasonable example to me :D – EpicPandaForce Aug 12 '14 at 14:08
  • 1
    Presumably you would unit test `Calculator`, not `MyClass`. Hence you can do without the `methodExample()` method and call things directly from `main()`. – Duncan Jones Aug 12 '14 at 14:08
0
public class Calculator {

    public static void main(String[] args){
        Calculator c = new Calculator();
        System.out.println(c.evaluate("5"));
    }

    public double evaluate(String expression){
        return Double.parseDouble(expression);
    }

}
nereide
  • 152
  • 8
0

Use the following UseCalculator.java to invoke the calculater:

package com.fdm.calculator;

public class UseCalculator {

public static void main(String[] args) {

    Calculator ObjCalculator = new Calculator();
    double resultValue= ObjCalculator.evaluate("5");
    System.out.println("Value from Calculator is:"+resultValue);

}
}
Output: Value from Calculator is:5.0