Project 2: Calculator

The goal of this project is to review the basics of object-oriented programming and to give you an initial try at system design.

1. Introduction and setup

In this project you will make a program that emulates a 4-function calculator. This program will use a GUI; a window looking like a calculator will appear on the screen, and the user can manipulate it by clicking on the buttons.

I have made the "face" of the calculator already-- the GUI is given to you. You need to write the rest of the program, which will interact with the window.

After moving into your directory for this course, checkout the code for project 1 (a proj1 directory will be created automatically).

svn checkout file:///cslab/class/cs245/projects/proj2

You should get the following files:

CalculatorFace.java             ConcreteCalculatorFace.class
ConcreteCalculatorFace$1.class

CalculatorFace is an interface that defines the method signatures for the object representing the calculator window. ConcreteCalculatorFace is a class implementing CalculatorFace (I am not giving you the source code). ConcreteCalculatorFace$1 is another class that ConcreteCalculatorFace makes use of. You don't need to know anything about it.

2. The calculator face

As you can see from inspecting the code in CalculatorFace.java, the only interaction with the window available to use is

You may either get the buttons all at once by means of an iterator (for example, if you want to store them yourself somehow) or can look them up by calling getButton with the character that appears on the button.

(The buttons do not write to the screen by default; they do nothing without you writing something for it.)

You will have to write action listeners for the buttons. If you have never used the ActionListener class (or other GUI components), read the relevant sections in the textbook (from chapter 17), look at the Java API, and ask for help if you want a fuller explanation or some guidence in finding the relevant parts of the class. (Please read the sections before asking for help... but do not hesitate to ask.)

ConcreteCalculatorFace has a constructor that takes no parameters. A window automaticallly appears.

Important: The "screen" on the calculator is only 10 characters wide. Java displays double numbers using more than 10 characters. Make sure that when you display results that you do not run off the end of the screen. You need to think about how to format your results. There are several ways to do this. One way is to use the DecimalFormat class described starting on page 73 in Absolute Java. You also can devise your own formating strategy using String manipulation.

3. To turn in

Don't submit your program using svn (you shouldn't be able to anyway if you try). Instead, copy all the files you made or modified to a turn-in directory I've made for you.

cp filename /cslab/class/cs245/turnin/{andrew,sarah,christopher,ryan,katelynn}

DUE: Wednesday, Sept 26, 5:00 pm.


Thomas VanDrunen
Last modified: Mon Sep 17 15:40:08 CDT 2007