CS 161 Assignment #2

Due Monday, September 21st by 11:59pm
(Not accepted after September 25th)

Introduction

For this assignment you'll write a Calculator class that implements the operations of a basic four-function calculator with memory. The methods will each print a message as they're invoked, much like a printing calculator would. Unlike a real calculator, however, yours will only operate on integers.

The Assignment

I've created a new project for you that contains the outline of a Calculator class. Please download and modify CalculatorProject instead of creating a new project via BlueJ, otherwise you won't be able to submit the assignment when you're finished. (The Calculator class currently has some sample methods to remind you of the syntax, but you should delete any code you don't use before submitting.) The steps below will guide you through creating the necessary fields and constructors, then the four arithmetic operators, and finally the memory-related functions. Good style is important! Make sure you write comments above each of the methods as you go, indent your code properly, and use meaningful variable and parameter names in your code. In the descriptions below, the text I'm showing illustrates an interaction with your Calculator class in the codepad window. You can perform equivalent tests via the point-and-click interface if you prefer.
  1. Your calculator will need to maintain its state. While we won't have a display like a normal calculator, a Calculator object will still need to remember its current value so that it knows what to do when we add to the current value, subtract from it, etc. Define a new field at the top of the class that will hold the calculator's value. Make sure you give the field a meaningful name so the rest of your code is easier to read.
  2. Next, define two constructors: The first should initialize the state to be zero, and needs no parameters. The second should take a single integer argument, and set the calculator's initial value to the specified value.
  3. Now add a method called add that takes a single integer argument. It should set the calculator's state to be the sum of its old value and the argument to add. It should also print a message describing the inputs to the addition and the result, as shown below. (In this output, lines starting with ">" have been typed into the BlueJ codepad, and the corresponding output in the terminal window is shown below each statement.)

    > Calculator c1;
    > c1 = new Calculator();
    > c1.add(5);
    0 + 5 = 5
    > c1.add(10);
    5 + 10 = 15
    > Calculator c2;
    > c2 = new Calculator(100);
    > c2.add(61);
    100 + 61 = 161
    
  4. After verifying that your constructors and add method work, implement the subtract and multiply methods. They should each take a single integer argument, like add. The subtract method should subtract its argument from the calculator's value, and multiply should multiply its argument by the current value. Each should print a single line of output describing the arithmetic operation, its inputs, and result:

    > Calculator c;
    > c = new Calculator(20);
    > c.multiply(3);
    20 * 3 = 60
    > c.subtract(10);
    60 - 10 = 50
    > c.multiply(2);
    50 * 2 = 100
    
  5. Now it's time to implement divide. It should work like the other three arithmetic methods, but with two differences: First, it should print a message if the denominator is zero, and clear the calculator's value. Second, it should print an additional message if the division will result in a loss of precision. Since our calculator only works with integers, some divisions will cause the result to be truncated. (Hint: Don't forget about the mod operator.)

    > Calculator c;
    > c = new Calculator(40);
    > c.divide(2);
    40 / 2 = 20
    > c.divide(3);
    Warning: division resulted in loss of precision.
    20 / 3 = 6
    > c.divide(2);
    6 / 2 = 3
    > c.divide(-1);
    3 / -1 = -3
    > c.divide(0);
    Error: division by zero -- value cleared.
    -3 / 0 = 0
    
  6. Next, implement the memory operations. Start with store, which takes no arguments, but causes the calculator's current value to be stored in a memory location. (Hint: You'll need to add a new field to your class.) Then implement recall, which takes no arguments, but causes the stored value to replace the calculator's current value. For example:

    > Calculator c;
    > c = new Calculator(20);
    > c.add(3);
    20 + 3 = 23
    > c.store();
    23 --> mem
    > c.multiply(5);
    23 * 5 = 115
    > c.recall();
    mem --> 23
    > c.add(10);
    23 + 10 = 33
    

Extending the Assignment

There's no extra credit for these extensions, but completing one or more will help you achieve enlightenment.

Submitting

Before submitting, make sure your code compiles, and test each of your methods thoroughly. When you're convinced that they all work, submit the project just like you did with the last assignment. If the submit menu item is greyed out, save this file to your desktop. Quit BlueJ, move "submission.defs" into your project folder, restart BlueJ, and try submitting again.


Brad Richards, 2009