Fundamentals of Computer Science II (CSC-152 98S)

Assignment One: A Simple Calculator

Assigned: Wednesday, January 21, 1998
Due: 8am, Wednesday, January 28, 1998

Summary. In this assignment, you will be developing a simple calculator in Java. Although Java is an object-oriented language, this program will be primarily imperative.

Assignment. Write a simple interactive calculator. More particularly, write a Java program that repeatedly reads in simple expressions (of the form number op number) and prints out the results of those expressions. You should use the library to get your input and the library to print your output. You will need to use the Java conditional statement (if) to determine which operation to apply.


To compare two strings (e.g., alpha and beta), use the equals method, as in

if (alpha.equals(beta)) {
  out.println(alpha + " and " + beta + " are the same.");
else {
  out.println(alpha + " and " + beta + " are different.");

You can require a special character to indicate "I'm done" (and look for that character with peek) or you can call the eof method. For the latter, you'll need to hit control-D to indicate "I'm done".

You will most likely want to do repetition with recursion. You can also use a while loop, presumably of the form

while (!done) 
  if (in.eof()) 
    done = true;
  else {
} // while

Evaluation. First and foremost, you are expected to turn in a working program. However, it is also very important that you comment your program and begin working on a good style (in particular, choosing good variable names and indenting correctly).

Turning it in. Create a file that contains your program code and one or more sample runs of your program. Email that file to me.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

Source text last modified Mon Jan 26 07:40:46 1998.

This page generated on Tue Jan 12 11:44:33 1999 by SiteWeaver.

Contact our webmaster at