C Sc 335 Practice Test 2 Answers

1a) Create an initial list of classes that would do a good job of modeling this system (ANSWER MAY VARY)

Candidate Class Name / Single Responsibility
Customer (or Rentor) / Stores all needed info about any rentor such as name, phone number, credit card number, list of rentals, history, ...
Cashier / Gather all data needed to make and record one transaction. A Cashier also produces a receipt (the transaction report with "typical" information)
Video / Store all necessary data about a physical rental: Customer, UPC, Title, cost to rent ...
VideoCollection / A list of all video titles that can be borrowed (only one record for dozens of copies)
Transaction / Store all needed data for each rental such as borrower, unique video identifier

more possible such as

TransactionLog / Maintain a list of all transactions

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. TBA / TBA
c. TBA / TBA

3. Known design pattern: Composite, 3A) UML diagram. Note: The practice test had an inferior general form of the Composite Design Pattern, if on the test, we’ll use this general form (from WikiPedia):

3A) Draw the 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

5.

This server now awaits one client

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

6. 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) { // Still need to remember MVC

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. Choose the better design and explain why ...

1) b When a method name does not reveal its purpose, change the name of the method. Use the refactoring “Rename Method” to change empty to isEmpty (unless empty does really empty the container). BTW: The C++ STL has many collection classes (stack, queue, list) where empty does not mean empty, it returns true if the stack isEmpty. Also, pop can modify and return as long as it is documented. You could argue that pop should only modify, and some people do.

2) b Whenever there is a public field, make it private and provide accessors. This prevents other object from modifying a field, which would be the worst form of coupling. Use the "Encapsulate Field" refactoring (Eclipse has this refactoring)

3) a The while loop is difficult to read, trace, and understand. And it is longer. The recursive solution can be easier to understand. Use the refactoring Replace iteration with Recursion to make more readable code. BTW: Worried about all of those method calls at runtime? A compiler can make the code to the left run as fast as the code to the right. And you should wait until you profile the code to see if this is a bottleneck

4) b When you have a complicated conditional (if-then-else) statement, use the refactoring "Decompose Conditional". Extract methods from the Boolean condition