Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Tenth Assignment Memory Calculator To Text File Output This week we will write modify the Memory Calculator to be able to save all of the

Tenth Assignment Memory Calculator To Text File Output

This week we will write modify the 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.

Sample output:

The current value is 0.0

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Clear

6. Save

7. Quit

What would you like to do? 1

What is the second number?

5

The current value is 5.0

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Clear

6. Save

7. Quit

What would you like to do? 2

What is the second number?

3

The current value is 2.0

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Clear

6. Save

7. Quit

What would you like to do? 3

What is the second number?

5

The current value is 10.0

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Clear

6. Save

7. Quit

What would you like to do? 4

What is the second number?

0

The current value is NaN

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Clear

6. Save

7. Quit

What would you like to do? 5

The current value is 0.0

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Clear

6. Save

7. Quit

What would you like to do? 6

The current value is 0.0

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Clear

6. Save

7. Quit

What would you like to do? 7 Goodbye!

The data in the file should look like this:

Initial value is 0

0.0 + 5.0 = 5.0

5.0 - 3.0 = 2.0

2.0 * 5.0 = 10.0

10.0 / 0.0 = NaN

Cleared

You will be graded according to the following rubric:

A save option has been added to the menu: 1 point

The save option calls a method that displays a JFileChooser dialog and allows the user to select a file: 2 points

The save method writes to the selected file: 1 point

The information written to the selected file is correct: 2 points

Exceptions are handled gracefully (i.e. nice error messages are displayed to the user rather than the stack trace): 1 point

Your program compiles: 1point

Your program runs: 1 point

You follow standard coding conventions (e.g. variable names, indentation, comments, etc.): 1 point

Note: If your program does not compile, you will receive a score of 0 on the entire assignment

Note: If you program compiles but does not run, you will receive a score of 0 on the entire assignment

Note: If your Eclipse project is not exported and uploaded to the eLearn drop box correctly, you will receive a score of 0 on the entire assignment

Note: If you do not submit code that solves the problem for this particular assignment, you will not receive any points for the programs compiling, the programs running, or following standard coding conventions.

Here is the code for memory Calculator:

package memoryCalculator;

import java.util.Scanner;

public class MemoryCalculator {

static Scanner keybd = new Scanner(System.in);

private double currentValue = 0;

// methods:

public static int displayMenu() {

int choice;

while(true)

{

System.out.print("Menu 1. Add " + "2. Subtract " + "3. Multiply " + "4. Divide " + "5. Clear "

+ "6. Quit " + "What would you like to do? ");

choice = Integer.parseInt(keybd.nextLine());

if (choice>=1 && choice <=6)

break;

else

System.out.println("Invalid Choice. Retry");

}

return choice;

}

public static double getOperand(String prompt) {

System.out.print(prompt + " ");

return Double.parseDouble(keybd.nextLine());

}

public double getCurrentValue() {

return currentValue;

}

public void add(double operand2) {

currentValue += operand2;

}

public void subtract(double operand2) {

currentValue -= operand2;

}

public void multiply(double operand2) {

currentValue *= operand2;

}

public void divide(double operand2)

{

if (operand2 == 0)

{

currentValue = Double.NaN;

return;

}

currentValue /= operand2;

}

public void clear() {

currentValue = 0;

}

}

///////////////////////DriverClass

///////////////////////////////////////////////

import memoryCalculator;

public class Driver {

public static void main(String[] args)

{

MemoryCalculator c = new MemoryCalculator();

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

int choice = c.displayMenu();

while (choice != 6) {

switch (choice) {

case 1:

c.add(getOperand("What is the second number?"));

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

break;

case 2:

c.subtract(getOperand("What is the second number?"));

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

break;

case 3:

c.multiply(getOperand("What is the second number?"));

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

break;

case 4:

c.divide(getOperand("What is the second number?"));

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

break;

case 5:

c.clear();

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

break;

default:

System.out.println("Invalid Choice!");

break;

}//switch

System.out.println();

choice = displayMenu();

}//while

System.out.println("Good Bye!");

}//main

} // end main

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

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago