Exercises - SIUE Computer Science

Exercises: 1. Consider a program that will keep track of the items in a school's
library. Draw a class hierarchy, including a base class, for the different kinds of
items. ... See the code in LibraryItem.java. .... Create a class SubstitutionCipher
that implements the interface MessageEncoder, as described in the previous
exercise.

Part of the document


Exercises:

1. Consider a program that will keep track of the items in a school's
library. Draw a class hierarchy, including a base class, for the different
kinds of items. Be sure to also consider items that cannot be checked out.

|Solution: |
|[pic] |


2. Implement your base class for the hierarchy from the previous exercise.

|Solution: |
| |
|See the code in LibraryItem.java. |

3. Draw a hierarchy for the components you might find in a graphical user
interface. Note that some components can trigger actions. Some components
may have graphics associated with them. Some components can hold other
components.


|Solution: |
| |
|Here is part of a such a hierarchy. There are many other types of |
|components. |
|[pic] |


4. Suppose we want to implement a drawing program that creates various
shapes using keyboard characters. Implement an abstract base class
DrawableShape that knows the center (two integer values) and the color (a
string) of the object. Give appropriate accessor methods for the
attributes. You should also have a mutator method that moves the object by
a given amount.

|Solution: |
| |
|See the code in DrawableShape.java. |


5. Create a class Square derived from DrawableShape, as described in the
previous exercise. A Square object should know the length of its sides. The
class should have an accessor method and a mutator method for this length.
It should also have methods for computing the area and perimeter of the
square. Although characters are taller than they are wide-so the number of
characters in the vertical sides will differ from the number in the
horizontal sides-you need not worry about this detail when drawing the
square.

|Solution: |
| |
|See the code in Square.java. |

6. Create a class SchoolKid that is the base class for children at a
school. It should have attributes for the child's name and age, the name of
the child's teacher, and a greeting. It should have appropriate accessor
and mutator methods for each of the attributes.

|Solution: |
| |
|See the code in SchoolKid.java. |

7. Derive a class ExaggeratingKid from SchoolKid, as described in the
previous exercise. The new class should override the accessor method for
the age, reporting the actual age plus 2. It also should override the
accessor for the greeting, returning the child's greeting concatenated with
the words "I am the best."

|Solution: |
| |
|See the code in ExaggeratingKid.java. |

8. Create an abstract class PayCalculator that has an attribute payRate
given in dollars per hour. The class should also have a method
computePay(hours) that returns the pay for a given amount of time.

|Solution: |
| |
|See the code in PayCalculator.java. |

9. Derive a class RegularPay from PayCalculator, as described in the
previous exercise. It should have a constructor that has a parameter for
the pay rate. It should not override any of the methods. Then derive a
class HazardPay from PayCalculator that overrides the computePay method.
The new method should return the amount returned by the base class method
multiplied by 1.5.

|Solution: |
| |
|See the code in RegularPay.java. |
|See the code in HazardPay.java. |

10. Create an abstract class DiscountPolicy. It should have a single
abstract method computeDiscount that will return the discount for the
purchase of a given number of a single item. The method has two parameters,
count and itemCost.

|Solution: |
| |
|See the code in DiscountPolicy.java. |

11. Derive a class BulkDiscount from DiscountPolicy, as described in the
previous exercise. It should have a constructor that has two parameters,
minimum and percent. It should define the method computeDiscount so that if
the quantity purchased of an item is more than minimum, the discount is
percent percent.

|Solution: |
| |
|See the code in BulkDiscount.java. |

12. Derive a class BuyNItemsGetOneFree from DiscountPolicy, as described in
Exercise 10. The class should have a constructor that has a single
parameter n. In addition, the class should define the method
computeDiscount so that every nth item is free. For example, the following
table gives the discount for the purchase of various counts of an item that
costs $10, when n is 3:

|Solution: |
| |
|See the code in BuyNItemsGetOneFree.java. |

13. Derive a class CombinedDiscount from DiscountPolicy, as described in
Exercise 10. It should have a constructor that has two parameters of type
DiscountPolicy. It should define the method computeDiscount to return the
maximum value returned by computeDiscount for each of its two private
discount policies. The two discount policies are described in Exercises 11
and 12.

|Solution: |
| |
|See the code in CombinedDiscount.java. |

14. Define DiscountPolicy as an interface instead of the abstract class
described in Exercise 10.

|Solution: |
| |
|See the code in DiscountPolicy.java. |

15. Create an interface MessageEncoder that has a single abstract method
encode(plainText), where plainText is the message to be encoded. The method
will return the encoded message.

|Solution: |
| |
|See the code in MessageEncoder.java. |

16. Create a class SubstitutionCipher that implements the interface
MessageEncoder, as described in the previous exercise. The constructor
should have one parameter called shift. Define the method encode so that
each letter is shifted by the value in shift. For example, if shift is 3, a
will be replaced by d, b will be replaced by e, c will be replaced by f,
and so on. Hint: You may wish to define a private method that shifts a
single character.

|Solution: |
| |
|See the code in SubstitutionCipher.java. |

17. Create a class ShuffleCipher that implements the interface
MessageEncoder, as described in Exercise 15. The constructor should have
one parameter called n. Define the method encode so that the message is
shuffled n times. To perform one shuffle, split the message in half and
then take characters from each half alternately. For example, if the
message is "abcdefghi", the halves are "abcde" and "fghi". The shuffled
message is "afbgchdie". Hint: You may wish to define a private method that
performs one shuffle.

|Solution: |
| |
|See the code in ShuffleCipher.java. |




Projects:

1. Define a class named Employee whose objects are records for employees.
Derive this class from the class Person given in Listing 8.1. An employee
record inherits an employee's name from the class Person. In addition, an
employee record contains an annual salary represented as a single value of
type double, a hire date that gives the year hired as a single value of
type int, and an identification number that is a value of type String. Give
your class a reasonable complement of constructors, accessor methods, and
mutator methods, as well as an equals method. Write a program to fully test
your class definition.

|Notes: |
| |
|This project should be pretty straightforward. The solution shown|
|here has nine constructors, including a default that specifies |
|nothing. Since it did not seem reasonable to have salary, hire |
|date or social security numbers without a name, the remaining |
|constructors all require at least the name to be specified. Note|
|the use of methods from the base class,