0

can you tell me how this line works.... my OperatorFactory.get("add") is not doing anything. I'm not getting anything printed

ArithmeticOperator add = OperatorFactory.get ("add");

when I have the following:

interface ArithmeticOperator {
 // Returns the result of applying the operator to operands a and b.
 double operate (double a, double b);
 // Return a String that is the name of this operator.
 String printName ();
}
public class OperatorFactory implements ArithmeticOperator {
 public OperatorFactory(){
 }
 public static ArithmeticOperator get(String name){
 if(name.equals("add"))
 return new PlusOperator();
 else if(name.equals("sub"))
 return new SubOperator();
 else if(name.equals("mult"))
 return new MultOperator();
 else if(name.equals("div"))
 return new DivOperator();
 else
 return null;
 }
 public double operate(double a, double b) {
 throw new UnsupportedOperationException("Not supported yet.");
 }
 public String printName() {
 throw new UnsupportedOperationException("Not supported yet.");
 }
}

public class PlusOperator extends OperatorFactory {
 public double operate(double a, double b) {
 return a + b;
 }
 public String printName() {
 return "Add";
 }
}
public class PlusOperator extends OperatorFactory {
 public double operate(double a, double b) {
 return a + b;
 }
 public String printName() {
 return "Add";
 }
}
Chinmay Kanchi
66.6k24 gold badges92 silver badges116 bronze badges
asked Mar 11, 2010 at 14:54
6
  • why should u get something printed? You are just building it, not calling its printName() method.. Commented Mar 11, 2010 at 14:58
  • "I am not getting anything printed". Well, the code doesn't do that anywhere. Where/how are you printing something then? Commented Mar 11, 2010 at 14:58
  • "I'm not getting anything printed" - Where and what are you trying to print? I don't see it in your code here Commented Mar 11, 2010 at 14:59
  • "I am not getting anything printed" - What they said Commented Mar 11, 2010 at 15:00
  • we really need "New Comment Posted" notifications :) Commented Mar 11, 2010 at 15:00

5 Answers 5

3

You never call add.printName(), and you certainly don't output anything, so I'm not surprised that nothing is being printed.

answered Mar 11, 2010 at 14:58
Sign up to request clarification or add additional context in comments.

Comments

2

Have you actually tried printing the name?

ArithmeticOperator add = OperatorFactory.get ("add");
System.out.println(add.printName());

Also, the PlusOperator should implement ArithmeticOperator directly. The Factory should not implement ArithmeticOperator. This allows you to remove the operate and printName methods from the factory class.

answered Mar 11, 2010 at 15:00

Comments

0

Doesn't look like your get() method calls printName(), so it shouldn't print anything.

answered Mar 11, 2010 at 15:00

Comments

0

You should call add.printName() if you would like to output the operator's name.

answered Mar 11, 2010 at 15:01

Comments

0

I won't repeat what everyone else already said - instead, here's a couple things to try in the future if you have this problem.

A good first sanity check for this problem is to run your code in a code coverage tool (I use EclEmma in Eclipse - I'm sure there's other good ones out there). This would show you that nothing is getting printed because the printName() method isn't being called.

Another way to debug this in Eclipse is by right clicking on your source, choosing "References> Project." This will show you from where the printName() method is being called, and you will see that it isn't being called because it isn't referenced from anywhere.

answered Mar 11, 2010 at 15:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.