Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Assignment Memory Calculator To Text File Output This week we will write modify the memory calculator from 06. Sixth Assignment Memory Calculator to be

Java Assignment

Memory Calculator To Text File Output

This week we will write modify the memory calculator from 06. Sixth Assignment Memory Calculator to be able to save all of the calculations done to a file. There are multiple ways to do this, but I suggest creating 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 the user chooses to add 2, you would create a new String 6 + 2 = 8 and add that String to the ArrayList. You 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. Be sure to handle exceptions gracefully.

Here is my existing code from the original assignment. There are two parts Memory and Calculator. Both parts are still needed in separate java and answer need to display as 3.0 or 2.0, etc.

Memory Java Code

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 Code

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

More Books

Students also viewed these Databases questions

Question

What distinguishes a Cartesian product from any other set?

Answered: 1 week ago

Question

Please answer #1. Thank you!

Answered: 1 week ago

Question

Understand how to design effective service guarantees.

Answered: 1 week ago

Question

Know when firms should not offer service guarantees.

Answered: 1 week ago