University of Arizona



Answers1. 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 __SortedArrayList6. 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? yes or no __YES NO____ (2pts)10. Write the output generated by the same code when aNumberMaybe is first "123" and then "NOgOOD" (6pts)String aNumberMaybe = "123"; -- ooString aNumberMaybe = "NOgOOD"; -- ++11. Answer begins on top of next page// imports weren’t required in the answerpublic class NumberFrame extends JFrame { public static void main(String[] args) { new NumberFrame().setVisible(true); } private JLabel heading = new JLabel("Enter a number below"); private JLabel myLabel; private JTextField inputField; public NumberFrame() { 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(ActionEvent ae) { try { double num = Double.parseDouble(inputField.getText()); myLabel.setText("You entered: " + num); } catch (NumberFormatException nfe) { myLabel.setText("!!ERROR"); } } }}12@Testpublic void testMyInteger() { MyInteger a = new MyInteger(2); MyInteger b = new MyInteger(5); assertEquals(-3, pareTo(b)); assertEquals(3, pareTo(a)); assertEquals(0, pareTo(a));}public class MyInteger implements Comparable<MyInteger> { private int val; public MyInteger(int value) { val = value; } public int compareTo(MyInteger other) { return this.val - other.val; }13. Candidate Objects and Major ResponsibilityBookStore: Coordinates Activities (Check out student, collect payment, update inventory)Student: Keeps track of CatCard number, address, sales confirmation orderShoppingBasket: Keeps track of desired purchasesItem: one the books that can be purchasedShelf: Shows books for purchase, with browse/search Inventory: Keeps inventory levels of all items14. 18ptsGrading 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 Has two or more reasonable multiplicity adornments (1 or * usually are possible)One Answer others are possible (a few word processor additions were made to get 100% on this question)onHand: InventorymyBasket: Shopping Basket15. Completely implement the hierarchy here: Shape, Circle, and Rectanglepublic abstract class Shape { // All shapes have an upper left corner initialized by this constructor private Point p; Shape(int x, int y) { p = new Point(x, y); } // All shapes can find the x and y location public int getX() { return p.getX(); } public int getY() { return p.getY(); } // Shapes compute their area in different ways. Let the sublclasses implement getArea abstract double getArea();}public class Rectangle extends Shape { private double my_width; private double my_height; public Rectangle(int x, int y, double height, double width) { super(x, y); my_width = width; my_height = height; } public double getArea() { return my_width * my_height; }}public class Circle extends Shape { private double my_radius; public Circle(int x, int y, double diameter) { super(x, y); my_radius = diameter / 2; } public double getArea() { return Math.PI * Math.pow(my_radius, 2); }}16a) Using the Uno inheritance hierarchy, write a UML diagram of this hierarchy…. Uno one() four() Dos one() two() Tres two() three()Cuatro one() three()16b) Using the Uno hierarchy, complete the table below to indicate the behavior of all messages. Use / to indicate a new line. If an instance will not understand the message, write X instead of output. (6pts) class namesmethod namesUnoDosTresCuatroone Uno 1Dos 1 / Uno 1Uno 1Tres 2 / Cuatro 3two XDos 2Tres2 / Tres 3Tres 2 / Cuatro 3threeXXTres 3Cuatro 3fourUno 4Uno 4Uno 4Uno 416c)? ? ((Uno) first).one(); ? // _______Uno 1_____? ?? ? third.three(); ? ? ? ? // ______CE_________ ???? ? fourth.one(); ? ? ? ? ?// __Tres 2 / Cuatro 3? ? fourth.two(); ? ? ? ? ?// __Tres 2 / Cuatro 3? ? ((Tres) second).two(); // _____CE____________? ?((Cuatro) third).one(); // __RE_____ ?// Tres cannot be cast to Cuatro17a) UML diagram17b) Using the Foo hierarchy, complete the table below to indicate the behavior of all messages. Use / to indicate a new line. If an instance will not understand the message, write X instead of output. (6pts) class namesmethod namesFooBarBazGooone Uno 1Bar1Baz 1 / Foo3Bar1 / Goo1two XBar2 / Bar 1Baz2Goo2threeFoo3Foo3Foo3Bar2 / Bar1 / Goo1 / Goo317c) For each blank line, write one of these three choices 16pts, 2 pts each) Foo a = new Foo(); Foo b = new Baz(); Foo c = new Bar(); Bar d = new Goo(); Object e = new Baz();? ?b.one();???????????????? _________ Baz1 / Foo3____???? c.one();????? ????????????__________ Bar1__________???? c.two();???????????????? ___________CE____________???? e.one();???????????????? ___________CE____________???? ((Baz)c).two();????????? __RE (ClassCastException)_???? ((Goo)d).three();??????? Bar2 / Bar1 / Goo1 / Goo3_???? ((Foo)e).one();????????? ____Baz 1 / Foo3__________???? ((Goo)((Foo)e)).one();?? __RE (ClassCastException)_?1718a) public interface Command { public boolean execute(); public boolean undo(); }1718b)public class BorrowCommand implements Command { protected Borrower theBorrower; protected Book theBook; public BorrowCommand(Borrower aBorrower, Book aBook) { theBorrower = aBorrower; theBook = aBook; } // When the Return button is clicked, execute this code public boolean 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; } public boolean 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; }} ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download