C Sc 335 Test 2 ANSWERS

// Add any instance variables you will need here

+1 Stack<Command> redoStack = new Stack>();

+1 Stack<Command> undoStack = new Stack>();

private class WithdrawListener implements ActionListener {

public void actionPerformed(ActionEvent evt) {

// Get the user input and convert to a number. Assume input is a valid

String numberAsString = withdrawField.getText();

double amount = Double.parseDouble(numberAsString);

// Handle withdrawal so it changes currentAccount and can be undone

+1 Command c = new WithdrawCommand(currentAccount, amount);

+2 undoStack.push(c);

+1 c.doIt();

withdrawField.setText(""); // Clear the correct field

updateBalanceLabel(); // Show the current balance

}

}

private class DepositListener implements ActionListener {

public void actionPerformed(ActionEvent evt)

{ // Get the user input and convert to a number. Assume the input is a valid number.

String numberAsString = depositField.getText();

double amount = Double.parseDouble(numberAsString);

// Handle the deposit so it changes currentAccount and can be undone

+1 Command c = new DepositCommand(currentAccount, amount);

+2 undoStack.push(c);

+1 c.doIt();

withdrawField.setText(""); // Clear the correct field

updateBalanceLabel(); // Show the current balance

}

}

private class UndoListener implements ActionListener {

public void actionPerformed(ActionEvent evt) {

// Undo the most recent operation

+1 if (!undoStack.isEmpty()) {

+2 undoStack.peek().unDoIt();

+3 redoStack.push(undoStack.pop());

}

private class RedoListener implements ActionListener {

public void actionPerformed(ActionEvent evt) {

// Redo the most recently undone operation

+1 if (!redoStack.isEmpty()) {

+2 redoStack.peek().doIt();

+3 undoStack.push(redoStack.pop());

}

2. Here are the names of the 23 Object-Oriented Design Patterns cataloged in the GoF book. Factory Method, Builder,

Pattern Name / Synopsis
a. Singleton / Ensure a class has only one instance and there is a global point of access to that instance.
b. Iterator / Provide a way to access the elements of a collection sequentially without revealing the underlying implementation.
c. Composite / Allow programs to treat individual objects and compositions of objects uniformly. Example: A JPanel may contain buttons, text fields, and other JPanels
d. Factory Method / Define a method for creating objects while controlling which class to instantiate.
e. Strategy / Make an algorithm interchangeable with other algorithms. Let different classes implement common methods.
f. Observer / Allow one object to notify registered objects that a change in its state occurred so the registered objects can do the appropriate thing.
g. Command / Let developers encapsulate a request as an object, so you can parameterize clients with different requests and can queue or log, time, or log requests and require companion operations such as undo.
h. Decorator / Allows behavior to be added to an existingobject by wrapping other objects that have that desired behavior.
i. Adaptor / Allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface.
j. Flyweight / Objects minimizes memory use by sharing as much data as possible with other similar objects. Examples include String pooling and interning the Integer objects in the range of -128 through 127, a Java gotcha.

3a) What output does the server code generate? 3b) What output does the client code generate?

This server waits for a day day 11

11 year 2012

2012 day 18

year 2013

4. If your team is completing 60 story points for three weeks in a row, what would be a good estimate of what the same team should plan for the next week long Sprint/Iteration? __60___ (5pts)

5. Are story points such as 1, 3, 5, 8, or 13 the number of hours the customer claims it will take you to complete that requirement? <yes or no> ____no__ (5pts)

7. To follow the Law of Demeter, from a method, a message can be sent to an object limited by four relationships. Name three of these four relationships between the method and the object receiving the message. (9pts)

From slide #26 in presentation #27

n  this object

n  a parameter of the method

n  an instance variable of this object

n  an object created within the method

or an object return by a message from that method

8. Which code violates the Law of Demeter, a or b? __b___ (3pts)

a double amt = game.getCash(currentPlayer);

b double amt = game.getPlayerList().player(currentPlayer).holdings().getCash();

9. Which is the better design a or b __b__ ? (2pts) Why? (2pts)

Hides details

Does not compare types with instanceof

Looks like s will be sent a polymorphic message. Point, Line, and Square all have draw messages

10. Which code represents the better design, a or b _b _____? (2pts) Why? (2pts)

Lower coupling

Encapsulates long boolean expression into a method

11. REFACTORING CATALOG According to the refactoring catalog, what would be the better design, a or b. Explain why it is a better design. If you chose differently, you will lose points. (20pts)

Which is the better design, a or b ___b__? (2pts) Why? (2pts)

Favor composition over inheritance

Which is the better design, a or b ___b_ ? (2pts) Why? (2pts)

When a method is doing the same thing but with different values, parameterize the methods

Which is the better design, a or b __b___? (2pts) Why? (2pts)

Captures common properties in the superclass

class MallardDuck implements Duck {

public void fly() {

System.out.println("I'm flying");

}

public void quack() {

System.out.println("Quack");

}

}

interface Turkey {

public void gobble();

public void fly();

}

class WildTurkey implements Turkey {

public void fly() {

System.out.println("I'm flying a short distance");

}

public void gobble() {

System.out.println("Gobble gobble");

}

}

+2 public class TurkeyAdapter implements Duck {
+1 private Turkey turkey;
public TurkeyAdapter() {
+1 turkey = new WildTurkey();
}
+1 public void quack() {
+2 turkey.gobble();
}
+1 public void fly() {
+1 for(int i=0; i < 5; i++) {
+2 turkey.fly();
}
}
}

1