Java: Subclass can't get value of field in superclass -
Java: Subclass can't get value of field in superclass -
here's superclass:
public class memorycalc { private double currentvalue; public double getcurrentvalue() { homecoming currentvalue; } public void setcurrentvalue(double currentvalue) { this.currentvalue = currentvalue; } public int displaymenu() { @suppresswarnings("resource") scanner input = new scanner(system.in); int selection = -1; while (choice < 1 || selection > 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 do? "); selection = input.nextint(); if (choice < 1 || selection > 6) { system.out.println(choice + " wasn't 1 of options"); } if (choice == 6) { system.out.println("goodbye!"); system.exit(0); } } homecoming choice; } public double getoperand(string prompt) { @suppresswarnings("resource") scanner input = new scanner(system.in); system.out.print(prompt); homecoming 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 subclass:
class scimemcalc extends memorycalc{ private double currentvalue; public int displaymenu(){ @suppresswarnings("resource") scanner input = new scanner(system.in); int selection = -1; while (choice < 1|| selection > 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 do?"); selection = input.nextint(); if (choice < 1|| selection > 8){ system.out.println(choice +" wasn't 1 of options"); } if (choice == 8){ system.out.println("thank you, bye!"); system.exit(0); } } homecoming choice; } public void power(double op2){ math.pow(currentvalue, op2); } public void log() { math.log(currentvalue); } }
here's driver:
public class scicalcdriver { public static void main(string[] args) { scimemcalc calc = new scimemcalc(); while (true){ system.out.println("the current value " + calc.getcurrentvalue()); int selection = calc.displaymenu(); double sec = 0; if (choice < 6) { sec = calc.getoperand("what sec 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 calculator can add, subtract, multiply, , split fine when utilize powerfulness or log method nil happens. i've tried using debugger , says get's necessary inputs, currentvalue doesn't seem change. don't think need create changes superclass. advice?
class memorycalc { private double currentvalue; } class scimemcalc extends memorycalc { private double currentvalue; }
what happening here there 2 variables declared same name. scimemcalc
not have access variable declared in memorycalc
because private.
instead create currentvalue
protected or interact through setters , getters.
class memorycalc { protected double currentvalue; } class scimemcalc extends memorycalc { // scimemcalc has access currentvalue }
java field subclass calculator superclass
Comments
Post a Comment