Skip to content
Advertisement

Working on an RPN calculator code assignment in Java

This is my second programming class and I am new to Java. I have been working on my first assignment and it involves classes and methods. I know very little about these topics and find myself lost.

My assignment asks me to create a RPN calculator that asks the user for two numbers and an operator. The calculator performs the operation on those two numbers and asks for one more number and one more operator. The calculator then uses the result from the first set and performs another operation with the new number just entered. The program ends when the user enters a letter. I am attaching my code (crude).

import java.util.Scanner;

public class RPNCalc 
{
    public static void main(String[]args)
    {
        Scanner keyboard = new Scanner(System.in);

        double v1, v2;
        String operator = keyboard.nextLine();
        char symbol = operator.charAt(0);

        System.out.print("Enter a value v1: ");
        v1 = keyboard.nextDouble();
        System.out.println();
        System.out.print("Enter a value v2: ");
        v2 = keyboard.nextDouble();
        System.out.println();
        System.out.print("Enter one of the valid operators +, -, *, /, nCr,:  ");
        operator = keyboard.nextLine();
        switch (symbol)
            {
             case'+':
                sum.writeOutput();
                break;
             case'-':
                minus.writeOutput();
                break;
             case'*':
                times.writeOutput();
                break;
             case'/':
                divide.writeOutput();
                break;
             case'q':
                System.out.println("Your last result was "  );
             default:
                System.out.println("You must choose an appropriate operator .");
             }
    }

    double value1, value2;
    int n;

    public static double sum(double value1,double value2)
    {
        double newSum = value1 + value2;
        return newSum;
    }
    public static double minus(double value1, double value2)
    {
        double newMinus = value1 - value2;
        return newMinus;
    }
    public static double times(double value1, double value2)
    {
        double newTimes = value1 * value2;
        return newTimes;
    }
    public static double divide(double value1, double value2)
    {
        double newDivide = value1 / value2;
        return newDivide;
    }
}

Advertisement

Answer

OK. Here we go. I have modified your program a little bit. Here are the issues that came when modifying your program. 1. There was no “writeOutput() method in any of the classes. Therefore I had to remove that piece of code. 2. Had to introduce a while loop to make this a iterative process. 3. moved down the operator.charAt(0) method after we read the operator string.

Since you are using switch statement, you cannot compare the operator value to “nCr” string value. To use this one, you will have to use String comparison using the equals() method.

Last but not least, the outcome of the operation should be one of the inputs for the next round of computation. Meaning the result of the first operation will be passed into the next operation’s first parameter.

import java.util.Scanner;

public class RPNCalc 
{
    public static void main(String[]args)
    {
        Scanner keyboard = new Scanner(System.in);

        double v1, v2;
//        String operator = keyboard.nextLine();
//        char symbol = operator.charAt(0);
        char operator = ' ';//First time around, set this to an something other than 'q'
        String operatorString = " ";
        System.out.print("Enter a value v1: ");
        v1 = keyboard.nextDouble();
        System.out.println();
        while(operator != 'q')
        {
            System.out.print("Enter a value v2: ");
            v2 = keyboard.nextDouble();
            System.out.println();
            System.out.print("Enter one of the valid operators +, -, *, /, nCr,:  ");
            operatorString = keyboard.next();//nextLine() doesn't wait until the user hit enter
            operator = operatorString.charAt(0);
            switch (operator)
            {
             case'+':
                v1 = sum(v1, v2);
                System.out.println(v1);
                break;
             case'-':
                v1 = minus(v1, v2);
                System.out.println(v1);
                break;
             case'*':
                v1 = times(v1, v2);
                System.out.println(v1);
                break;
             case'/':
                v1 = divide(v1, v2);
                System.out.println(v1);
                break;
             case'q':
                System.out.println("Your last result was "  );                
             default:
                System.out.println("You must choose an appropriate operator .");
             }
        }
    }

    double value1, value2;
    int n;

    public static double sum(double value1,double value2)
    {
        double newSum = value1 + value2;
        return newSum;
    }
    public static double minus(double value1, double value2)
    {
        double newMinus = value1 - value2;
        return newMinus;
    }
    public static double times(double value1, double value2)
    {
        double newTimes = value1 * value2;
        return newTimes;
    }

    public static double divide(double value1, double value2)
    {
        if (value2 == 0)
        {
            System.out.println("Division by Zero. Try again");
            return value1;
        }
        double newDivide = value1 / value2;
        return newDivide;
    }
}
5 People found this is helpful
Advertisement