<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import CS99.*;

/**
 * Calculator - Lab 4
 * Author: Michael Clarkson
 * NetID: mrc26
 * Date: 7/5/00
 */
class Calculator {

	public static void main(String args[]) {
		
		System.out.println("Calculator");
		System.out.println("----------");
		System.out.println();
	
		////////////////////////////////////////////
		// Input
		////////////////////////////////////////////
		
		double num1, num2;	// The two operands the user enters
		char op;			// The character representing the operation
							//   that the user enters, in {+, -, *, /}.
		
		System.out.print("Enter a number: ");
		num1 = Console.readDouble();
		
		System.out.print("Enter an operation (+, -, *, /): ");
		op = Console.readChar();
		
		System.out.print("Enter a number: ");
		num2 = Console.readDouble();
		
		
		////////////////////////////////////////////
		// Calculation
		////////////////////////////////////////////
		
		boolean validResult = true;		// Is the value in result valid?
		double result = 0;				// The result of the operation
		
		if (op == '+') {
			result = num1 + num2;
		} else if (op == '-') {
			result = num1 - num2;
		} else if (op == '*') {
			result = num1 * num2;
		} else if (op == '/') {
			if (num2 == 0.0) {			// Guard against division by 0
				validResult = false;
				System.out.println("Cannot divide by 0");
			} else {
				result = num1 / num2;
			}
		} else {
			validResult = false;
			System.out.println("Invalid operation.");
		}
		
		
		////////////////////////////////////////////
		// Output
		////////////////////////////////////////////
		
		// Only print this out if the result is valid
		if (validResult) {				
			System.out.println(num1 + " " + op + " " + num2 + " = " + result);
		}
	}
	
}
</pre></body></html>