Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Memory Calculator to Text File Output Modify the memory calculator to be able to save all the calculations done to a file. Create an

Java Memory Calculator to Text File Output

Modify the memory calculator to be able to save all the calculations done to a file. Create an ArrayList of STring objects. Each time the user performs an operation (add, subtract, multiply, divide or clear), add a new string. For instance, if the current value is 6 and user chooses to add 2, you would create a new String "6 + 2 = 8" and add that String to the ArrayList. Will also need to add a new Save item to the menu. When the user chooses this option, you should call a method that displays a JFileChooser dialog to the user, allow them to select where to save the data, and then write all of the strings from the ArrayList to that file.

Here is my code.

Memory Java

import java.util.Scanner;

public class Memory {

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

{

MemoryCalculator c = new MemoryCalculator(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: 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);

}

}

MemoryCalculator Code

import java.util.Scanner;

public class MemoryCalculator {

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

public MemoryCalculator(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

Nested Relations And Complex Objects In Databases Lncs 361

Authors: Serge Abiteboul ,Patrick C. Fischer ,Hans-Jorg Schek

1st Edition

3540511717, 978-3540511717

More Books

Students also viewed these Databases questions

Question

Do other psychological syndromes have any place in the courtroom?

Answered: 1 week ago

Question

1. Identify three approaches to culture.

Answered: 1 week ago

Question

2. Define communication.

Answered: 1 week ago

Question

4. Describe how cultural values influence communication.

Answered: 1 week ago