Home > database >  Getting 0.0 when printing the return value of a method
Getting 0.0 when printing the return value of a method

Time:05-30

class input{
    private double num1;
    private double num2;
    
    public void setNum1(double num1) {
        this.num1 = num1;
    }

    public double getNum1() {
        return this.num1;
    }
    
    public void setNum2(double num2) {
        this.num2 = num2;
    }

    public double getNum2() {
        return this.num2;
    }
}

class calculator extends input{
    double a = getNum1();
    double b = getNum2();
    
    double add() {
        return a b;
    }
}

class advcalc extends calculator{
    double less() {
        return a-b;
    }
}

public class Inheritence {
    public static void main(String[] args) {
    advcalc cal = new advcalc();
    cal.setNum1(10);
    cal.setNum2(10);
    
    System.out.println(cal.add());
    }
}

Here I'm practicing inheritance. The first class gets inputs, second class is used for addition and 3rd is used for subtraction. I have created an object of 3rd class. After passing the inputs by setNum1() && setNum1(), when I try to print the return value of the add, it prints 0.0. I couldn't figure out what's the issue. Someone help me out please.

CodePudding user response:

a = getNum() is being executed before assigning the value.

double a = getNum1();
double b = getNum2();
double add() {
  return a b;
}

instead of this you can assign the values inside add method.This way it will be assigned after you create an instance.

double a, b = 0;
double add() {
    this.a = getNum1();
    this.b = getNum2();
    return a   b;
}

There are various ways to achieve this . You can also pass the values in add method or create a parameterized constructor.

CodePudding user response:

In your code the variables a and b, defined in calculator will always be 0.0, because they are set during the initialization of you object and only once:

  1. You call new advcalc()
  2. During initialization double a = getNum1(); and double b = getNum2(); are called
  3. You call setNum1 and change the value of num1 in input, but a stays unchanged
  4. You call add and a and b are both 0.0

If you drop the variables a and b and call getNum1 and getNum2 in add it should work. You could also override setNum1 and setNum2 in calculator and set a and b there.

CodePudding user response:

I'm practicing inheritance.

Then are you aware that a and b (in class calculator) are member variables1 and that they are hiding2 member variables num1 and num2 in the parent class (i.e. class input). Hiding can lead to problems (as you have discovered). Hence you should make num1 and num2 protected3 and remove a and b.

Consider the following:
(Note that I changed the class names according to Java naming conventions.)

public class Inheritance {

    public static void main(String[] args) {
        AdvCalc cal = new AdvCalc();
        cal.setNum1(10);
        cal.setNum2(10);
        System.out.println(cal.add());
    }
}

class Input {
    protected double num1;
    protected double num2;

    public void setNum1(double num1) {
        this.num1 = num1;
    }

    public double getNum1() {
        return this.num1;
    }

    public void setNum2(double num2) {
        this.num2 = num2;
    }

    public double getNum2() {
        return this.num2;
    }
}

class Calculator extends Input {
    public double add() {
        return num1   num2;
    }
}

class AdvCalc extends Calculator {
    public double less() {
        return num1 - num2;
    }
}

1 Refer to Declaring Member Variables (in Oracle's Java tutorials).
2 Refer to Variable Shadowing and Hiding in Java
3 Refer to Controlling Access to Members of a Class

  •  Tags:  
  • java
  • Related