|
Comp201: Principles of Object-Oriented Programming I
|
This tutorial consists of exercises on the visitor pattern discussed in class. It also helps prepare for HW06.
Consider the following UML model of keys and cars:
You are to do the following:
1. Fill in the concrete methods of all the above concrete classes so that only a specific key will drive a specific car when that car's turnKey() method is called with that key, i.e. call its drive() method. All other keys will call the car's alarm() method. Download ACar.java and AKey.java.
For instance, suppose we have:
- An object called ford of type Ford, and
- An object called fordKey of type FordKey
then ford.turnKey(fordKey) will cause ford.drive() to be called.
On the other hand, suppose instead we have
- An object called saturnKey of type SaturnKey
then ford.turnKey(saturnKey) will cause ford.alarm() to be called.
2. Fill in the ACar.drive() and ACar.alarm() methods to print out something indicative that the method has been called, e.g. "Car has been driven!" and "Car alarm sounded!".
3. Document your code, of course. It is recommended that you document the methods before you write the method bodies!
3. Write a test program that clearly tests all possible key and car combinations. Be sure that the test program prints clear messages indicating exactly what is being tested at the moment, i.e. what key-car combination is being used.
Create the object system that models the following using the Visitor Design pattern:
You're sitting in your room...um...."working"....or maybe not..... Suddenly, the phone rings and some person is on the other end. You need to proffer a response, but the exact response behavior will depend on not only what you've been doing, but who the person on the line is.
Now, there are a couple of scenarios:
Some things to consider:
You are required to do the following:
You will need use the IList framework in the listFW package in order to compile and test your code. Download the listFW package with visitors here. Note that these problems are the start of HW06.
Assume that the IList contains Integer objects.
Since Integer objects and primitives like int contain no intelligence and cannot be inherited because they are final, we need an external technique to compare their values and then perform different tasks based on the result of the comparison. This can be accomplished using the if-else construct (see below).
BEFORE you start an exercise, write an appropriate JUnit test method (you can put them all into a single Test_ListVisitor test class) to test your expected results!
The conditional construct, "if-else", is a much abuse feature of most programming languages. We have not introduced it until now because we want you to understand that its use is very often unnecessary and perhaps worse, the product of poor design. Good object oriented programming has minimal use for conditional statements, relying instead on the intrinsic behavior already built into its objects. Delegation is a much more robust, flexible, extensible and proveably correct technique of generating varying behavior than conditional statements.
But there are times when it is useful, particularly when dealing with primitives, as they cannot be endowed with their own intelligence, so we plunge into dark and dangerous waters...
The if-else construct is used to choose between the execution of two sets of statements depending on a boolean value. That is, one set of statements is executed if the value is true and the other set is executed if the value is false. The general syntax for the if-else construct is
if ( [boolean expression]) { [true statement 1] [true statement 2] [etc...] } else { [false statement 1] [false statement 2] [etc...] }
where[boolean expression] is an expression that evaluates to a boolean true or false value. If the[boolean expression] evaluates to true, then the [true statements] will be executed, otherwise the [false statements]will be executed.
For more information on the if-else construct, see the Java Resources page on if-else.
For example:
if ( x > 5) { x = 0; System.out.println("x has been reset to zero."); } else { x++; System.out.println("x = "+x); }
The result of this code would be:
for x = 3: "x = 4"
for x = 7: "x has been reset to zero"
To create expressions that result in boolean values, you can use the primitive comparison operators: > , <, >=, <=, ! (not) , != (not equal), == (equal), && (and), || (or) and ^ (exclusive or)
For more information on theses operators, see the Java Resources page on "Expressions".
Note that these are comparison operators whose expressions result in booleans, which is not the same as actually performing the boolean operation on the supplied values! In particular, "==" does not do an assignment, while "=" does! See the important advice at the bottom of the Java Resources page on if-else that may save you hours of debugging!
Last Revised Thursday, 03-Jun-2010 09:50:18 CDT
©2007 Stephen Wong and Dung Nguyen