Midterm: Next Week
-open books/notes
-no pc allowed
-material covered:
-everything discussed in class
-take-home assignments
- project assignments
Recommend problems to answer when studying for exam:
find examples of classes where these two keywords (private, void) can appear in the class declaration (classes with return types)
send solution of problem 10 for feedback
Show a class diagram for problem #5.
#6.iii, 6.iv
What are the characteristics of good unit tests?
EXAMPLE EXAM PROBLEMS
1. Which of these pairs represents the IS-A relationship?
> (i) duck/bird
(ii) bird/fly
beak/bird
bird/nest
2. Which Java technology best supports the IS-A relationship
> Inheritance
Access restriction
strong typing
garbage collection
3. Which Java keywords cannot appear in a class declaration?
extends
> protected
void (classes can have return types)
private
4. Which of these code fragments represents a HAS-A relationship between Foo and Bar?
class Foo extends Bar {...}
class Foo implements Bar {...}
> class Foo {private Bar;}
class abstract class Bar extends Foo {...}
5. You are designing an Object-Oriented program to help people plan an evening out. It is proposed that the following classes are used:
class Address { }
class Business { } //anything that appears in the yellow pages
class City { }
class Restaurant { }
class Theater { }
Add inheritance and instance variables to show the IS-A and HAS-A relationships. Do not introduce any additional classes. Do not introduce any additional classes.
((a variation would be to draw a class diagram for these entities))
Ans:
// Restaurant and Theater extend Business
class Restaurant extends Business {
}
class Theater extends Business {
}
class Business {
Address bizAddress;
}
class City {
Business someBusiness;
}
6. True/False. Circle the correct answer for each of the following statements.
T (i) If class B is a subclass of A, then B's constructor can explicitly call A's constructor using the keyword 'super'.
T (ii) One can declare a variable of an interface type, but cannot create an object of that type.
(iii) The private fields of a class are visible within the file in which the class is defined.
(iv) A non-static method cannot refer to any static fields of the class.
7. The following is declared within the class A.
public static void main(String[] s)
Which statement is correct?
(i) Since main is static, then no object of class A can be created within main.
> (ii) Main is required to be static since the program cannot an object of class A before main() is executed
(iii) The array s must be initialized in A's constructor
(iv) A compile-time error will result since the string array must be called argc. ((no, you can call it anything you want))
8. Write a ThreeWayLamp class that models the behavior of a lamp with a three-way bulb. (off, med, high). Each time the switch is activated, the bulb goes to the next state.
class ThreeWayLamp {
private int state;
void switch() {
state = (state + 1) % 3;
// print message here
}
ThreeWayLamp() {
state = 0; //new lamp is off
}
}
9. What are the characteristics of good unit tests?
10. Draw a class diagram for the following
"A building can be specialized in many ways; in this example we show HighRise and TownHouse. A building is owned by one or more Persons. A building can be queried for its creation date with the message getCreationdate(). A HighRise response to the hasElevator() message. A Person can own zero or more buildings. A Person has an age and a name. Man and Woman are the only possible types of Person. A building has one or two front doors and zero or more back doors, all of which are Doors. At any point in time a Building can contain zero or more Persons. A building must be able to reference its doors, but not the other way around. "
11. Draw a sequence diagram for two Subscriber objects s1 and s2. . .
Behavioral Patterns
Ex: assume a database of all IIT student grades and the staff of cs445 wishes to view the grades of students in cs445.
They could write a SpreadSheetView class that pulls data from the DB and displays it.
HW1 HW2 HWs
Anna 45 85 80
Joe 95 90 85
Gus 90 100 95
The code to communicate between the view and the DB:
interface GradDMViewer {
void update(String course, String name, String assignment, int mark);
}
Where new grade (mark) information is available (e.g. new grades posted on conections to existing ones) the grade DB must commuincate this to the view.
Let's suppose that Anne's mark in HW1 changed to 100. The DB code must make a call to spreadSheetView.update().
EX:
SpreadSheetView ssv = new SpreadSheetView();
. . .
ssv.update("cs445", "Anne", "HW1", "100");
Then the view redisplays itself.
HW1 HW2 HWs
Anna 100 85 80
Joe 95 90 85
Gus 90 100 95
We want to add a bargraph view for averages.
# # #
*
* *
* * *
* * *
* * *
A J G
If we want to maintain both view then the code will look like this.
SpreadSheetView ssv = new SpreadSheetView();
BarGraphView bgv = new Bargraph();
ssv.update("cs445","Anne","HW1", 100);
bgv.update("cs445","Anne", "HW1",100);
Q: what if i want a pie chart?
A: need to change the code again.
! This is wrong; The code should be reusable without having to change implementation.!
leads to the Observer Pattern (which you'll find all the time) which addresses this issue.
Observer Pattern
Instead of hard-coding the views that need to be notified on change, the database can maintain a list of observers (objects) that need to be notified about a change.
Vector observers = new Vector();
. . .
for(int i = 0; i < v =" (GradeDBViewer)" style="font-weight: bold;">Pattern Functionality) Interface
---------------------------------------
--------------------------------------------------
Adapter same different
--------------------------------------------------
Decorator different same
--------------------------------------------------
Proxy same same
--------------------------------------------------
EX: Adapter some code that works on Rectangle objects and calls their scale method.
interface Rectangle {
void scale(float factor); //grow/shrink by a factor
float area();
float circumference();
}
class MyClass {
void myMethod(Rectangle r) }
. . .
r.scale(2);
. . .
}
}
Let's assume another class of non-scaleable rectangles, call 'em NonScaleRectangle, which lacks
the scale method but has the other methods in Rectangle and in addition, other methods such as setWidth, setHeight, etc.
class NonScaleRectangle {
void setWidth(float w) { . . . }
void setHeight(float h) { . . . }
. . .
}
Problem: You may wish to use objects of this type (e.g. improve performance, interoperability). But you cannot use NonScaleRectangle directly because the interface is different from Rectangle.
Solution: Write an Adapter
(i) subclassing
class ScaleRectangle extends NewScaleRectangle implements Rectangle {
void scale(float factor) {
setWidth(factor*getWidth( ) );
setHeight(factor*getHeight( ) );
}
}
(ii) Composition / Forwarding
class ScaleRectangle2 implements Rectangle {
NonScaleRectangle r;
ScaleRectangle2( NonScaleRectangle r) {
this.r = r;
}
void scale (float factor) {
setWidth(factor*getWidth( ) );
setHeight(factor*getHeight( ) );
}
float area() {
return r.area( );
}
}
Decorator Pattern
Adapter changes interface without adding functionality.
Decorator extends functionality with the same interface.
-Decorator changes some instances of a class such that they have extra functionality.
-Decorator sounds like subclassing, but it's NOT (subclassing modifies functionality of ALL instances ??)
EX: Assume a Window interface (for a window manager) and a BorderedWindows interface. BorderedWindow draws a border outside the window.
interface Window {
Rectangle bounds(); //rectangle bounding the windows
void draw(Screen s); //draw it on a screen
. . .
}
class WindowImpl implements Window {
. . .
}
(i) decoration by subclassing
class BorderedWindow1 extends WindowImple {
void draw(Screen s) {
supper.draw(s);
bounds().draw(s);
}
}
(ii) Composition / Forwarding
class BorderedWindow2 implements Window {
Window innerWindow;
BorderedWindow2(Window innerWindow) {
this.innerWindow = innerWindow;
}
void draw(Screen s){
innerWindow.draw(s);
innerWindow.bounds().draw(s);
}
. . .
}
ADVICE: Do not try forcing a pattern until you have a real need for it. With experience, you;ll notice problems and ask yourselves "hmm, how have other people solved this problem." It will come naturally with experience, but you should be aware of this.
Thursday, March 1, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment