Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!