Answers and grading criteria to CSc 335 Test 1, 9-Oct-2012 1. What is your favorite ice cream? ______Anything ______________ (4pts) 2. Describe one advantage to developing software using an object-oriented style. Do not just list an advantage, also describe why it is an advantage. (4pts) Here are several possible answers (others were accepted too) With an OO approach, we can model systems at a more real world level using abstractions like an object. This leads to a more understandable design. Encapsulation of data with methods reduces the amount data that must be sent around from one function to another. This helps protect data from accidental modification. Inheritance allows us to capture common behavior amongst many types. This allows us to write a method once instead of repeating the method. OO design results in stand-alone module that provide a service. When maintenance is needed, we only need to modify one or two of the modules (classes) without affecting the others. Polymorphism allows us to treat many types as the same type. This allows us to program to an interface rather than and implementation. And the gang of 4 say that is a good thing 3. List three object relationships (there are six-- remember cars and gas stations?). Notes: Inheritance, extends, implements are class relationships and could result in 0 points out of 6. "Depends on" is to general to count for any points You can't get credit for both "composes other objects" and "aggregates other obects" (only one counts). 1. 2. 3. 4. 5. 6. In general, a relationship between two objects occurs when one object ... sends a message to another object sends a message to an instance variable sends a message to an object passed as an argument sends a message to a reference returned by a message constructs an object send message to a reference shared by other objects 4. Answer true or false to the following questions a) In Java, one class can extend one class and implement several interfaces <true or false>? _true_ (2pts) b) In Java, one class can extend more than one class <true or false>? __false__ (2pts) c) The Single Responsibility Principle means that large object-oriented software systems should have only one class that is responsible for everything <true or false>? _false_ (2pts) 1 5. Write a check-mark √ in the comment for each bit of code a..e that can exist in an interface. (10pts, 2each) public interface Sample { public int i; // a) ______ public Sample() { System.out.println("Construct me"); } // b) ______ public static final String NAME = "UofA"; // c) __√___ public double f1(double x); // d) __√___ public double f2(double x) { return 2 * x; } // e) ______ } 6. What goes into the bottom compartment (rectangle) of a class diagram? Check one only. (2pts) ___Attributes (instance variables) ___Class Name __x _Operations (methods) 7. Which is the better design, a) a high degree of cohesion or b) a low degree of cohesion? __a___? (2pts) 8. Write the output generated by the following code (assume all imports are present). (8pts) Queue<Integer> numberQ = new LinkedBlockingQueue<Integer>(40); numberQ.add(3); numberQ.add(2); numberQ.add(5); numberQ.add(numberQ.remove()); System.out.println(numberQ.peek()); // a. ____________ System.out.println(numberQ.remove()); // b. ____________ System.out.println(numberQ.size()); // c. ____________ for (Integer current : numberQ) { System.out.print(current + " : " + current + " : "); // d._____________________________ } #8 1 pt for each of the 7 integers 1 pt for the presence of :'s a) 2 b) 2 c) 2 d) 5:5:3:3: #9 1 point for each correct value (-1 if one line had 12dot3 rather than 0.0 for example) Good 12.3 Ugly 12.3 Bad 0.0 Ugly 0.0 2 10. Complete an application that converts from Celsius to Fahrenheit and Fahrenheit to Celsius. (16pts) add(C); add(F); // The JTextfield where the Celsius temperature gets entered // The JTextfield where the Fahrenheit temperature gets entered // Layout of the GUI is now complete, but you need more code here: C.addActionListener(new CListener()); #10 16 pts total F.addActionListener(new FListener()); } // End constructor private class CListener implements ActionListener { F.setText("" + (9.0 / 5.0 * Double.parseDouble(C.getText()) + 32)); } } private class FListener implements ActionListener { C.setText("" + (5.0 / 9.0 * (Double.parseDouble(F.getText()) - 32))); 3 added listener to C in the constructor (or private helper) 3 added listener to F in the constructor (or private helper) 5 code in CListener 1 got text 1 parsed it 1 formula perfect 2 setText (needs "") 5 code in FListener 1 got text 1 parsed it 1 formula perfect 2 setText (needs "") 11. Correctly using inheritance, polymorphism, and good Object-Oriented design, build an inheritance hierarchy in Java code so the following assertions compile and pass. You will need two classes and three methods (to save time, you will not be implementing a class for InState Students) 11a) Completely implement class Student (12pts) public abstract class Student { +2 +2 private double units; private String name; public Student(String name, double units) { +2 this.units = units; this.name = name; } public String getName() { return name; } +2 public double getUnits() { +2 return units; } public abstract double getTuitionDue(); +2 } 3 11b) Completely implement class OutOfState (8pts) public class InState extends Student { +2 public InState(String str, double d) { super(str, d); } +3 11a) Student 12 pts 2 class heading -1 if not abstract 2 instance variables, -1 if not private 2 constructor correct 2 getName implemented correctly 2 getUnits implemented correctly 2 getTuitionDue is present and abstract 11b) OutOfState 2 class heading -1 if extends missing 3 constructor correct, uses super(name, units) 3 getTuitionDue implemented correctly public double getTuitionDue() { +3 if (getUnits() < 12.0) return 1000.00 * getUnits() + 150.00; else return 12000.00 + 150.00; } Special case: Three students did not implement an inheritance hierarchy but had code that compiled. Rick gave them the half right score of 10/20 although it could have been 0/20 } 12. Write the output generated by each statement that has no compile time or runtime errors. If the statement ///////////////////////////////////////// a) first.one(); CE ///////////////////////////////////////// b) second.one(); Dos 1 / Dos 2 ///////////////////////////////////////// c) second.three(); CE ///////////////////////////////////////// d) ((Cuatro) fourth).three(); Cuatro 3 / Uno 1 ///////////////////////////////////////// e) ((Dos) second).two(); Dos 2 ///////////////////////////////////////// f) ((Tres) second).three(); RE Dos cannot be cast to Tres ///////////////////////////////////////// g) ((Dos) fourth).two(); RE Cuatro cannot be cast to Dos ///////////////////////////////////////// h) ((Tres) fifth).one(); Tres 1 Dos 1 Tres 2 4 #12 All or none except the last one that has three lines of output, -2 in not perfect but has 1 or 2 correct lines 13a) Use the UML diagram to complete class Approver. (13 pts) public abstract class Approver { protected String name; protected Approver successor; // or write new accessorss public Approver(String name) { this.name = name; } public void setSuccessor(Approver successor) { this.successor = successor; } public abstract void processRequest(PurchaseRequest request); } #13a) Approver 13 pts 2 class is abstract -2 if missing abstract 2 instance variables, -1 if not private 2 constructor correct 3 setSuccessor implemented correctly 2 processRequest heading is correct and abstract #13b) Director 8 pts 2 class heading -1 if missing extends 3 constructor correct , uses super -2 is super not used. 5 processRequest is implemented correctly (no need to check for null approver). Ways to lose a point 1-5 points include (but are not limited to): 13b) Use the UML diagram to complete class Director (do NOT write the other two subclasses) (10 pts) public class Director extends Approver { public Director(String name) { super(name); } public void processRequest(PurchaseRequest request) { if (request.getAmount() < 10000.0) System.out.println(name + " approved " + request.getTitle()); else if (successor != null) successor.processRequest(request); } -1 heading error -1 accessing private IVs -1 Missing () in two or more places -1 output incorrect -1 wrong comparison -2 no comparison -2 incorrect call to processRequest } BTW this.name = name; instead of super(name) results in this compiletime error: Implicit super constructor Approver() is undefined. Must explicitly invoke another constructor 5