Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COMP5028 Lab 10 (Week 11) Sample Solutions 
 
1. The following diagram shows some relationships between classes. Produce a code 
skeleton that implements the design. Be careful not to add any extra details that are not 
shown in the diagram. 
 
Sample Solution in Java 
 
interface Customer {} 
class Person implements Customer {} 
abstract class BankAccount { 
    private Customer owner; 
} 
class DepositAccount extends BankAccount {} 
 
 
2. The following diagram shows a design for sharing states amongst many objects. Create 
code that implements this design. 
 
 
Sample Solution in Java 
 
You’ll notice that in the LockState class I’ve filled out a couple of extra details – namely 
the final keywords and internal locked variable. We’ll cover this in lectures in Week 12. 
 
class LockState { 
    public static final LockState LOCKED = new LockState(true); 
    public static final Lockstate UNLOCKED = new LockState(false); 
    private Boolean locked; 
    private LockState(Boolean locked) { 
        this.locked = locked; 
    } 
    public String getState() { 
        // return locked as String 
    } 
} 
 
LockState
+LOCKED[1]: LockState
+UNLOCKED[1]: LockState
-LockState(locked: Boolean)
+getState(): String
Lock
-status: LockState
+Lock(initial: LockState)
+getStatus(): String
<>
Customer
BankAccount
DepositAccount Person
owner
class Lock { 
    private LockState status; 
    public Lock(LockState initial) { 
        status = initial; 
    } 
    public String getStatus() { 
        return status.getState(); 
    } 
} 
 
3. The following design shows part of our system for drawing shapes from a previous 
assignment. Create code that implements the skeleton of this design. Can you improve the 
design at all? Assume that there is an implementation of Line.draw() already, i.e. you can 
create the method signature but not fill in the details of how to do the actual drawing. 
 
Sample Solution in Java 
 
One way to improve the design is to realise that Triangle and Rectangle can use lines to 
draw themselves. Here’s a revised design. Note that there’s lots of different ways you 
could manage when lines are created and drawn. (Some of this hinges on 
mutable/immutable object design decisions.) 
 
 
+draw()
Shape
+draw()
-point1 : Point
-point2 : Point
Line
-x : int
-y : int
Point
+draw()
-topLeft : Point
-bottomRight : Point
Rectangle
+draw()
-point1 : Point
-point2 : Point
-point3 : Point
Triangle
Then the code becomes: 
 
abstract class Shape { 
    public abstract void draw() {} 
} 
 
class Point { 
    private int x, y; 
} 
 
class Line extends Shape { 
    private Point point1, point2; 
    public Line(Point point1, Point point2) { 
        this.point1 = point1; 
        this.point2 = point2; 
    } 
    public void draw() {//implementation exists!-see question above} 
} 
 
class Triangle extends Shape { 
    private Point point1, point2, point3; 
    private Line line1, line2, line3; //lines are internally managed to 
                                      //ensure constraints 
    public void draw() { 
        line1 = new Line(point1, point2); 
        line2 = new Line(point2, point3); 
        line3 = new Line(point1, point3); 
        line1.draw(); 
        line2.draw(); 
        line3.draw(); 
    } 
} 
 
class Rectangle extends Shape { 
    private Point topLeft, bottomRight; 
    private Line top, left, bottom, right; ; //lines are internally 
                                             //managed to 
                                             //ensure constraints 
    public void draw() { 
        //from topLeft and bottomRight calculate topRight and bottomLeft 
        //draw the four lines as per the way that it’s done in Triangle 
    } 
} 
+draw()
Shape
+draw()
-point1 : Point
-point2 : Point
Line
-x : int
-y : int
Point
+draw()
-topLeft : Point
-bottomRight : Point
Rectangle
+draw()
-point1 : Point
-point2 : Point
-point3 : Point
Triangle
3
4