Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Assignment My answers are not showing a decimal point. It should be 5.0 + 2.0 = 7.0 In the text output file and console

Java Assignment

My answers are not showing a decimal point. It should be 5.0 + 2.0 = 7.0 In the text output file and console it's displaying as 5+2 = 7.

Also, if I choose selection 6 (Save), I am not prompted with the Menu Display again.

Initial value is 0.0 should display when initially displaying the option for the first time and should be the first line in the text output file.

There are two separate classes. Calculator and Memory.

Calculator Code

import java.io.File;

import java.io.FileWriter;

import java.util.ArrayList;

import java.util.Scanner;

import javax.swing.JFileChooser;

class Calculator {

ArrayList list = new ArrayList();

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

public Calculator(double currentValue)

{

this.currentValue = currentValue;

}

public void display() {

File outputFile = null;

JFileChooser fileChooser = new JFileChooser( //Creating the JFileChooser

"Choose a file to save the contents"); //Displaying the dialog and getting response

int userSelection = fileChooser.showSaveDialog(null);

if (userSelection == JFileChooser.APPROVE_OPTION) {

outputFile = fileChooser.getSelectedFile();

try {

FileWriter fw = new FileWriter(outputFile); //Writing the Array List into the file and displaying on the console

for (String i : list) {

System.out.println(i);

fw.write(i + " ");

}

fw.close();

} catch (Exception e) {

System.out.println(e);

}

}

}

public static int displayMenu(Scanner choose)

{

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

System.out.println("7. 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)

{

list.add((int) this.currentValue + " + " + (int) operand2 + " = "

+ (int) (this.currentValue + operand2));

this.currentValue += operand2;

}

public void subtract(double operand2)

{

list.add((int) this.currentValue + " - " + (int) operand2 + " = "

+ (int) (this.currentValue - operand2));

this.currentValue -= operand2;

}

public void multiply(double operand2)

{

list.add((int) this.currentValue + " * " + (int) operand2 + " = "

+ (int) (this.currentValue * operand2));

this.currentValue *= operand2;

}

public void divide(double operand2)

{

if (operand2 == 0)

{

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

list.add((int) this.currentValue + " / " + (int) operand2 + " = NaN");

clear();

}

else

{

list.add((int) this.currentValue + " / " + (int) operand2 + " = "

+ (int) (this.currentValue / operand2));

this.currentValue /= operand2;

}

}

public void clear()

{

list.add("Cleared");

this.currentValue = 0;

}

}

Memory Code

import java.io.FileWriter;

import java.util.ArrayList;

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

choice = c.displayMenu(choose);

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:

c.currentValue = 0;

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

break;

case 6: c.currentValue = 0;

c.display();

break;

case 7: c.currentValue = 0;

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

break;

default:

System.out.println("Enter correct choice");

break;

}

}

else

{

right = true;

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

}

}while (choice != 6);

}

}

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

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions

Question

3. What might you have done differently

Answered: 1 week ago

Question

4. Did you rethink your decision?

Answered: 1 week ago

Question

3. Did you seek anyones advice?

Answered: 1 week ago