1
Solutions for UML Class Diagrams
Chapter 9
Exercise 9.1: Rectangle
Rectangle
width: double
height: double
Rectangle()
Rectangle(width: double, height: double)
getArea(): double
getPerimeter(): double
Constructs a default rectangle.
Constructs a rectangle with the specified width and height.
Returns the area of this rectangle.
Returns the perimeter of this rectangle.
The width of this rectangle (default 1).
The height of this rectangle (default 1).
Exercise 9.2: Stock
Stock
Exercise 9.6: StopWatch
StopWatch
startTime: long
-endTime : long
+StopWatch()
+start(): void
+stop(): void
+getElapsdTime(): long
get methods for all data fields are provided and omitted for
brevity.
2
Exercise 9.7: Account
Account
id: int
-balance: double
annualInterestRate: double
dateCreated: java.util.Date
+Account()
+Account(id: int, balance: double)
+getId(): int
+getBalance(): double
The ID of this account (default 0).
The balance of this account (default 0).
The annual interest rate of this account (default 0).
The date when this account was created.
Constructs a default account.
Constructs an account with the
specified ID and balance.
Returns the ID of this account.
Returns the balance of this account.
Exercise 9.8: Fan
Fan
+SLOW = 1
+MEDIUM = 2
+FAST = 3
speed: int
on: boolean
radius: double
The speed of this fan (default 1).
Indicates whether the fan is on (default false).
The radius of this fan (default 5).
Constant.
Constant.
Constant.
Exercise 9.9: RegularPolygon
3
RegularPolygon
-n: int
side: double
-x: double
-y: double
+RegularPolygon()
Constructs a RegularPolygon with default values.
The number of sides of the polygon (default 3).
The length of a side (default 1).
The x-coordinate for the center of this polygon (default 0).
The ycoordinate for the center of this polygon (default 0).
getter and setter methods for all data fields are provided
and omitted for brevity.
Exercise 9.10: QuadraticEquation
QuadraticEquation
-a: double
-b: double
-c: double
+QuadraticEquation(a: double, b:
Constructs a QuadraticEquation with the specified
Three coefficients for the equation.
get methods for all data fields are provided and omitted for
brevity.
Exercise 9.11: LinearEquation
LinearEquation
-a: double
-b: double
-c: double
-d: double
-e: double
-f: double
The coefficients for the equation.
getter methods for all data fields are provided and omitted
for brevity.
Chapter 10
Exercise 10.1: Time
4
Time
hour: int
minute: int
second: int
The hour for the time.
The minute for the time.
The second for the time.
Exercise 10.3: MyInteger
MyInteger
value: int
+MyInteger(value: int)
+getValue(): int
+isPrime(): boolean
+isPrime(value: int): boolean
An int value for the object.
Constructs a MyInteger object with the specified int value.
Returns the value in this object.
Returns true if the value in this object is prime.
Returns true if a specified int value is prime.
Returns true if the value in a specified MyInteger object is prime.
Exercise 10.4: MyPoint
MyPoint
x: double
y: double
+MyPoint()
+MyPoint(x: double, y: double)
+getX(): double
+getY(): double
+distance(secondPoint: MyPoint): double
+distance(x: double, y: double): double
+distance(p1: MyPoint, p2: MyPoint): double
x-coordinate of this point.
y-coordinate of this point.
Constructs a Point object at (0, 0).
Constructs an object with specified x and y values.
Returns x value in this object.
Returns y value in this object.
Returns the distance from this point to another point.
Returns the distance from this point to (x, y).
Returns the distance between two points.
Exercise 10.8: Tax
5
Tax
-filingStatus: int
-brackets: int[][]
-rates: double[]
-taxableIncome: double
+Tax()
+Tax(filingStatus: int, brackets: int[], rates: double[],
taxableIncome: double)
+getFilingStatus(): int
+setFilingStatus(filingStatus: int): void
Exercise 10.11: Circle2D
Circle2D
-x: double
-y: double
radius: double
+Circle2D()
+Circle2D(x: double, y: double, radius: double)
+getX(): double
+getY(): double
Exercise 10.12: Triangle2D
6
Triangle2D
p1: MyPoint
p2: MyPoint
p3: MyPoint
+Triangle2D()
+Triangle2D(x1: double, y1: double, x2: double, y2:
double, x3: double, y3: double,)
Exercise 10.13: MyRectangle2D
MyRectangle2D
-x: double
-y: double
width: double
height: double
+MyRectangle2D()
+MyRectangle2D(x: double, y: double, width: double,
height: double)
+getX(): double
+setX(x: double): void
+getY():double
+setY(y: double): void
+getWidth(): double
Exercise 10.14: MyDate
7
MyDate
year: int
-month: int
-day: int
The year for the date.
The month for the date.
The day for the date.
8
Chapter 11
Exercise 11.1: Triangle
GeometricObject
Triangle
side1: double
side2: double
side3: double
+Triangle()
+Triangle(side1: double, side2:
double, side3: double)
Three sides of the triangle.
Creates a triangle with default sides (1).
Creates a triangle with the specified sides.
9
Exercise 11.2: Person, Student, Staff, Employee
Person
Student
Employee
Faculty
Staff
Person
name: String
address: String
phone: String
-email: String
+Person()
+Person(name: String, address:
String, phone: String, email:
String)
Student
status: String
+Student()
+Student(name: String, address:
String, phone: String, email:
String)
+getStatus(): String
+setStatus(status: String): void
Employee
office: String
salary: int
dateHired: java.util.Date
+Employee()
+Employee(name: String, address:
String, phone: String, email: String)
+getOffice(): String
Faculty
officeHour: String
rank: String
+Faculty()
+Faculty(name: String, address: String,
phone: String, email: String)
+getOfficeHour(): String
+setOfficeHour(officeHour: String): void
10
Staff
-title: String
+Staff()
+Staff(name: String, address: String,
phone: String, email: String)
MyDate
year: int
month: int
day: int
+Faculty()
Exercise 11.3: Account
Account
CheckingAccount
SavingsAccount
CheckingsAccount
-overdrawnLimit: int
+CheckingsAccount()
+CheckingsAccount(id: int, balance:
double, annualInterestRate: double)
SavingsAccount
+SavingsAccount()
+SavingsAccount(id: int, balance:
double, annualInterestRate: double)
11
Exercise 11.5: Course
Course
courseName: String
students: ArrayList<String>
+Course(courseName: String)
The name of the course.
An ArrayList to store the students for the course.
Creates a course with the specified name.
Exercise 11.10: MyStack
MyStack
+isEmpty(): boolean
+getSize(): int
+peek(): Object
Returns true if this stack is empty.
Returns the number of elements in this stack.
Returns the top element in this stack.
MyArrayList
12
Chapter 13
Exercise 13.1: Triangle
GeometricObject
Triangle
side1: double
side2: double
side3: double
+Triangle()
+Triangle(side1: double, side2:
double, side3: double)
Three sides of the triangle.
Creates a triangle with default sides (1).
Creates a triangle with the specified sides.
Exercise 13.5: GeometricObject
GeometricObject
color: String
filled: boolean
dateCreated: java.util.Date
#GeometricObject()
+getColor(): String
+setColor(color: String): void
+isFilled(): boolean
+setFilled(filled: boolean): void
«interface»
java.lang.Comparable<GeometricObject>
Exercise 13.6: ComparableCircle
13
ComparableCircle
«interface»
java.lang.Comparable<ComparableCircle>
Circle
14
Exercise 13.7: Colorable, Square
Square
-side: double
+Square()
«interface»
Colorable
+howToColor(): void
GeometricObject
Exercise 13.9: Circle
«interface»
Comparable<Circle>
Exercise 13.10: Rectangle
«interface»
Comparable<Rectangle>
15
Exercise 13.11: Octagon
Octagon
side: double
+Octagon()
«interface»
Comparable<Octagon>
+compareTo(o: Octagon): int
GeometricObject
«interface»
Cloneable
Exercise 13.17: Complex
Complex
-a: double
-b: double
+Complex()
+Complex(a: double, b: double)
+Complex(a: double)
+getRealPart(): double
«interface»
java.lang.Cloneable
«interface»
java.lang.Comparable<Complex>