-2

I have simple usecase but seems tricky to me atleast.

I have class

   public class Operation {
    private int a,b;
    public Operation(){

    }
    public Operation(int a,int b){

        this.a=a;
        this.b=b;       
    }
    public int calculate()
    {
        return (a+b);
    }

}

and four classes that extends Operation

public class Addition extends Operation {
    public Addition(){
        super();
    }
    public Addition(int a,int b){
        super(a,b);     
    }
    public int calculate(int a,int b)
    {

                return (a+b);
    }

}
public class Subtraction extends Operation {
    public Subtraction()
    {

    }
    public Subtraction(int a,int b)
    {
        super(a,b); 
    }
    public int calculate(int a,int b)
    {
        return (a-b);
    }

}
public class Multiplication extends Operation {
    public Multiplication()
    {

    }
    public Multiplication(int a,int b)
    {
        super(a,b); 
    }
    public int calculate(int a,int b)
    {
        return (a*b);
    }

}
public class Division extends Operation {
    public Division()
    {

    }
    public Division(int a,int b)
    {
        super(a,b); 
    }
    public int calculate(int a,int b)
    {
        return (a/b);
    }


}

and my Main method has to do follow

public class CalculationTest {

    public static void main(String[] args) {
        Operation op=new Addition(100,200);
        System.out.println(op.calculate());
        op=new Subtraction();
        System.out.println(op.calculate()); //this should print -100 as output
        op=new Multiplication();
        System.out.println(op.calculate()); //this should print 2000 as output
        op=new Division();
        System.out.println(op.calculate()); //this should print 0 as output


    }

}

I have to get output 300 -100 2000 0 but I am getting output as 300 0 0 0

Thanks in advance

user09
  • 920
  • 2
  • 12
  • 38
  • 2
    Have you debugged your code? Have you stepped through it with pen and paper? Do you know what a paramerless constructor is? Do you know what default field values are? – Sotirios Delimanolis Jan 10 '16 at 16:34

1 Answers1

1

I have to get output 300 -100 2000 0 but I am getting output as 300 0 0 0

That is because you are not giving them any input.

Operation op=new Addition(100,200);

This has input and it works.

 op=new Subtraction();

This has no input so the values default to 0 and the result is 0.

If you want to subtract two values, you still have to provide them, try

 op = new Subtraction(100, 200);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I am very new to Java and this is my first post in Stackoverflow. Sorry for that. My usecase doesn't not provide any parameters for Subtraction, Multiplication and Division classes when instantiating them. The values has to come from the first initialization. That is what I am confused about. Again sorry if I am doing terrible wrongs – user09 Jan 10 '16 at 16:40
  • @arjun I suggest you check whether this is the real requirement for you homework because do what you suggest doesn't make any sense. Much more likely you would do the code very differently. Are you sure the calculate shouldn't be taking the values? – Peter Lawrey Jan 10 '16 at 16:41
  • Yes as per the requirement only Addition constructor accepts values and none of the Subtraction, Multiplication and Division constructors accepts values. And even calculate method doesn't accept parameters. The values that are assigned to Addition constructor are has to come/apply to remaining classes. – user09 Jan 10 '16 at 16:56
  • You can do that, but it would be very bad programming practice. You could make your fields `private static int a, b` but like I said, DON'T ever do this in a real program. – Peter Lawrey Jan 10 '16 at 16:59
  • Thanks alot @Peter Lawrey no I got it. One more small question. How to override the calculate method from Operation to remaining classes. Something is wrong in my code. It is not overriding instead it is taking only Operation's calculation method and providing same output 300 for all the methods – user09 Jan 10 '16 at 17:05
  • @arjun You said " And even calculate method doesn't accept parameters." and yet you have written methods which takes parameters. You have to decide which one it is. – Peter Lawrey Jan 10 '16 at 17:19