Comp 110

Homework Assignment #3

Due: 11/9/2009

 

  1. Design a class named Triangle to represent a triangle.

The class contains:

§  Three double data fields named  sideA, sideB, and sideC that specify the lengths of the three sides of the triangle.  The default values is 1.0 for each side.

§  A string data field named color that specifies the color of a triangle.  Hypothetically, assume that all triangles have the same color.  The default color is white.

§  A no-arg constructor that creates a default triangle.

§  A constructor that creates a triangle with the specified side lengths.

§  The accessor and mutator methods for all three data fields.

§  A method named getArea() that returns the area of this triangle if it is a valid triangle and -1.0 otherwise.

§  A method named getPerimeter() that returns the perimeter if it is a valid triangle and -1.0 otherwise.

§  A method named isValidTriangle() that returns true if this is a valid triangle and false otherwise.

For this class:

a.    Draw a UML diagram for it.

b.    Specify detailed algorithms for each non-trivial method (e.g., algorithms are not usually needed for constructors and simple accessor and mutator methods).

(Compare with problem 7.1 on page 259 of the book.)

 

  1. Design a class named Account that represents an individual’s bank account.

The class contains:

§  An int data field named id for the account (default 0).

§  A double data field named balance for the account (default 0.0)

§  A double data field named annualInterestRate that stores the current interest rate (default 0.0)

§  A Date data field named dateCreated that stores the date when the account was created.

§  A no-arg constructor that creates a default account.

§  The accessor and mutator methods for id, balance, and annualInterestRate.

§  The accessor method for dateCreated.

§  A method named getMonthlyInterestRate() that returns the monthly interest rate.

§  A method named withdraw() that withdraws a specified amount from the account if the account has enough money in it.  Otherwise, it prints a message saying “Insufficient funds”.

§  A method named deposit() that deposits a specified amount to the account.

For this class:

a.    Draw a UML diagram for it.

b.    Specify detailed algorithms for each non-trivial method.

(This is a slight modification of the first part of problem 7.3 on page 260 of the book.)