C Sc 335 Spring 12 Practice Test 2 Answers

1a) Create an initial list of classes that would do a good job of modeling this system (YOUR ANSWER MAY VARY) Customer, Cashier, Video, Transaction, TransactionLog, VideoCollection

1b) Write a sequence diagram to represent the objects involved in a use case that begins when a customer arrives at a checkout with videos to rent. (YOUR ANSWER MAY VARY)

Here's another example of a sequence diagram drawn with a tool from an anonymous source (different class names, but more readable perhaps)

2.

Pattern Name / Synopsis
a. Iterator / Provide a way to access the elements of a collection sequentially without revealing the underlying implementation.
b. one covered this semester / TBA
c. one covered this semester / TBA

3. Known design pattern: Composite, 3A) UML diagram

3B) class Abstract

publicabstractclass Abstract {

private String name;

public Abstract(String name) {

this.name = name;

}

public String getName() {

returnname;

}

abstractpublicvoid display(String prepend);

}

3C) The Primitive and Composite classes

public class Primitive extends Abstract {

public Primitive(String name) {

super(name);

}

publicvoid add(Primitive primitive) {

System.out.println("'Cannot add to a PrimitiveElement'");

}

publicvoid display(String prepend) {

System.out.println(prepend + "draw " + getName());

}

}

class Composite extends Abstract {

private ArrayList<Abstract> elements = new ArrayList<Abstract>();

publicstaticfinal String INDENT = " ";

public Composite(String name) {

super(name);

}

publicvoid add(Abstract a) {

elements.add(a);

}

publicvoid display() {

display("");

}

@Override

publicvoid display(String prepend) {

System.out.println(prepend + "draw " + getName());

// display each child element in this Composite

Iterator itr = elements.iterator();

while (itr.hasNext()) {

Abstract a = (Abstract) itr.next();

a.display(prepend + INDENT);

}

}

}

4. Write code, interfaces and/or classes for a problem using a Design Patternnot previously discussed in class or section. One of these: Adapter, Proxy, Mediator, State, or Chain of Responsibility.

Question not given for obvious reasons. It would look like the one above.

There may only be one Design Pattern Question on the test if Rick thinks it would be too much

6.

This server now awaits one client

Client wrote: I am client Server wrote: This server is shutting down.

9. I really think a similar question on the real test will be shorter, it has to be. But still think about paintComponent, draw, a few geom classes, and a timer

public class HangmanPanel extends JPanel implements Observer {

private int wrongs;

public HangmanPanel() {

wrongs = 0;

}

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

// Always draw the gallows and the noose at startup

g2.fill(new Rectangle2D.Double(5, 230, 200, 5)); // base of scaffold

g2.fill(new Rectangle2D.Double(5, 10, 5, 225)); // side of scaffold

g2.fill(new Rectangle2D.Double(5, 5, 80, 5)); // top of scaffold

g2.draw(new Line2D.Double(new Point(80, 10), new Point(80, 30))); // noose

if (wrongs > 0)

g2.draw(new Ellipse2D.Double(70, 30, 20, 20)); // x, y values could be very rough

if (wrongs > 1)

g2.draw(new Line2D.Double(80, 51, 80, 110));

if (wrongs > 2)

g2.draw(new Line2D.Double(80, 70, 50, 90));

if (wrongs > 3)

g2.draw(new Line2D.Double(80, 70, 110, 90));

if (wrongs > 4)

g2.draw(new Line2D.Double(80, 110, 60, 140));

if (wrongs > 5)

g2.draw(new Line2D.Double(80, 110, 100, 140));

public void update(Observable hangmanGame, Object ignored) {

wrongs = ((HangmanModel) hangmanGame).numberOfWrongGuesses();

repaint();

}

7. Check the correct answer with an X, 0.5 pts each

a. All Java keywords are written in lower case.

_X_true __false

b. Does a call to System.gc() force garbage collection to take place?

__ yes always _x_ not always

c. Assume: byte a = 3; byte b = 2; byte c; // byte c was missing from pt

What is the result of c = a + b;

__X__ compile time error Type mismatch: cannot convert from int to byte

____ c == 5

____ c == 3

____ runtime exception

8. Answers in class