Practice Test 1 - University of Arizona



C Sc 335 Practice Test 1 Section Leader ________ Name ___________________________ 186pts Real test will be shorter1. Write the correct term, either "RESPONSIBILITIES" or "METHODS and DATA" in the blank space (2pts)At the conceptual level, an object is a set of ________________At the implementation level, an object is a set of ________________2. Describe the essence of polymorphism. (3pts)3. List two Java structures that allow for polymorphic messages ____________ ___________ (2pts)4. If you change ArrayList to LinkedList, will the following code still compile? true or false _____ (2pts) ?List<String> list = new ArrayList<String>(); ? list.add("b"); ? list.add("a"); ? list.add("c");? Collections.shuffle(list);5. Place an X by the name of any interfaces in the Java collection framework in the java.util package? (6pts) ___Collection ___ArrayList ___Queue ___Set ___Map __SortedArrayList6. Which goes into the top compartment (rectangle) of a class diagram of a single class? (2pts) ___Attributes(instance variables) ___Class Name ___Operations (methods)7. Can one Java class implement more than one interface? yes or no ________ (2pts)8. What is better, high cohesion or low cohesion? low or high ________ (2pts)9. Will all assertions in the following test method pass? yes or no ______ (2pts) @Test? public void testNext() {? ? List<String> list = new? ArrayList<String>();??? list.add("b");??? list.add("a");??? Iterator<String> itr = list.iterator();??? assertTrue(itr.hasNext());??? assertEquals("b", itr.next());??? assertEquals("a", itr.next());??? assertTrue(itr.hasNext());? }10. Write the output generated by the same code when aNumberMaybe is first "123" and then "NOgOOD" (6pts)String aNumberMaybe = "123";try { System.out.println("--"); double num= Double.parseDouble(aNumberMaybe); System.out.println("oo");}catch (NumberFormatException nfe) { System.out.println("++");}String aNumberMaybe = "NOgOOD";try { System.out.println("--"); double num = Double.parseDouble(aNumberMaybe); System.out.println("oo");}catch (NumberFormatException nfe) { System.out.println("++");}11. Finish this program with a graphical user interface that will allows use to enter text into a field and be told whether or not that text represents a valid number. If the input is not a number, put !!ERROR in the middle. Hint use Double.parseDouble(String) that throws an exception if the argumnet isn't a valid number. (15pts) // Assume all imports existpublic 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; // initialize the JFrame 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);12. In the box, write class MyInteger so its objects can be sorted into their natural ordering with a call to java.util.Collections.sort. Each compareTo message must return the difference in value with a negative value indicating the object is less than the argument. Complete a test method to completely test the compareTo method (12pts)public interface Comparable<T> { public int compareTo(T other);}//////////////////////////////////import static org.junit.Assert.*;import org.junit.Test;public class MyIntegerTest { @Test public void testMyInteger() { }}13. The UofA Bookstore has decided that there is just too much traffic in the bookstore at the beginning of each semester. In an effort to reduce in-house traffic, the bookstore has decided to implement an online storefront where students can purchase all of their books online and just pick them up sometime after they’ve been purchased. You are to do some analysis and come up with a model for the bookstore’s new online front. Details:A student may remove 1 to many items from the shelf and place them into a shopping basketA student should be able to remove items from a shopping basket and place them back on the shelfA student should be able to purchase the items in their shopping basketTo check out, a student must give their shopping basket to the cashier (there is only one cashier)The cashier creates an order that consists of a item, quantity, and a price based on the item’s ISBNIf the CatCard has enough money then the total amount will be deducted and the items will be removed from inventory, a claim check confirmation for the order will be sent to the student’s email addressIf the CatCard funds are insufficient, place all back on the shelfCreate a list of the five most important objects to model this system along with the major responsibility: (10pts)Candidate Object Major Responsibility14. Draw a UML class diagram showing all of your candidate objects and any relationships between them. Show inheritance relationships, interface implementation, or general association such as dependency by drawing a line. Write any multiplicity adornment you can think of. You will likely have 1, and or * in a few places at least. Each classes needs the class name and at least one appropriate method (no attributes needed for a perfect score). (12pts)15. Given class Point and a test driver for class Circle and class Rectangle, implement a properly designed inheritance hierarchy in Java (on the next page). Let Shape be the abstract class. Implement all three classes to the right of the non-inheritance version on the next page. Completely implement all constructors and all methods. Include all instance variables. The Circle and Rectangle classes must behave exactly the same with the inheritance hierarchy as without. The assertions must pass. (20pts)import static org.junit.Assert.*;import org.junit.Test;public class ShapeTest { @Test public void test getArea() { // 10 pixels over, 10 pixels down, radius 2.0 Shape c = new Circle(10, 10, 2.0); asssertEquals(3.14159, c.getArea(), 0.001); // width = 3.25, height = 5.75 Shape r = new Rectangle(40, 10, 3.25, 5.75); sssertEquals(18.6875, r.getArea(), 0.001); // width = 3.25, height = 5.75 Shape r2 = new Rectangle(40, 60, 2, 3); asssertEquals(6.0, r.getArea(), 0.1); }}public class Point { // Use this type private int xPos; private int yPos; public Point(int x, int y) { xPos = x; yPos = y; } public int getX() { return xPos; } public int getY() { return yPos; }}public class Rectangle { private Point upperLeft; private double width; private double height; public Rectangle(int x, int y, double height, double width) { upperLeft = new Point(x, y); width = width; height = height; } public int getX() { return upperLeft.getX(); } public int getY() { return upperLeft.getY(); } public double getArea() { return width * height; }}public class Circle { private Point upperLeft; private double radius; public Circle(int x, int y, double diameter) { upperLeft = new Point(x,y); radius = diameter / 2; } public int getX() { return upperLeft.getX(); } public int getY() { return upperLeft.getY(); } public double getArea() { return Math.PI*Math.pow(radius, 2); }}Using good Object-Oriented Design, completely implement the hierarchy here 16a) Using the Uno inheritance hierarchy, write a UML diagram of this hierarchy to the right. Do not include the object class. The diagram must include all class names, methods for each class, and arrows to indicate which class extends which. (6pts)///////////////////////////////////////// HIERARCHY WILL BE ON SEPARATE PIECE OF PAPERpublic class Uno { public void one() { System.out.println("Uno 1"); } public void four() { System.out.println("Uno 4"); }}///////////////////////////////////public class Dos extends Uno { public void one() { System.out.println("Dos 1"); super.one(); } public void two() { System.out.println("Dos 2"); }}///////////////////////////////////public class Tres extends Uno { public void two() { System.out.println("Tres 2"); three(); } public void three() { System.out.println("Tres 3"); }}///////////////////////////////////public class Cuatro extends Tres { public void one() { super.two(); } public void three() { System.out.println("Cuatro 3"); }}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 two threefour16c) (Same as Section Handout) Write the output generated by each statement that has no compile time or runtime errors. If the statement would cause an error, write “CT” for “compile time error" or RE for "runtime error" to indicate when the error would be detected. (18pts, 3ea) Object first = new Uno(); Dos second = new Dos(); Uno third = new Tres(); Tres fourth = new Cuatro(); ((Uno) first).one(); // ______________________ third.three(); // ______________________ fourth.one(); // ______________________ fourth.two(); // ______________________ ((Tres) second).two(); // ______________________ ((Cuatro) third).one(); // ______________________17a) (Same as Section Handout)Using the Foo inheritance hierarchy, write a UML diagram of this hierarchy to the right. Do not include the object class. The diagram must include all class names, methods for each class, and arrows to indicate which class extends which. (6pts)//////////////////////////////////// HIERARCHY WILL BE ON SEPARATE PIECE OF PAPERclass Foo { public void one() { System.out.println("Foo1"); } public void three() { System.out.println("Foo3"); }}////////////////////////////////////class Bar extends Foo { public void one() { System.out.println("Bar1"); } public void two() { System.out.println("Bar2"); one(); }}////////////////////////////////////class Baz extends Foo { public void one() { System.out.println("Baz1"); super.three(); } public void two() { System.out.println("Baz2"); }}////////////////////////////////////class Goo extends Bar { public void one() { super.one(); System.out.println("Goo1"); } public void two() { System.out.println("Goo2"); } public void three() { super.two(); System.out.println("Goo3"); }}17b) 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 two three17c) For each blank line, write one of these three choices 16pts, 2 pts each) The actual output if the code compiles and runs. Use / to indicate a new line. CT (compile time) if an error would occur at compile time, or RE (runtime error) if an error would occur while the program is running Foo a = new Foo(); Foo b = new Baz(); Foo c = new Bar(); Bar d = new Goo(); Object e = new Baz(); b.one(); _________________________ c.one(); _________________________ c.two(); _________________________ e.one(); _________________________ ((Baz)c).two(); _________________________ ((Goo)d).three(); _________________________ ((Foo)e).one(); _________________________ ((Goo)((Foo)e)).one(); _________________________ 17. The Command design pattern encapsulates the concept of a command into an object. You can save it for later execution. In the following code, six commands of two different types are constructed and saved in a Vector. Later on, the execute message is sent to all six Command objects. The two classes that you will be asked to write are highlighted in boldface: Command and BorrowCommand. To save time, you do not need to write class ReturnCommand.// Have two borrowers and three books to store in the two Command objectsBorrower joe = new Borrower("Joe");Borrower kim = new Borrower("Kim");Book b0 = new Book("Pascal");Book b1 = new Book("Java");Book b2 = new Book("c++");Command c1 = new BorrowCommand(joe, b0); // Construct a Commmand so Joe borrows PascalCommand c2 = new BorrowCommand(joe, b1); // Construct a Commmand Joe borrows JavaCommand c3 = new BorrowCommand(kim, b2); // ...Command c4ThatFails = new BorrowCommand(kim, b0); // Nothing will happen later with execute()OutputJoe has [] attempts to BORROW Pascal[Joe has [Pascal], Kim has []]Joe has [Pascal] attempts to BORROW Java[Joe has [Pascal, Java], Kim has []]Kim has [] attempts to BORROW c++[Joe has [Pascal, Java], Kim has [c++]]Kim has [c++] attempts to BORROW Pascal[Joe has [Pascal, Java], Kim has [c++]]Joe has [Pascal, Java] attempts to RETURN Pascal[Joe has [Java], Kim has [c++]]Joe has [Java] attempts to RETURN c++[Joe has [Java], Kim has [c++]]Command c5 = new ReturnCommand(joe, b0); Command c6Fails = new ReturnCommand(joe, b2);List commandList = new Vector();commandList.add(c1);commandList.add(c2);commandList.add(c3);commandList.add(c4ThatFails); // already outcommandList.add(c5);commandList.add(c6Fails); // Doesn't havefor(int j = 0; j < commandList.size(); j++) { Command ref = (Command)commandList.get(j); ref.execute(); System.out.println(borrowerList); System.out.println();}18a) Write interface Command as if it were in its own file 18b) Complete class BorrowCommand as if it were in its own file. The execute method adjusts the Borrower and the Book if everything is all right. However, if the Book is already borrowed, execute returns false. // BorrowCommand attaches a specific book copy to a specific subscriber, both, the book// copy’s and the subscriber’s objects are supplied by the programmer. public class BorrowCommand implements Command { protected Borrower theBorrower; protected Book theBook; public BorrowCommand(Borrower aBorrower, Book aBook) { theBorrower = aBorrower; theBook = aBook; } // When the Borrow button is clicked, execute this public boolean execute() { System.out.println(theBorrower + " attempts to BORROW " + theBook); } public boolean undo() { // a borrow by returning System.out.println(theBorrower + " undo a Borrow" + theBook); ................
................

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

Google Online Preview   Download