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

JavaScript

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.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement