CSU Banner

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

  1. To implement a Java method with a parameter and return value,
  2. to learn how to check for valid input using a Scanner object,
  3. to learn how to parse a String using a Scanner object,
  4. to use a switch statement to evaluate an expression with different operators, and
  5. 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:

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

Grading Criteria


Submit P3.java to Checkin.


CS Banner
CS Building