user3795283
user3795283

Reputation: 27

Field value of superclass is showing up as zero when referencing it from a subclass

I am currently having an issue with obtaining a value in the superclass from the subclass. In the superclass, if choice == 5, it calls the power method in the subclass. The problem is that it prints out 0.0 when it calls getCurrentValue() from the power method in the subclass even when the field currentValue has a value other than 0. I'm wondering if I messed something up when I extended the superclass. When I'm calling getCurrentValue() from the power method in the subclass, could I be calling a different currentValue field, perhaps one that's value hasn't changed?

Here is the code for my superclass:

import java.util.Scanner;


public class TrippelTylerMemoryCalculator {

     //Fields
     private double currentValue;

     //Methods
     public static int displayMenu(){
         Scanner input = new Scanner(System.in);
         int choice;

     System.out.print("\nMenu \n" +
            "1. Add \n" +
            "2. Subtract \n" +
            "3. Multiply \n" +
            "4. Divide \n" +
            "5. Power \n" + 
            "6. Logarithm \n" +
            "7. Clear \n" +
            "8. Quit \n\n" +
            "What would you like to do? \n");

    choice = input.nextInt();
    if(choice<1 || choice>6){
        while(choice<1 || choice>6){
            System.out.println("You have entered an invalid menu option. \n" +
                "What would you like to do? \n");
            choice = input.nextInt();
        }
        return choice;
    }
    else{
        return choice;
    }
    }

public static double getOperand(String prompt){
    Scanner input = new Scanner(System.in);
    double operand;
    System.out.println(prompt);
    operand = input.nextDouble();
    return operand;
}

public double getCurrentValue(){
    return currentValue;
}

public void add(double operand2){
    double operand = operand2;
    double answer;
    answer = currentValue + operand;
    currentValue = answer;
}

public void subtract(double operand2){
    double operand = operand2;
    double answer;
    answer = currentValue - operand;
    currentValue = answer;
}

public void multiply(double operand2){
    double operand = operand2;
    double answer;
    answer = currentValue * operand;
    currentValue = answer;
}

public void divide(double operand2){
    double operand = operand2;
    double answer;
    if(operand == 0){
        currentValue = Double.NaN;
    }
    else{
        answer = currentValue / operand;
        currentValue = answer;
    }
}

public void clear(){
    currentValue = 0;
}

//Main Method
public static void main(String[] args) {
    TrippelTylerMemoryCalculator instance = new TrippelTylerMemoryCalculator();
    TrippelTylerScientificMemoryCalculator instance2 = new TrippelTylerScientificMemoryCalculator();

    //Fields for main method
    double operand;
    boolean repeat = true;

    //Program starts here
    while(repeat){
        System.out.print("The current value is: " + instance.getCurrentValue() + "\n");
        int choice;
        choice = displayMenu();
        if(choice == 1){
            operand = getOperand("What is the second value?");
            instance.add(operand);
        }
        else if(choice == 2){
            operand = getOperand("What is the second value?");
            instance.subtract(operand);
        }
        else if(choice == 3){
            operand = getOperand("What is the second value?");
            instance.multiply(operand);
        }
        else if(choice == 4){
            operand = getOperand("What is the second value?");
            instance.divide(operand);
        }
        else if(choice == 5){
            operand = getOperand("What is the second value?");
            instance2.power(operand);
        }
        else if(choice == 6){

        }
        else if(choice == 7){
            instance.clear();
        }
        else if(choice == 8){
            System.out.println("Goodbye!");
            System.exit(0);
        }

    }
}

}

And here is the code for my subclass:

 public class TrippelTylerScientificMemoryCalculator extends TrippelTylerMemoryCalculator{

    public void power(double operand2){
         double operand = operand2;
         double answer;
         //answer = Math.pow(temp, operand);
         System.out.println(getCurrentValue());
    }
 }

Any input would be greatly appreciated! Thanks everyone!

Upvotes: 1

Views: 156

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500385

You've got two instances, referred to by variables instance and instance2. These are the only lines of your code which use instance2:

TrippelTylerScientificMemoryCalculator instance2 = new 
    TrippelTylerScientificMemoryCalculator();

...

instance2.power(operand);

Where do you expect the value to come from? I suspect you only want one instance, and make that an instance of the subclass. Then all your operations will act on the same object. So just change the instance variable declaration to:

TrippelTylerScientificMemoryCalculator instance = new 
    TrippelTylerScientificMemoryCalculator();

... remove the instance2 declaration entirely, and use instance when you want to call the power method.

(I'd also strongly advise shorter type names, but that's a different matter.)

Additionally, note that unlike all the rest of your operations, your power method doesn't alter the current value (or do anything useful, in fact), but does take responsibility for printing it. That seems odd to me. You should try to be consistent in what your methods do.

Upvotes: 1

Related Questions