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

·  In general, a relationship between two objects occurs when one object ...

1.  sends a message to another object

2.  sends a message to an instance variable

3.  sends a message to an object passed as an argument

4.  sends a message to a reference returned by a message

5.  constructs an object

6.  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)

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() { // b) ______

System.out.println("Construct me");

}

public static final String NAME = "UofA"; // c) __√___

public double f1(double x); // d) __√___

public double f2(double x) { // e) ______

return 2 * x;

}

}

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 thepresence 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


10. Complete an application that converts from Celsius to Fahrenheit and Fahrenheit to Celsius. (16pts)

add(C); // The JTextfield where the Celsius temperature gets entered

add(F); // 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());

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

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

private double units; +2

private String name;

public Student(String name, double units) { +2

this.units = units;

this.name = name;

}

public String getName() { +2

return name;

}

public double getUnits() { +2

return units;

}

public abstract double getTuitionDue(); +2

}

11b) Completely implement class OutOfState (8pts)

public class InState extends Student { +2

public InState(String str, double d) { +3

super(str, d);

}

public double getTuitionDue() { +3

if (getUnits() < 12.0)

return 1000.00 * getUnits() + 150.00;

else

return 12000.00 + 150.00;

}

}

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


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

}

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

}

}

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