CS 163/164, Fall 2018
Programming Assignment - P3
Desktop Calculator
Due Monday, Sep. 10th at 6:00pm
Late - Tuesday, Sep. 11th at 8:00am
Objectives of this Assignment
- To implement a Java method with a parameter and return value,
- to learn how to check for valid input using a Scanner object,
- to learn how to parse a String using a Scanner object,
- to use a switch statement to evaluate an expression with different operators, and
- to practice using the Double wrapper class to convert a number to a string.
Instructions
Create a Java file called P3.java with a class name P3, and a method named evaluate.
A starting point is shown below:
// P3 Assignment
// Author: ???
// Date: ???
// Class: CS163/164
// Email: ???@cs.colostate.edu
import java.util.Scanner;
public class P3 {
public static String evaluate(String expression) {
// Print expression
System.out.println(expression);
// Declare variables for operands, operator, result, and return value
String returnString = "";
// Create Scanner object to parse expression
// Use Scanner to read operands and operator
// Compute a numerical result for the expression
// Convert numerical result to string
// Return result
return returnString;
}
}
All of the Java code you write should be contained in the evaluate method.
For this assignment, you must follow the directions below exactly:
- Create a project in Eclipse called P3.
- Create a class in the project called P3, without a main method!
- Copy the starting code shown above into the class.
- P3.java should not have a main method!
- Download the graphical user interface from here.
- Declare double variables for both operands and result.
- Declare String variables for the operator.
- Create a Scanner to parse the expression, as discussed in class.
- Use the Scanner to check whether the first operand is a double.
- If so, read it into the first operand variable.
- If not, return "Invalid Operand!", thereby exiting the method.
- Use the Scanner to check whether there is an operator.
- If so, read it into the operator variable.
- If not, return "Invalid Operator!", thereby exiting the method.
- Make sure the operator variable is a "+", "-", "*", "/", "%", or "^".
HINT: A switch statement with a default case might be useful.
- If not, return "Invalid Operator!", thereby exiting the method.
- Use the Scanner to check whether the second operand is a double.
- If so, read it into the second operand variable.
- If not, return "Invalid Operand!", thereby exiting the method.
- Using a switch statement based on the operator, evaluate the expression.
- When complete, the result variable should contain the result of the expression.
- The operator generally corresponds to the Java operator, i.e. "*" for multiplication.
- One exception is the "^" operator, which requires special handling.
- The "^" operator is exponent, which raises the first operand to the power specified by the second operand.
- By far the easiest way to implement the exponent operator is using Math.pow().
- Several examples of input expressions and the return strings are shown below.
- Use the Double wrapper class to convert the numerical result to a string, for example:
CODE: String returnString = Double.toString(result);
- Return the converted string, thereby exiting the method.
You do not have to close the Scanner, but you can if you want to.
Sample Output
The code you are writing is integrated into a graphical user interface (application)
for a desktop calculator. To use the Calculator, run the main method in
Calculator.java.
Enter a number, an operator, another number, then press the equals sign. Examples
of the correct output are shown below:
Expression (input string) | Result (return value) |
"11.22 + 3.456 = " | "14.676 " |
"5.555 - 32.14 = " | "-26.585 " |
"25634.8 * .32 = " | "8203.136 " |
"2.1 / 55.3377 = " | "0.037948812473232535 " |
"12345 % 23 = " | "17.0 " |
"5 ^ 4 = " | "625.0 " |
"Whatever + 2 = " | "Invalid Operand! " |
"4.0 - 1.2.3 = " | "Invalid Operand! " |
"1.234 $ 0.5 = " | "Invalid Operator! " |
Specifications
- Work on your own, as always.
- The name of the source code file must be exactly
P3.java
- Name the file exactly - upper and lower case matters!
- Comments at the top as shown above.
- Assignments should be implemented using Eclipse.
- Assignments should be implemented using Java, version 1.8.
- Make sure your code runs on machines in the CSB 120 lab.
- Turn in through the Checkin portal.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests
Preliminary tests use the values shown in the table above.
- testCompile: checks that program compiles. (0 points)
- test1: checks addition ("+"). 5 points
- test2: checks subtraction ("-"). (5 points)
- test3: checks multiplication ("*"). (5 points)
- test4: checks division ("/"). (5 points)
- test5: checks modulo ("%"). (5 points)
- test6: checks exponentiation ("^"). (10 points)
- test7: checks that invalid first operand returns "Invalid Operand!". (5 points)
- test8: checks that missing first operand returns "Invalid Operand!". (5 points)
- test9: checks that invalid operator returns "Invalid Operator!". (5 points)
- test10: checks that invalid second operand returns "Invalid Operand!". (5 points)
- test11: checks that missing second operand returns "Invalid Operand!". (5 points)
- Final Tests
Final tests use arbitrary values.
- test12: checks addition ("+"). 5 points
- test13: checks subtraction ("-"). (5 points)
- test14: checks multiplication ("*"). (5 points)
- test15: checks division ("/"). (5 points)
- test16: checks modulo ("%"). (10 points)
- test17: checks exponentiation ("^"). (10 points)
- Final grading includes the preliminary tests.
Submit P3.java to Checkin.