You are on page 1of 7

Solutions for UML Class Diagrams

Chapter 9

Exercise 9.2:
Fan

-speed: int The speed of this fan (default 1).


-on: boolean Indicates whether the fan is on (default false).
-radius: double The radius of this fan (default 5).

+Fan() Constructs a fan with speed 1, fan off, radius 5.


+getSpeed(): int Returns the speed of this fan.
+setSpeed(speed: int): void Sets a new speed for this fan.
+isOn(): bool Returns true if this fan is on.
+setOn(on: bool): void Sets this fan on to true or false.
+getRadius(): double Returns the radius of this fan.
+setRadius(radius: double): void Sets a new radius for this fan.
+getColor(): string Returns the color of this fan.
+setColor(color: string): void Sets a new color for this fan.

252
Exercise 9.4:
MyPoint
-x: double x-coordinate of this point.
-y: double y-coordinate of this point.
+MyPoint() Constructs a Point object at (0, 0).
+MyPoint(x: double, y: double) Constructs an object with specified x and y values.
+getX(): double Returns x value in this object.
+getY(): double Returns y value in this object.
+distance(secondPoint: MyPoint): double Returns the distance from this point to another point.

253
Exercise 9.6:
QuadraticEquation
-a: double Three coefficients.
-b: double
-c: double
+QuadraticEquation(a: double, b: double, Constructs a QuadraticEquation object with the
c: double) specified coefficients.
+getDiscriminant() :double Returns the discriminant.
+getRoot1(): double Returns root1.
+getRoot2(): double Returns root2.
di ( dP i M P i ) d bl

254
Chapter 10
Exercise 10.10:
MyInteger
-value: int An int value for the object.
+MyInteger(value: int) Constructs a M yInteger object with the specified int value.
+getValu e(): int const Returns the value in this object.
+isPrime(): bool const Returns true if the valu e in this object is p rime.
+isPrime(value: int): bool Returns true if a specified in t value is prime.
+isPrime(value: MyInteger): bool Returns true if the valu e in a specified M yInteger object is prime.
+isEven(): b ool const Returns true if the valu e in this object is even .
+isEven(value: int): bool Returns true if a specified in t value is even.
+isEven(value: MyInteg er): bool Returns true if the valu e in a specified M yInteger object is even.
+isOdd(): bool const Returns true if the valu e in this object is odd .
+isOdd (value: int): bool Returns true if a specified in t value is odd.
+isOdd(value: M yInteger): bool Returns true if the valu e in a specified M yInteger object is odd.
+equals (anotherValue: int): bool const Returns true if a specified in t value is equal to the value in this
object.
+equals (anotherValue: M yInteger): bool const Returns true if the valu e in a specified M yInteger object is eq ual
to the valu e in th is object.
+parseInt(value: String): int Returns the int value for the specified string.

Exercise 10.12:

Stock

-symbol: string The symbol of t his stoc k.


-na me: string The name of this stock.
-previousClosingPrice: double The pre vious c losing price of this stock.
-currentPrice: double The current price of this stoc k.

+Stock(symbol: string, name : string) Construc ts a stock with a specified symbol and a name.
+getSymbol(): string cons t Returns the symbol of this stock.
+getName(): string const Returns the name of this stoc k.
+getPreviousClosingPrice (): double const Returns the previous closing pric e of this stock.
+getCurrentPric e(): double const Returns the curre nt price of this stock.
+se tPreviousClosingPrice(price: double): void Sets the previous closing pric e of this stock.
+se tCurrentPrice(price: double): void Sets the current price of this stock.

+getChangePercent(): double const Returns the percentage of change of this stock.

255
Chapter 11

Exercise 11.8:
UML Class Diagram for Exercise 11.8

Circle2D
-x: d ou ble The x-coordinate of the center of the circle.
-y: d ou ble The y-coordinate of the center of the circle.
-radius: double The radius of the circle.
+Circle2D() Constructs a Circle2D with x 0, y 0, radius 1.
+Circle2D(x: double, y: double, radius: double) Constructs a Circle2D with the s pecified x, y, and radius.
+getX(): double const Returns x.
+getY(): double const Set a new x.
+setX(x: double): void Returns y.
+setY(y: double): void Set a new y.
+getRadius(): double const Returns n.
+setRadius(radius : double): void Set a new n .
+getPerimeter(): doub le const Returns the perimeter of the regular polygon.
+getArea(): double const Returns the area of the regular polygo n.
+contains(x: double, y: d ou ble): boolean const Returns true if the sp ecified point (x, y) is ins ide th is circle.
+contains(circle: Circle2D): boolean cons t Returns true if the sp ecified circle is in side this circle.
+overlaps(circle: Circle2D): boolean const Returns true if the sp ecified circle overlaps with this circle.

Exercise 11.12:
UML Class Diagram for Exercise 11.12

MyDate Get and set funct ions for these three data fields are omitted.

-ye ar: int The year, month, and day for the object.
-month: int
-da y: int

+MyDate() Construc ts a MyD ate for c urrent date.


+MyDate(elapsedTime : long) Construc ts a MyD ate for the specified e lapsed time .
+MyDate(year: int, month: int, day: int) Construc ts a MyD ate for the specified date.
+se tDa te (elapsedTime : long): void Sets a MyDate for the specified date.

256
Chapter 15

Exercise 15.2:

Person

Student Employee

Faculty Staff

Person Student
-n ame: string -status: s tring
-address: string +Stud ent()
-p hone: strin g
+Stud ent(nam e: string, address :
-email: s tring string, phone: string, em ail: string)
+Person() +getStatus(): string const
+Person(nam e: string, address: string, +setStatus(status: string): void
phone: string, email: string) +toString(): string const
+getName(): string con st
+getAddress(): string const
+getPhone(): string cons t
+getEmail(): string con st
+s etName(name: s tring): void
+s etAddress(address: string): void
+s etPhone(phonee: string): void
+s etEmail(email: string): void
+toString(): string const

Employee Faculty
-office: string -officeHour: s tring
-s alary: int -rank: string
-d ateHired: MyDate +Faculty()
+Employee() +Faculty(name: string, address: string, phone:
string, email: string)
+Employee(name: string, address:
string, ph on e: string, email: string) +getOfficeHour(): string const
+getOffice(): string const +setOfficeHour(officeHour: string): void
+getSalary(): int const +getRank(): string const
+getDateHired(): MyDate +setRank(rank: string): void
+s etOffice(office: string): void +toString(): s tring const
+s etSalary(salary: int): void
+s etDateHired (dataHired: Date): void
+toString(): string const

257
Staff MyDate
-title: string -year: int
+Staff() -m on th: int
-day: int
+Staff(name: string, address: string,
phone: string, email: string) +Faculty()

+getTitle(): string cons t +getYear(): int const


+s etTitle(title: s tring): void +getMonth(): int const
+toString(): string const +getDay(): int const
+setYear(year: int): void
+setMonth(month: int): void
+setDay(day: int): void
+toString(): s tring v

Exercise 15.4:

Account

CheckingAccount SavingsAccount

CheckingsAccount SavingsAccount
-overdrawnLimit: int +SavingsAccou nt()
+CheckingsAccount() +SavingsAccou nt(id: int, balance:
+CheckingsAccount(id: int, balance: double, annualInterestRate: d oub le)
double, annualInterestRate: d ouble)
+getOverdrawnLimit(): int const
+s et OverdrawnLimit(overdrawLimit:
int): void

258

You might also like