CS 101 (Spring 1999)
Lab 1:Expressions, Functions, and Procedural Abstraction

Lab Assigned Design Due
(Mondays 2 PM)
Lab Due
(Fridays 2 PM)
15 Jan None 22 Jan


By the end of this lab, you should...

Before starting:

Read over the entire lab before you start. Before beginning the assignment, download the provided portions of the lab into your cs101 folder by clicking on the "download project" button in the on-line version of this assignment.
[[[download project folder for PC]]]
[[[view project files for UNIX]]]


In this lab, you will implement the "brains" of a calculator.  You have been supplied with a Calculator class that, when instantiated, displays a calculator window on the screen, complete with buttons and display.  However, if you run the program, you will notice that the calculator is "stupid."  It doesn't know how to perform any of the operations.  Your job is to create a SmartCalculator that extends the Calculator by defining methods that perform the appropriate operations.  After you define the various methods, you should test them by running the program.  Enter values into the calculator and check the results.

Begin by opening the Lab1.prj project file in Symantec Cafe. Try executing it. Notice that the buttons can be clicked, but calculator is "stupid," as described above.

Now open the file SmartCalculator.java within the Symantec Cafe editor. Supply the necessary header information (name, lab section, etc.). This is the only file you need to modify for this lab. In this file, you'll extend the calculator by providing the functionality to make it "smart" as described below.

Each of the following specifications describes the input parameters and return values of a desired procedure that is needed as a method of the calculator.  For each specification,

Hint: To avoid making the same mistakes over and over, test as you go. That is, type in a procedure, compile, execute, and verify the results, and then go on to the next problem.  After you get the hang of it, you can write more code before you test.  Java's built-in class Math may be helpful for some of these exercises.

  1. Unary operators on numbers.  Each of these are operators that take a single number (in this case, a double) as a parameter, and return a double as a result.  The first one has already been done for you in the provided file.
  2. Binary operators on numbers.  Each of these are operators that take in two numbers (both of type double) as parameters, and return a double as a result.
  3. Binary operators on booleans.  Each of these are operators that take in two numbers (both of type boolean) as parameters, and return a boolean as a result.
  4. Memory operations.  Now it's time to implement the methods corresponding to the top three buttons in the orange column of the calculator.  These buttons add a value into the calculator's memory (M+), recall the value from the memory (MR), and exchange the currently displayed value with the one in the calculator's memory (MXCH).  In order to keep track of the current value of the memory, you should first declare an instance variable of type double at the top of your SmartCalculator class definition.  The declaration should appear as the first line following the open brace, before any method definitions.  You can choose any appropriate name for the variable.  Once that is done, you can define the various methods that access and modify the calculator's memory as follows.  In each of these procedures, when we say "the calculator's memory," we are referring to the instance variable you have defined as part of the SmartCalculator object.
  5. Be sure to test all of your methods thoroughly by entering values into the calculator and checking the answers.  For each of the boolean operations, be sure to test all four possible input combinations.  Also, remember to try zero values and negative values when testing numerical methods.

What To Turn In:

For every CS101 lab you turn in, you should fill in a cover sheet and staple it on the front of your lab. You can either print out the cover sheet (on a single page, please) or just pick one up from the stack on top of the CS101 mailbox. Attach a paper printout of the SmartCalculator.java file containing the methods you wrote, and place it in the CS101 mailbox by 2:00pm on the due date.  Check that you have filled in the header information (name, email, date, and lab section) at the top of the file.  If you need help printing, ask a TA or refer to the FAQ page.

Last modified 14:31:03 CST 21 January 1999 by Ron K. Cytron