|
Comp201: Principles of Object-Oriented Programming I
|
Check the time stamp at the bottom of the page to be sure that you have the latest version with any typographical corrections!
Pledge of Honor |
1.a 5 pts | 1.b 5 pts | 1.c 15 pts | 2.a.i 10 pts | 2.a.ii 10 pts | 2.a.iii 10 pts | 2.b 15 pts | 3.a 15 pts | 3.b 15 pts | Total: 100 pts
|
Below is the stub code for the class Car. You are to complete the code as specified in the problem questions that follow.
/** * Modeling a car with state-dependent behaviors. */ public class Car {
private int mileage = 0;
private static abstract class ACarState { // STUDENT TO DETERMINE WHAT THE STATE-DEPENDENT METHODS ARE AND // ADD THE CORRESPONDING ABSTRACT METHODS TO THIS ABSTRACT ACarState. // See problem 1.b }
// STUDENT TO FIGURE OUT WHAT THE CONCRETE STATES ARE AND
// CREATE THE CONCRETE STATES AS ANONYMOUS INNER CLASSES.
// See problem 1.c; code for concrete car states goes here...
// THIS IS THE STATE OF A CAR;
// STUDENT TO SET aCarState TO AN APPROPRIATE INITIAL STATE
private ACarState aCarState;
// STUDENT TO APPLY THE STATE PATTERN TO DELEGATE // THE STATE-DEPENDENT BEHAVIORS OF A CAR TO ITS STATE. // See problem 1.b public String startEngine() { // STUDENT TO COMPLETE }
public String stopEngine() { // STUDENT TO COMPLETE }
public String pushAccelerator() { // STUDENT TO COMPLETE }
public String hitTree() { // STUDENT TO COMPLETE }
public int getMileage() {
// STUDENT TO COMPLETE
}
}
The following is an example of a sequence of interactions with a Car object showing its response to each method call.
(The above sequence is replicated in the supplied test code).
Problem 1.a (5 pts): From
the above sequence interactions with a Car object, it is clear that the
Car object changes its behavior dynamically. This dynamic change of behavior
can be implemented using the state design pattern. How many states does
Car
have? What are they? Write your answer at the top of the
supplied Car.java code.
Problem 1.b (5 pts): Of the five
Car methods,
startEngine,
stopEngine, pushAccelerator, hitTree, getMileage,
which ones are state dependent? Is the field
mileage state-dependent? Add the
corresponding state-dependent methods to the abstract class
ACarState that represents the union of all the
states of the Car. Write your answer at
the top of the supplied Car.java code. and complete the code for the aforementioned five
Car methods.
Problem 1.c (15 pts): Use anonymous inner classes to implement all the concrete states of Car such that the above behavior is replicated. Your code should conform to the following restrictions:
Definition of Palindrome: According to Webster.com, a palindrome is a word, verse, or sentence (as "Able was I ere I saw Elba") or a number (as 1881) that reads the same backward or forward. In this problem, you are to determine whether or not an LRStruct is a palindrome in two different ways.
Problem 2.a (30 pts total): One way is to check the LRStruct against its reverse to see if they both contain the same elements in the same order. To implement this algorithm do the following.
public class LRSReverse implements IAlgo {
// STUDENT TO COMPLETE SINGLETON PATTERN CODE IF APPLICABLE.
public Object emptyCase(LRStruct host, Object... nu) {
// STUDENT TO COMPLETE
}
public Object nonEmptyCase(LRStruct host, Object... nu) {
// STUDENT TO COMPLETE
}
}
public class LRSEqual implements IAlgo {
// STUDENT TO COMPLETE SINGLETON PATTERN CODE IF APPLICABLE.
public Object emptyCase(LRStruct host, Object... other) {
// STUDENT TO COMPLETE
}
public Object nonEmptyCase(LRStruct host, Object... other) {
// STUDENT TO COMPLETE
}
}
public class LRSPalindrome implements IAlgo {
// STUDENT TO COMPLETE SINGLETON PATTERN CODE IF APPLICABLE.
public Object emptyCase(LRStruct host, Object... nu) {
// STUDENT TO COMPLETE
}
public Object nonEmptyCase(LRStruct host, Object... nu) {
// STUDENT TO COMPLETE
}
}
Problem 2. b (15 pts): Another way to determine whether or not a given LRStruct is a palindrome is first to observe the following.public class LRSPalin implements IAlgo {
// STUDENT TO COMPLETE SINGLETON PATTERN CODE IF APPLICABLE.
public Object emptyCase(LRStruct host, Object... nu) {
// STUDENT TO COMPLETE
}
public Object nonEmptyCase(LRStruct host, Object... nu) {
// STUDENT TO COMPLETE
}
}
In this problem we will explore algorithms on a mutable list that will enable us to randomize the order of the elements in the list.
Problem 3.a (15 pts): RandomSwap First, we will need an algorithm to swap the first element of a LRStruct list with a random element in the rest of the list.
The problem here is that in order to randomly pick an element from the rest of the list, we must first know how may elements are in that list. Otherwise, we cannot insure an equal probability for swapping with any given element. After we know how many elements are in the rest of the list, we can pick a random number that is at most that large and then swap with that number element in the rest of the list.
We could do this in two passes of the list, one to get the length and another to swap with the randomly chosen element. NOT!
Instead we will accumulate the length using a forward accumulation (i.e. on the way in) and then swap the element on the way back out. We do this by returning a count, which is a random number that is between zero and the length of the list. The count gets decremented every time the algo returns, so if we swap only when the count exactly equals zero, it will swap at the desired point and swap only once. Ta da! One pass through the list!
More succinctly, the entire algorithm looks like this:
You are to do the following:
Problem 3.b (15 pts): Randomize To randomize the whole LRStruct is now almost trivial.
You are to do the following:
Note: The testing routine utilizes the fact that computerized random number generators don't really produce random numbers. If you initialize the random number generator with the same "seed" value every time, then it will give you exactly the same sequence of numbers out every time. Since most security software that relies on random numbers, this fact causes massive problems from keno games to electronic voting systems. We are using this fact to our advantage here to enable us to test the outcome of the randomization since the random number sequence is predictable.
Last Revised Thursday, 03-Jun-2010 09:50:18 CDT
©2006 Stephen Wong and Dung Nguyen