Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Assignment This program is meant to use a GUI to actually control our memory calculator. To do this, you will need to create an

Java Assignment

This program is meant to use a GUI to actually control our memory calculator. To do this, you will need to create an instance of MemoryCalc in the CalcGUI and write event Handlers to pass information back and forth between the calculator and the GUI. There are many possible ways to achieve this, but the easiest is probably to develop four different event Handlers: DigitHandler - This event Handler is added to the 0-9 and . (dot) keys. If the equals button was just pressed, this event Handler will overwrite the total shown in the text field with the value of the key (its label) that was just pressed. If the equals button was not just pressed, this event Handler will append the current key's label onto the end of the string shown in the text field.

OperatorHandler This event Handler is added to the +, -, *, and / keys. It sets the value of a class field to the operator that the user has chosen.

ClearHandler This event Handler is added to the C key. It calls the calculator's clear method and sets the value of the text field back to 0.0.

EqualsHandler This event Handler is added to the = key. It reads the current value from the text field, converts it to a double, and calls the appropriate calculator method based on the current operator, passing in the double value. The calculator will compute the answer, and then this event Handler will update the value in the text field to the calculator's current total.

That is all that required for the homework assignment, however I also encourage you to play around with the different types of calculators we have written in this course, perhaps after this course ends. For instance, you could add functionality for geometric operations like sine and cosine. Or you could create a Super Calculator that has different views for scalar, vector, and matrix calculations. Whatever you come up with, I suggest you clean it up and document it. Potential employers always look favorably on being able to review code you have written and ask you questions about it.

Hints: You can use the Action Event's getSource() method to get a reference to the button that was pressed, and you can use Button's getText() method to find out the label of that button.

When the EqualsHandler is converting the text shown in the text field into a double value in order to pass it to the calculator, be careful to handle the case where the text field contains an invalid value, such as 6..72. In this case you should catch the exception, display an error message to the user, and abort the call to the calculator (just restore the current total to the text field).

The user should only be able to enter numbers between when an operator has been selected and when the equals button has been pushed. The DigitHandler should ignore any button presses when the GUI is not in this state. You will be graded according to the following rubric: ?Something happens when the user clicks on any of the keys of the calculator JavaFX GUI ?The user cannot enter a value without first choosing an operator. ?The user is able to enter numbers correctly (i.e. the previous total is overwritten when the user starts a new number, and new digits are appended to the new number). ?When the equals button is pressed, the appropriate calculator method is called based on the operator the user has selected. ?When the equals button is pressed, the appropriate operand passed to the calculator method as an argument. ?When the equals button is pressed, the value in the text fieldis updated to the new total: ?The clear button calls the calculator's clear method and updates the text field to 0.0. ?If the user enters an invalid value, an error message is displayed.

Here is my code with the two classes.

Memory

import java.util.Scanner;

public class Memory {

public static void main(String[] args) // Main function

{

Calculator c = new Calculator(0);

Scanner choose = new Scanner(System.in);

int choice = 0;

String prompt;

boolean right = false;

double operand2;

do // Loop until user wants to quit

{

if(c.getCurrentValue() > 0)

System.out.println("The current value is " + c.getCurrentValue()); //Displaying current value

choice = c.displayMenu(choose); //Displaying the user's menu

if ((choice <= 6) && (choice > 0)) //Checking for a valid option to be selected

{

switch(choice) //Calling the appropriate function

{

case 1: operand2 = c.getOperand("What is the second number? ");

c.add(operand2);

break;

case 2: operand2 = c.getOperand("What is the second number? ");

c.subtract(operand2);

break;

case 3: operand2 = c.getOperand("What is the second number? ");

c.multiply(operand2);

break;

case 4: operand2 = c.getOperand("What is the second number? ");

c.divide(operand2);

break;

case 5:

c.currentValue = 0;

System.out.println("The current value is 0.0");

break;

case 6: c.currentValue = 0;

System.out.println("Goodbye!");

break;

}

}

else

{

right = true;

System.out.println("I'm sorry, " + choice + " wasn't one of the options. Please try again.");

}

}while (choice != 6);

}

}

Calculator

import java.util.Scanner;

public class Calculator {

public static double currentValue=0; //Variable to hold result

public Calculator(double currentValue)

{

this.currentValue = currentValue;

}

public static int displayMenu(Scanner choose) // Displaying the user's menu

{

int choice;

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 = choose.nextInt();

return choice;

}

public static double getOperand(String prompt)

{

Scanner choose = new Scanner(System.in);

System.out.print(prompt);

double option = choose.nextDouble();

return option;

}

public double getCurrentValue()

{

return this.currentValue;

}

public void add(double operand2)

{

this.currentValue += operand2;

}

public void subtract(double operand2)

{

this.currentValue -= operand2;

}

public void multiply(double operand2)

{

this.currentValue *= operand2;

}

public void divide(double operand2)

{

if (operand2 == 0)

{

System.out.println("The current value is NaN. ");

clear();

}

else

{

this.currentValue /= operand2;

}

}

public void clear()

{

this.currentValue = 0;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

Students also viewed these Databases questions

Question

A sequence has the discrete-time Fourier transform

Answered: 1 week ago