RPNCalculator Error Code

This is my second programming class, and I'm new to Java. I am working on my first assignment and includes classes and methods. I know very little about these topics and consider myself lost. In my assignment, I am asked to create an RPN calculator that asks the user for two numbers and an operator. The calculator performs an operation on these two numbers and asks for another number and another operator. Then the calculator uses the result from the first set and performs another operation with the newly entered number. The program ends when the user enters a letter. I am attaching my code (raw). I need the same understanding methods as with coding. Please help as soon as possible, I want to find out. Any help is appreciated.

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; } } 
+8
java methods class
source share
2 answers

OK Like this. I changed your program a bit. The following are issues that occur when you change your program. 1. There was no writeOutput () method in any of the classes, so I had to remove this piece of code. 2. I had to introduce a while loop to make this an iterative process. 3. after we read the operator line, we lowered the operator.charAt (0) method.

Since you use the switch statement, you cannot compare the value of the statement with the string value "nCr". To use this, you will need to use String comparisons using the equals () method.

And last but not least, the result of the operation should be one of the inputs for the next round of calculations. The value of the result of the first operation will be transferred to the first parameter of the next operation.

 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; } } 
+1
source share

You treat the names of your functions as if they were objects of a class. Does it even compile?

You want to exit your program when the user types "q".

You start by reading the statement at the beginning. Why?

You need to enclose the main body of your program in a cycle.

I'll post a few lines for you, but the bulk of the changes remain an exercise for you

 double result; System.out.print("Enter the first value"); result = keyboard.nextDouble(); boolean done = false; while (!done) { System.out.println("Enter a value"); v1 = keyboard.nextDouble(); System.out.print("Enter one of the valid operators +, -, *, /, nCr,: "); operator = keyboard.nextLine(); symbol = operator.charAt(0); switch (symbol) { case '+': result = sum(result, v1); break; ... case 'q': System.out.println("Your last result was " + result); done = true; break; } } 
0
source share

All Articles