Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Memory Calculator to Text File Output Modify the memory calculator to be ab

ID: 3702687 • Letter: J

Question

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;

    }

}

Explanation / Answer

//Memory.java

package com.src;

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 <= 7) && (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;

case 7:

c.save();

break;

}

}

else

{

right = true;

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

}

} while (choice != 6);

}

}

//MemoryCalculator.java

package com.src;

import java.util.ArrayList;

import java.util.Scanner;

import com.src.FileChooserExample;

import javax.swing.JFileChooser;

public class MemoryCalculator {

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

public static ArrayList<String> operations;

public MemoryCalculator(double currentValue){

this.currentValue = currentValue;

this.operations = new ArrayList<>();

}

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("7. Save");

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

String s = this.currentValue + " + " + operand2 + " = ";

this.currentValue += operand2;

s += this.currentValue;

operations.add(s);

}

public void subtract(double operand2){

String s = this.currentValue + " - " + operand2 + " = ";

this.currentValue -= operand2;

s += this.currentValue;

operations.add(s);

}

public void multiply(double operand2){

String s = this.currentValue + " * " + operand2 + " = ";

this.currentValue *= operand2;

s += this.currentValue;

operations.add(s);

}

public void divide(double operand2){

if (operand2 == 0){

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

clear();

}

else{

String s = this.currentValue + " / " + operand2 + " = ";

this.currentValue /= operand2;

s += this.currentValue;

operations.add(s);

}

}

public void clear(){

this.currentValue = 0;

}

public void save() {

FileChooserExample fc = new FileChooserExample();

fc.openfile();

}

}

//FileChooserExample.java

package com.src;

import javax.swing.*;   

import java.awt.event.*;   

import java.io.*;

import java.util.ArrayList;

import com.src.MemoryCalculator;

public class FileChooserExample extends JFrame implements ActionListener{   

JMenuBar mb;   

JMenu file;   

JMenuItem open;   

JTextArea ta;   

FileChooserExample(){   

open=new JMenuItem("Open File");   

open.addActionListener(this);   

file=new JMenu("File");   

file.add(open);

mb=new JMenuBar();   

mb.setBounds(0,0,800,20);   

mb.add(file);   

ta=new JTextArea(800,800);   

ta.setBounds(0,20,800,800);   

add(mb);   

add(ta);   

}   

public void actionPerformed(ActionEvent e) {   

if(e.getSource()==open){   

JFileChooser fc=new JFileChooser();   

int i=fc.showOpenDialog(this);   

if(i==JFileChooser.APPROVE_OPTION){   

File f=fc.getSelectedFile();   

String filepath=f.getPath();

ArrayList<String > oper = MemoryCalculator.operations;

try{  

//BufferedReader br=new BufferedReader(new FileReader(filepath));  

BufferedWriter bw = new BufferedWriter(new FileWriter(filepath));

String s1="",s2="";

for(int j=0;j<oper.size();i++){   

s1 = oper.get(j) ;

bw.write(s1);

}   

//ta.setText(s2);   

bw.close();   

}catch (Exception ex) {ex.printStackTrace(); }

}   

}   

}   

public void openfile() {   

FileChooserExample om=new FileChooserExample();   

om.setSize(500,500);   

om.setLayout(null);   

om.setVisible(true);   

om.setDefaultCloseOperation(EXIT_ON_CLOSE);   

}   

}

//output:


Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
7. Save

What would you like to do? 1
What is the second number? 7
The current value is 7.0

Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
7. Save

What would you like to do? 7
The current value is 7.0

Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
7. Save

What would you like to do?

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote