CS239 - Advanced Programming

 

Lab 11 : Gaining Experience Designing Classes

Instructions: Answer as many of the following questions as you can during the lab period. You must type your answers in the worksheet (provided below) and submit your answers using Blackboard (by attaching the worksheet and all of your source code). by midnight tonight.   Note: Enter "Lab 11" and your name in the Comments Area.  You must also print out the worksheet and your code.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Download the worksheet  to your working directory on the M drive.. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)

1. A Class of Immutable Objects: This part of the lab will give you some experience designing a simple class of immutable objects (see page 554 in your text for a definition). The textual description of this class follows:

A fraction is a quotient of one integer divided by another (often indicated by a/b).

The dividend a is called the numerator and the divisor b is called the denominator. One can add a fraction to, subtract a fraction from, multiply a fraction by, and divide a fraction by another fraction. The result of each of these operations is another fraction.

  1. What are the important noun phrases in this textual description?
  2. What data type would you use for each?
  3. What are the important verb phrases in this textual description.
  4. What parameters are required by each?
  5. What is the return type of each?
  6. Given the analysis above, what would your initial encapsulation be? (Do not include any constructors at this point.)
  7. To add and subtract two fractions you must first find a common denominator and convert each to an equivalent fraction with that denominator. Given this observation, what private method would you add?
  8. All classes should have a toString() method that returns a String representation of an instance. For this class, how should the String be formatted? (Note: Remember to think about the sign of the fraction.)
  9. Given that Fraction objects are supposed to be immutable, should this class have "set" methods?
  10. Should this class have accessor (i.e., "get") methods for the numerator and/or denominator?
  11. Given the discussion of the toString() method above, it would clearly be useful to always use positive integers for the numerator and the denominator and keep a sign attribute. What type should the sign attribute be and what values should it take on? (Hint: Think about how it will be used in the various methods.)
  12. What operation is very similar to addition?
  13. How would you use the add() method to perform this other operation?
  14. What operation is very similar to multiplication?
  15. How would you use the multiply() method to perform this other operation?
  16. Given the discussion of the addition method (and its related method) above, what private method would you add?
  17. Given the discussion of the multiplication method (and its related method) above, what private method would you add?
  18. What explicit value constructor should this class certainly contain?
  19. Do you think this class should contain a default constructor? Why or why not?
  20. One could add the following class constants:
21.      static final int NEGATIVE = -1;
22.      static final int POSITIVE =  1;
      

Is this is a good idea? Why or why not? If so, should they be public or private?

2. A Class of Mutable Objects: This part of the lab will give you some experience designing a simple class of mutable objects. The textual description of this class follows:

An "Esketcher" is a simple electronic drawing toy (like the famous Etch A Sketch from The Ohio Art Company Click here for related information from another site.).

You can shake an "Esketcher", you can turn the horizontal drawing dial clockwise, you can turn the horizontal drawing dial counter-clockwise, you can turn the vertical drawing dial clockwise, and you can turn the vertical drawing dial counter-clockwise.

  1. What are the important noun phrases in this textual description.
  2. What are the important verb phrases in this textual description?
  3. What parameters are required by each?
  4. What is the return type of each?
  5. Since the noun phrases are part of the verb phrases it may not be necessary to have any attributes. Assuming that this is the case, what would your initial encapsulation be? (Do not include any constructors at this point.)
  6. Though it isn't mentioned in the textual description, an "Esketcher" has a "virtual pen" that does the drawing. What important information about the "virtual pen" must be maintained?
  7. Though it isn't mentioned in the textual description, when the "virtual pen" gets to the right or left edge turning the horizontal dial in the offending direction has no effect. Similarly, when the "virtual pen" gets to the top or bottom edge turning the horizontal dial in the offending direction has no effect. What important information must be maintained to support this behavior?
  8. How could you combine the two "turn horizontal dial" methods into one?
  9. How could you combine the two "turn vertical dial" methods into one?
  10. It is probably not a good idea to combine the public "turn vertical dial" and "turn horizontal dial" methods. Why not?
  11. Though it is probably not a good idea to combine the public "turn vertical dial" and "turn horizontal dial" methods, it might be a good idea to add a private method that accomplishes the same thing. What should the signature of this method be?
  12. How would this private method relate to the public "turn" methods (e.g., unrelated, the private method calls the public methods, the public methods call the private methods, the public and private methods call each other)?
  13. Given the public and private "turn" methods identified above, what class attributes would you add? (Be careful to indicate whether they are public or private.)

lab created by David Bernstein.  Modified slightly by Elizabeth Adams.