Micheal.Ma
Micheal.Ma

Reputation: 1

A Memory Calculator Program

I have this program that asks user to enter a value and it calculates it as the user like to initial value of zero, then it ask the user what process to do again and ask the user to enter a value again and it calculates it to the last value of the instance, the problem is every time it asks the user to enter value it calculates it to zero not to the last entry. Please help me find the bug:

The program has to has two classes, one for the calculator and the other for the methods:

FIRST CLASS

public class MainClass {

    public static void main(String[] args) {            
        MemoryCalculator calc = new MemoryCalculator();
        calc.getCurrentValue();
        displayMenu();
    }

    public static int displayMenu() {

        Scanner input = new Scanner(System.in);

        int choice;
        do {
            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.println("What would you like to do?");

            choice = input.nextInt();

            if (choice > 6 || choice < 1) {
                System.out.println("Sorry," + choice + " was not an option");
                return displayMenu();
            }

        } while (choice > 6 || choice < 1);
        MemoryCalculator calc = new MemoryCalculator();

        if (choice == 5) {
            calc.clear();
            return 0;
        } else if (choice == 6) {
            System.out.println("Goodbye! ");
            System.exit(0);
        }

        System.out.println("What is the second number? ");
        double operand2 = input.nextDouble();

        switch (choice) {

        case 1:
            calc.add(operand2);
            break;

        case 2:
            calc.subtract(operand2);
            break;

        case 3:
            calc.multiply(operand2);
            break;

        case 4:
            calc.divide(operand2);
            break;

        }

        return displayMenu();
    }

    public static double getOperand(String prompt) {

        return 0;
    }

}

SECOND CLASS

public class MemoryCalculator {

    private double currentValue;

    public double getCurrentValue() {
        System.out.println("The current value is " + currentValue);
        return 0;
    }

    public void add(double operand2) {
        currentValue = currentValue + operand2;
        getCurrentValue();
    }

    public void subtract(double operand2) {
        currentValue -= operand2;
        getCurrentValue();
    }

    public void multiply(double operand2) {
        currentValue *= operand2;
        getCurrentValue();
    }

    public void divide(double operand2) {
        if (operand2 == 0) {
            System.out.println("Sorry, you can not divide by 0");
        }
        currentValue /= operand2;
        getCurrentValue();

    }

    public void clear() {
        currentValue = 0;
        getCurrentValue();
    }

}

Upvotes: 0

Views: 2942

Answers (1)

Ejaski
Ejaski

Reputation: 199

You probably want to keep the last value stored in "calc". I see 3 bugs.

  1. Move this line before the start of your "do" loop. This will keep it from reseting the value inside this variable/class.

    MemoryCalculator calc = new MemoryCalculator();

  2. Move your ending "while" loop line to the bottom of your method(right before the return statement). It only appears to be working because in your return statement you are calling your method again...see #3. Also you will want to change the "or" to the "and" operator in the while statement "choice>6 && choice<1"

    }while(choice>6 && choice<1);

  3. In you displayMenu method change the return statement, because you don't want it to call itself in an infinite loop... now that the do while loop is fixed.

return displayMenu();

to this

return choice;

Upvotes: 1

Related Questions