Reputation: 29
Here's the superclass:
public class MemoryCalc {
private double currentValue;
public double getCurrentValue() {
return currentValue;
}
public void setCurrentValue(double currentValue) {
this.currentValue = currentValue;
}
public int displayMenu() {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
int choice = -1;
while (choice < 1 || choice > 6) {
System.out.println();
System.out.println("Menu");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Clear");
System.out.println("6. Quit");
System.out.println();
System.out.print("What would you like to do? ");
choice = input.nextInt();
if (choice < 1 || choice > 6) {
System.out.println(choice + " wasn't one of the options");
}
if (choice == 6) {
System.out.println("Goodbye!");
System.exit(0);
}
}
return choice;
}
public double getOperand(String prompt) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print(prompt);
return input.nextDouble();
}
public void add(double op2) {
currentValue += op2;
}
public void subtract(double op2) {
currentValue -= op2;
}
public void multiply(double op2) {
currentValue *= op2;
}
public void divide(double op2) {
if (op2 == 0) {
currentValue = Double.NaN;
} else {
currentValue /= op2;
}
}
public void clear() {
currentValue = 0;
}
}
Here's the Subclass:
class SciMemCalc extends MemoryCalc{
private double currentValue;
public int displayMenu(){
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
int choice = -1;
while (choice < 1|| choice > 8){
System.out.println();
System.out.println("Menu:");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Power");
System.out.println("6. Logarithm");
System.out.println("7. Clear");
System.out.println("8. Quit");
System.out.println();
System.out.println("What would you like to do?");
choice = input.nextInt();
if (choice < 1|| choice > 8){
System.out.println(choice +" wasn't one of the options");
}
if (choice == 8){
System.out.println("Thank you, good bye!");
System.exit(0);
}
}
return choice;
}
public void power(double op2){
Math.pow(currentValue, op2);
}
public void log() {
Math.log(currentValue);
}
}
Here's the Driver:
public class SciCalcDriver {
public static void main(String[] args) {
SciMemCalc calc = new SciMemCalc();
while (true){
System.out.println("The current value is " + calc.getCurrentValue());
int choice = calc.displayMenu();
double second = 0;
if (choice < 6) {
second = calc.getOperand("What is the second number? ");
}
if (choice == 1) {
calc.add(second);
} else if (choice == 2){
calc.subtract(second);
} else if (choice == 3){
calc.multiply(second);
} else if (choice == 4){
calc.divide(second);
} else if (choice == 5){
calc.power(second);
} else if (choice == 6){
calc.log();
} else if (choice == 7){
calc.clear();
}
}
}
}
Now the calculator can add, subtract, multiply, and divide fine but when I use the power or log method nothing happens. I've tried using the debugger and it says it get's all the necessary inputs, but the currentValue doesn't seem to change. I don't think I need to make any changes to the superclass. Advice?
Upvotes: 1
Views: 739
Reputation: 1
You have declared the currentValue as a private member of class so it cannot be accessed by other
classes. when you make it protected or public then it will work. And will solve you problem.
just change that currentValue field in super class.
protected double currentValue // it will be only accessed by its child class
public double currentValue // it will be accessed by all classes in this package
Upvotes: 0
Reputation: 97140
You're shadowing your superclass' currentValue
by re-declaring it in your subclass. To resolve the issue, in your subclass SciMemCalc
, get rid of the line declaring currentValue
:
private double currentValue;
And change all of SciMemCalc
's accesses to currentValue
to use the superclass' accessor (getter) and mutator (setter) methods:
public void power(double op2){
setCurrentValue(Math.pow(getCurrentValue(), op2));
}
public void log() {
setCurrentValue(Math.log(getCurrentValue()));
}
Alternatively, you could declare currentValue
as protected
in your superclass which would allow you to access it directly in your subclass. However, also in this case, you have to make sure that you assign the result of your calculation back to currentValue
:
public void power(double op2){
currentValue = Math.pow(currentValue, op2);
}
public void log() {
currentValue = Math.log(currentValue);
}
Upvotes: 0
Reputation: 5046
Remove private double currentValue;
from the subclass and change private double currentValue;
in the superclass to protected double currentValue;
. If you have a private value, it can't be accessed by the superclass, and the duplicate value you have created just hides the original. Protected solves this.
Upvotes: 0
Reputation: 37845
class MemoryCalc {
private double currentValue;
}
class SciMemCalc extends MemoryCalc {
private double currentValue;
}
What is happening here is there are actually two variables declared with the same name. SciMemCalc
does not have access to the variable declared in MemoryCalc
because it is private.
Instead you would normally make currentValue
protected or interact with it through setters and getters.
class MemoryCalc {
protected double currentValue;
}
class SciMemCalc extends MemoryCalc {
// SciMemCalc has access to currentValue
}
Upvotes: 3