Question
I have supplied the original Memory Calc and ScientificMemCalc below. In this assignment, you will write a subclass of the MemoryCalc class from 06. Sixth
I have supplied the original Memory Calc and ScientificMemCalc below.
In this assignment, you will write a subclass of the MemoryCalc class from 06. Sixth Assignment Memory Calculator. This new calculator, called ScientificMemCalc, should be able to do everything that the MemoryCalc could do, plus raise the current value to a power and compute the natural logarithm of the current value. This is a fairly realistic assignment often when you are working for a company, you will be asked to make minor extensions to existing code. You will need to override the displayMenu method to add the new options. Be sure to only add code to the ScientificMemCalc class if it is necessary leverage the code from the base MemoryCalc class whenever possible. Use the power of inheritance to do this rather than cutting and pasting or otherwise duplicating the code. Finally, write a new class called ScientificCalcDriver that shows the functionality of your new class. This will be very similar to the CalcDriver class contained in the assignment9.jar file.
MEMORYCALC:
import java.util.Scanner;
public class MemoryCalc {
private double currentValue;
public double getCurrentValue() {
return currentValue;
}
public void setCurrentValue(double currentValue) {
this.currentValue = currentValue;
}
public int displayMenu() {
Scanner input = new Scanner(System.in);
int choice = -1;
while (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 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) {
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;
}
}
_____________________________________________________________
CalcDriver
public class CalcDriver {
public static void main(String[] args) {
MemoryCalc calc = new MemoryCalc();
while (true) {
System.out.println("The current value is " + calc.getCurrentValue());
int choice = calc.displayMenu();
double second = 0;
if (choice
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.clear();
}
}
}
}
The output should look somewhat the same except with these changes.
Here is the grading ruberic:
You will be graded according to the following rubric (each item is worth one point):
The ScientificMemCalc class is a subclass of MemoryCalc
? The scientific calculator can do everything that the memory calculator can do
? The scientific calculator can raise the current value to a power and find the natural logarithm of the current value (hint: use Math.pow and Math.log)
? Your driver program allows a user to try all of the functionality in the scientific calculator
? No code from MemCalc is unnecessarily duplicated in ScientificMemCalc
? You have followed the principle of encapsulation. In particular, the currentValue field in the MemCalc class is private
? Your program produces correct output for all inputs
Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Power 6. Logarithm 7. Clear 8. Quit What would you like to do? 4 What is the second number? 6 The current value is 5.0 Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Power 6. Logarithm 7. Clear 8. Quit What would you like to do? 5 What is the second number? 2 The current value is 25.0 Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Power 6. Logarithm 7. Clear 8. Quit What would you like to do? 6 The current value is 3.2188758248682006Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started