C Sc 335 Fall 10 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 Iterator<Integer> itr = set.iterator();

assertTrue(itr.hasNext());

assertEquals(-1, (int) itr.next());

assertEquals(0, (int) itr.next());

assertEquals(2, (int) itr.next());

assertEquals(5, (int) itr.next());

assertFalse(itr.hasNext());

3. Write code that will print all elements of set above using the for/in loop

// Other answers possible, but try using the enhanced for loop when possible

for (Integer currentInt : set)

System.out.println(currentInt);

4.

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


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

5B) class Abstract

public abstract class Abstract {

private String name;

public Abstract(String name) {

this.name = name;

}

public String getName() {

return name;

}

abstract public void display(String prepend);

}

5C) The Primitive and Composite classes

public class Primitive extends Abstract {

public Primitive(String name) {

super(name);

}

public void add(Primitive primitive) {

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

}

public void display(String prepend) {

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

}

}

class Composite extends Abstract {

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

public static final String INDENT = " ";

public Composite(String name) {

super(name);

}

public void add(Abstract a) {

elements.add(a);

}

public void display() {

display("");

}

@Override

public void 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);

}

}

}

6. No answer to a design pattern not discussed before. I suspect there will be room for only one on the test

7A) b

Why? When the name of a method 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.

7B) b

Why: Whenever there is a public field, make it private and provide accessors. This prevents other object from modifying a field, which is the worst form of coupling. Use the "Encapsulate Field" refactoring (BTW: this refactoring is available in Eclipse)

You do not need to quote the names of the refactorings on the test

7C) a

Why: The while loop is difficult to read, trace, and understand. And it is longer. The recursive solution can be easier to understand and implement. Use the refactoring Replace iteration with Recursion to make more readable code.
-or-

b

Why? We know this particular recursive method makes MANY calls with arguments 0, and 1 that are not needed. If asked in an interview, pick the iterative version even thought the recursive version is more readable and easier to implement. Tests help ensure the iterative version works

If you can justify your answer with a good reason, your answer is correct.

7D) b

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

7D-2) Apply "Rename Method" and have the method return the logical negation, then apply "Reverse Conditional" (switch the if and else)

You do not need to quote the names of the refactorings on the test

7E) a

Why? Whenever a method returns an object that needs to be downcasted by its callers, use the "Encapsulate Downcast" refactoring to move the downcast to within the method. Better yet, use generics

7F) b

Why? The design on the left has one class doing work that should be done by two. Use the "Extract Class" refactoring to create a new class and move the relevant fields and methods from the old class into the new class.

-or-

a

Why? One class isn't doing very much. Move all of its features into another class and delete it. Yes, I just realized the same example shows that both answers should be accepted. Inline class show the opposite is better

Move all its features into another class and delete it.

7G) b

Why? If you have a code fragment that can be grouped together, use Extract Method to turn the fragment into a method whose name explains the purpose of the method.

7H) b

Why? When a method is not used by any other class, use the refactoring "Hide Method" to make the method private. Why: Do not clutter the public interface with methods the user does not need or is not going to be interested in.

7I) b

Why? Magic numbers often don’t reveal their meaning very well (we usually know 3.14, but what is 0.045 (BTW it is the VA state sales tax, I think) If you have a literal number with a particular meaning, create a constant, name it after the meaning, and replace the number with it. Apply " Replace Magic Number with Symbolic Constant".


7J) b Why? There are too many '.'s, too long to understand. Also Because violates the Law of Demeter (see Tuesday's lecture)

8A) Server 8B) Client
This server now awaits one client Server wrote: This server is shutting down
Client wrote: I am a client


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));

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();

}

10. 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