Question

In this assignment, you will write a subclass of the MemoryCalc class from 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.

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 < 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) {

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 < 5) {

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.

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Main.java (ScientificCalcDriver.java):

public class Main
{
//main function
public static void main(String[] args)
{
//create the object for ScientificMemCalc class
ScientificMemCalc c = new ScientificMemCalc();
while (true)
{
//display the current value
System.out.println("The current value is " + c.getCurrentValue());
//read choice from user
int choice = c.displayMenu();
double operand2 = 0;
if (choice < 6)
{
//get oeprand2 from user
operand2 = c.getOperand("What is the second number? ");
}
if (choice == 1)
{
c.add(operand2);
}
else if (choice == 2)
{
c.subtract(operand2);
}
else if (choice == 3)
{
c.multiply(operand2);
}
else if (choice == 4)
{
c.divide(operand2);
}
else if(choice==5)
{
c.power(operand2);
}
else if(choice==6)
{
c.logarithm();
}
else if (choice == 7)
{
c.clear();
}
}
}
}

MemoryCalc.java:

import java.util.Scanner;
public class MemoryCalc
{
protected double currentValue;
public double getCurrentValue()
{
return currentValue;
}
public void setCurrentValue(double currentValue)
{
this.currentValue = currentValue;
}
public int displayMenu()
{
Scanner choose = 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. Power");
System.out.println("6. Logarithm");
System.out.println("7. Clear");
System.out.println("8. Quit");
System.out.println();
System.out.print("What would you like to do? ");
choice = choose.nextInt();
if (choice < 1 || choice > 8)
{
System.out.println(choice + " wasn't one of the options");
}
if (choice == 8)
{
System.out.println("Goodbye!");
System.exit(0);
}
}
return choice;
}
//method for getOperand
public double getOperand(String prompt)
{
Scanner choose = new Scanner(System.in);
System.out.print(prompt);
return choose.nextDouble();
}
//method for add
public void add(double operand2)
{
currentValue += operand2;
}
//method for subtract
public void subtract(double operand2)
{
currentValue -= operand2;
}
//method for multiply
public void multiply(double operand2)
{
currentValue *= operand2;
}
//method for divide
public void divide(double operand2)
{
if (operand2 == 0)
{
currentValue = Double.NaN;
}
else
{
currentValue /= operand2;
}
}
//method for clear
public void clear()
{
currentValue = 0;
}
}

ScientificMemCalc.java:

import java.util.Scanner;
//class for ScientificMemCalc
public class ScientificMemCalc extends MemoryCalc
{
//method definition for power
public void power(double operand2)
{
this.currentValue=Math.pow(this.getCurrentValue(),operand2);
}
//method definition for logarithm
public void logarithm()
{
this.currentValue= Math.log(this.getCurrentValue());
}
//method for displayMenu
public int displayMenu()
{
return super.displayMenu();
}
}

Sample Output The current value is θ.0 Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Power 6. Logarithm 7. Clear 8. Quit w

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

Add a comment
Know the answer?
Add Answer to:
In this assignment, you will write a subclass of the MemoryCalc class from Sixth Assignment – Mem...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • I have provided a main method. Please add your fraction class. You need constructors, add, subtract,...

    I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your toString method needs to return the numerator followed by / followed by the denominator - NO spaces. DO NOT make any attempt to reduce the fractions (we shall do that later). Please add comments throughout. import java.util.Scanner; public class H4 { public static class Fraction { } public static void main(String[] args) { Fraction f=...

  • With this program you are going to design a math practice program for younger users. You...

    With this program you are going to design a math practice program for younger users. You will ask the user to enter two numbers. Only numbers may be entered. If the user enters a word, the program should disregard the entry and wait for an integer entry. There will not be another prompt if the user enters data other than whole numbers (integers). Here is a sample run: Your static methods should accept two integers and return an integer. Do...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • Java Write a Fraction class that implements these methods:  add ─ This method receives a...

    Java Write a Fraction class that implements these methods:  add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction.  multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction.  divide ─ This method receives a Fraction parameter and divides the parameter fraction by the calling object fraction.  print ─ This method prints the fraction using fraction notation (1/4, 21/14, etc.)...

  • The main method of your calculator program has started to get a little messy. In this...

    The main method of your calculator program has started to get a little messy. In this assignment, you will clean it up some by moving some of your code into new methods. Methods allow you to organize your code, avoid repetition, and make aspects of your code easier to modify. While the calculator program is very simple, this assignment attempts to show you how larger, real world programs are structured. As a new programmer in a job, you will likely...

  • I was wanting to know how would I call my add class through my if statement....

    I was wanting to know how would I call my add class through my if statement. Here is my class: import java.util.Scanner; public class Main {    public static void main(String[] args)    {        int num1 = 0;        int num2 = 0;        int answer = 0;        int userInput = 0;        Scanner sc = new Scanner(System.in);                      System.out.println("Hello, this is a program where you can choose...

  • Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class...

    Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class Delete extends Info { String name; int Id; int del; public Delete() { } public void display() { Scanner key = new Scanner(System.in); System.out.println("Input Customer Name: "); name = key.nextLine(); System.out.println("Enter ID Number: "); Id = key.nextInt(); System.out.println("Would you like to Delete? Enter 1 for yes or 2 for no. "); del = key.nextInt(); if (del == 1) { int flag = 0; //learned...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT