Answers

1. At the conceptual level, an object is a set of __RESPONSIBILITIES__

At the implementation level, an object is __METHODS and DATA__

2. The same method or interface may be realized in different ways. The actual method that executes is determined at runtime and is dependent on the type. Different classes can be treated as the same type: Comparables or ActionListeners for example.

3. __inheritance__ ___interfaces__ (2pts)

4. _true_ (2pts)

5. Place an X by the name of any interfaces in the Java collection framework in the java.util package? (6pts)

_X_Collection ___ArrayList _X_Queue _X_Set _X_Map __SortedArrayList

6. Which goes into the top compartment (rectangle) of a class diagram of a single class? (2pts)

___Attributes(instance variables) __X_Class Name ___Operations (methods)

7. Can one Java class implement more than one interface? yes or no ___YES_____ (2pts)

8. What is better, high cohesion or low cohesion? low or high ___HIGH_____ (2pts)

9. Will all assertions in the following test method pass? yesor no __YES____ (2pts)

10. Write the output generated by the same code when aNumberMaybe is first "123" and then "NOgOOD" (6pts)

String aNumberMaybe = "123";
--
oo / String aNumberMaybe = "NOgOOD";
--
++

11. Answer begins on top of next page

// imports weren’t required in the answer

public class NumberFrame extends JFrame {

public static void main(String[] args) {

newNumberFrame().setVisible(true);

}

privateJLabel heading = new JLabel("Enter a number below");

privateJLabelmyLabel;

privateJTextFieldinputField;

publicNumberFrame() {

setTitle("Numbers");

setSize(170, 125);

setDefaultCloseOperation(EXIT_ON_CLOSE);

Container cp = this.getContentPane();

cp.setLayout(null);

heading.setLocation(0, 0);

heading.setSize(140, 20);

myLabel = new JLabel("Nothing yet ...");

myLabel.setLocation(30, 30);

myLabel.setSize(140, 20);

inputField = new JTextField();

inputField.setSize(170, 20);

inputField.setLocation(0, 72);

cp.add(heading);

cp.add(myLabel);

cp.add(inputField);

NumberListener listener = new NumberListener();

inputField.addActionListener(listener);

}

private class NumberListener implements ActionListener {

public void actionPerformed(ActionEventae) {

try {

doublenum = Double.parseDouble(inputField.getText());

myLabel.setText("You entered: " + num);

}

catch (NumberFormatExceptionnfe) {

myLabel.setText("!!ERROR");

}

}

}

}

12

@Test
publicvoidtestMyInteger() {
MyInteger a = newMyInteger(2);
MyInteger b = newMyInteger(5);
assertEquals(-3, a.compareTo(b));
assertEquals(3, b.compareTo(a));
assertEquals(0, a.compareTo(a));
} / publicclassMyInteger
implements Comparable<MyInteger> {
privateintval;
publicMyInteger(int value) {
val = value;
}
publicintcompareTo(MyInteger other) {
returnthis.val - other.val;
}

13. Candidate Objects and Major Responsibility

BookStore: Coordinates Activities (Check out student, collect payment, update inventory)

Student: Keeps track of CatCard number, address, sales confirmation order

ShoppingBasket: Keeps track of desired purchases

Item: one the books that can be purchased

Shelf: Shows books for purchase, with browse/search

Inventory: Keeps inventory levels of all items

14.18pts

Grading Criteria you should consider for the test

+ 4 Has the same classes as candidate objects

+2 Rectangles around each class

+4 Each class has at least one operation

+4 Almost every class has at least one or more attributes (one is enough)

+2 Reasonable associations (lines where a solid line is a dependency, dotting not required) exist

+2 Hastwo or more reasonable multiplicityadornments (1 or * usually are possible)

One Answer others are possible(a few word processor additions were made to get 100% on this question)

15.Completely implement the hierarchy here: Shape, Circle, and Rectangle
public abstractclassShape {

// All shapes have an upper left corner initialized by this constructor

privatePointp;

Shape(intx,inty) {

p=newPoint(x,y);

}

// All shapes can find the x and y location

publicintgetX() {

returnp.getX();

}

publicintgetY() {

returnp.getY();

}

// Shapes compute their area in different ways. Let the sublclassesimplement getArea

abstractdoublegetArea();

}

public classRectangleextendsShape {

privatedoublemy_width;

privatedoublemy_height;

publicRectangle(intx,inty,doubleheight,doublewidth){

super(x,y);

my_width=width;

my_height=height;

}

publicdoublegetArea(){

returnmy_width*my_height;

}

}

public classCircleextendsShape {

privatedoublemy_radius;

publicCircle(intx,inty,doublediameter){

super(x,y);

my_radius=diameter/2;

}

publicdoublegetArea(){

returnMath.PI*Math.pow(my_radius,2);

}

}

16a) public interface Command {

publicboolean execute();

publicbooleanundo();

}

16b)

public class BorrowCommand implements Command {

protected Borrower theBorrower;

protected Book theBook;

publicBorrowCommand(Borrower aBorrower, Book aBook) {

theBorrower = aBorrower;

theBook = aBook;

}

// When the Return button is clicked, execute this code

publicboolean execute( ) {

System.out.println( theBorrower + " attempts to BORROW " + theBook );

if(!theBook.isAvailable( ) ) //The book is not available. Cannot borrow it.

return false;

theBook.borrowBook( );

return true;

}

publicboolean undo( ) { // a return

System.out.println(theBorrower + " undo a Borrow" + theBook);

if (theBook.isAvailable()) //The book is not borrowed. Cannot return it.

return false;

// The borrower should no longer hold the returned book

if (!theBorrower.removeBook(theBook)) //The book is not borrowed by this subscriber.

return false;

theBook.returnBook();

return true;

}

}

1