Untitled
unknown
plain_text
4 years ago
2.8 kB
15
Indexable
Note:- I have modify the code by declaring class member as private and give access through getter and setter.
Please mark it as brainliest answer:).
/--------------------------Calculator.java---------------------/
/**
* The Class Calculator.
*/
public class Calculator {
   /** The accumulator. */
   private double accumulator;
   /**
   * Instantiates a new calculator.
   */
   public Calculator() {
       this.accumulator = 0.0;
   }
   /**
   * Gets the accumulator.
   *
   * @return the accumulator
   */
   public double getAccumulator() {
       return accumulator;
   }
   /**
   * Sets the accumulator.
   *
   * @param accumulator the new accumulator
   */
   public void setAccumulator(double accumulator) {
       this.accumulator = accumulator;
   }
}
* The Class Calculator. 3 4 5 6 7 public class calculator 1 /** The accumulator. */ private double accumulator; * Instantiate
/-------------------------------------CalculatorWithMemory.java-----------------------/
/**
* The Class CalculatorWithMemory.
*/
public class CalculatorWithMemory extends Calculator {
   /** The memory. */
   private double memory;
   /**
   * Instantiates a new calculator with memory.
   */
   public CalculatorWithMemory() {
       this.memory = 0;
   }
   /**
   * Save.
   */
   public void save() {
       memory = getAccumulator(); // The value of accumulator assigned to memory
   }
   /**
   * Recall.
   */
   public void recall() {
       setAccumulator(memory); // The value of memory again assigned to accumulator
   }
   /**
   * Clear memory.
   */
   public void clearMemory() {
       memory = 0; // Zero value is assigned to the memory
   }
   /**
   * Sets the memory.
   *
   * @param memory the new memory
   */
   public void setMemory(double memory) {
       this.memory = memory;
   }
   /**
   * Gets the memory.
   *
   * @return the memory
   */
   public double getMemory() {
       return memory; // This returns the value stored in the memory
   }
}
* The class calculatorWithMemory. 5 public class calculatorWithMemory extends calculator { ора от л во 18The memory. */ priva
/---------------------------Main.java-----------------------------/
class Main {
   public static void main(String[] args) {
       CalculatorWithMemory cal = new CalculatorWithMemory();
       cal.setMemory(34.0);
      
       cal.recall();
       System.out.println("Accumulator after recall: " + cal.getAccumulator());
       cal.clearMemory();
       cal.recall();
       cal.save();
       System.out.println("Accumulator now: " + cal.getAccumulator());
   }
}
1 sssr class Main public static void main(String[] args) { CalculatorWithMemory cal = new calculatorWithMemory(); cal.setMemo
/-------------------output--------------------/
Editor is loading...