This assignment emphasizes the following topics:
This assignment is due at the beginning of class on Wednesday, 15 October.
As always, a neat write-up of the finger exercises should be turned in along with your completed laboratory.
The second part of your finger exercises is to put together a crib sheet for the exam. You may write anything you want on either side of a single 8.5" x 11" piece of paper. You may work with other students to discuss what might helpfully go on that piece of paper, but you should write your paper up yourself.
Please save your exam crib sheet to turn in with your problem set.
Specifically, you will implement an active object class which will interact with a Calculator object to respond to the user. The Calculator (see attached interface documentation) can do such things as figuring out which button the user presses, and managing redrawing, displaying, and other various things that happen on your computer screen. However, it does not actually know how to interpret these button presses, or what answer to display. That is your object's job.
For this lab, you will need to define an active (i.e., animate) object class that will interact with the Calculator object. A particular instance of a Calculator object will be passed to your object's constructor when your object is created (i.e., when you run the Calculator.Main program), so be sure to define an appropriate constructor. (Note that this is very similar to what you did in last week's Balancer lab.) Your object must then tell the Calculator exactly what to do, such as recognizing and appropriately responding to the user's button presses.
athena % java Calculator.Main
These are all static, i.e., accessible as parts of the Calculator interface: e.g., Calculator.OP_ADD. They are also final, i.e., they cannot be changed. Finally, they are all of type int. (Hint: think switch/case.)
All of these constants are distinct from one another and from the numbers 0 through 9; it is intended that the numbers 0 through 9 serve as buttonIDs for their respective buttons.
An additional constant, Calculator.NO_OP, is not a buttonID but is provided for convenience.
public int getButton();The int returned is a buttonID indicating which of the buttons was pressed: one of 0 through 9, or one of the buttonIDs defined in the Calculator interface. Each time that the getButton method is called, it returns the next button pressed. So, if the calculator's user presses 3 + 4 =, your object's first call to your Calculator's getButton() method will return 3. The next call to getButton() will return Calculator.OP_ADD. The third call will return 4, and the fourth call will return Calculator.EQUALS. Then, if you call your Calculator's getButton method again, it will wait for another button to be pressed before returning.
Each Calculator object also has its own getText and setText methods for interacting with the Calculator's display. (Note that the display, and these methods, operate using Strings, not a numeric type such as int.) Their signatures are as follows:
public String getText();
public void setText( String s);getText() returns the String currently displayed on that Calculator's screen. Unlike getButton(), getText() doesn't wait for something to happen (e.g., for the value to change) before returning it; it just tells you what text is currently being displayed.
setText replaces any currently displayed text with its single argument, a String.
We've provided you with a coercion routine which takes a String and returns a floating point (double) decimal number:
double cs101.util.Coerce.StringTodouble( String s);(Remember that in Java, double (the primitive data type) and Double (the object) are two different kinds of things.)
In fact, it is built out of two java.lang.Double methods:
public static Double Double.valueOf( String s); public double Double.doubleValue;You can use these methods directly if you prefer.
To produce a String from a double, you can use:
public static String String.valueOf( double d);To figure out whether a char or String appears in a given String, use
public int String.indexOf( int ch); public int String.indexOf( String str);
As usual, you should follow an incremental design strategy -- keeping the end goal (a working calculator) in mind, but starting with simple implementations first. Before going to lab, think about how you would implement the following:
Write code that will allow you to reset your calculator. For this purpose, you will probably find it useful to define a reset() method which you can call whenever you want to reset the calculator. Later on, you can then fill the body of reset() with the things you want to do when you reset (i.e., setting fields to their appropriate values). Remember that there is nothing wrong with an object calling its own methods. In fact, methods used in this way can be very useful for grouping commonly used code together, so that instead of rewriting the same code whenever you need it, you can just call the method.
Question: How would you make the calculator reset (i.e., call the reset() method you have defined in your class) when then the CLEAR button is pressed, but not at any other time?
At this point, you can now try to design a simple ButtonHandler that accepts only digit op digit =. Any other input should cause the ButtonHandler to reset itself. Map out the logic of this controller. At each step, be explicit about what appears on the Calculator's display. Since the operator does not appear on the display, you may want a place to store it. Be sure that you know where you'll keep each piece of information at each point along the way.
Questions: Draw an FSM state diagram that represents this logic. How many states does it have? How do you move from one state to another?
Once you have digit op digit =, you can build more complexity in any of several ways. Take these one step at a time. Some of them require limited additional storage, e.g., if you wish to treat all digits or all numbers uniformly.
Your code should define a class called ButtonHandler. Its constructor method should take a Calculator as its only argument. Your ButtonHandler should be an active object: it should have its own (activated) animacy. At each point in time, it should gobble up a button press (by calling the Calculator object's getButton() method) and, if appropriate, update the Calculator's display. (It should also update its own internal state at each step.)
You will almost certainly want to keep your central control loop small. This is best accomplished by spinning off procedures to handle separate cases. By passing parameters, you can make these cases reasonably general. Remember that you don't want to be writing a lot of the same code many times. When this happens, it is a good hint that you've got a common pattern, and it's always a good idea to write down the common pattern, give it a name, and make it into its own method. But make sure that your common pattern makes sense, too. A good rule of thumb is: if you can't explain your code pretty clearly and concisely to someone else, it isn't designed right.
Details of the ButtonHandler specification, as well as hints for a stage-by-stage implementation, are given in the Laboratory section, below. Roughly speaking, it follows these steps:
When you begin to code, even though you may have constructed a sophisticated design for your ButtonHandler, you should always begin your labwork with a simple, independently testable implementation and build on it as each stage works robustly.
For example, start with a very simple class with just a constructor that takes a Calculator as its only argument, and make sure that it compiles. Then, you can begin testing your class design, without any of the internal logic. Although it makes a pretty lousy calculator, a simple test that will help ensure that your infrastructure is working is to simply echo each button pressed back to its Calculator's screen.
Compile and run your code to make sure that you've built things correctly. If you have problems, now is a good time to work them out.
Remember the following, quoted from the Evaluation Policies handout: "... you will in general get a higher score for basic but elegant, well-tested, well-documented, and well-understood code than for adding features to poorly written/documented/tested code. We are more interested in how well you do what you do than in how much you do."
Record your experiences, including tests that various stages of your implementation succeed and fail. Make sure that you understand how your code behaves, and how it will behave under inappropriate as well as appropriate circumstances.
This course is a part of Lynn Andrea Stein's Rethinking CS101 project at
the MIT AI Lab and the Department of Electrical Engineering and
Computer Science at the Massachusetts
Institute of Technology.