JMU JMU - Department of Computer Science
Help Tools
Lab: Gaining Experience Designing Classes


Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.

Getting Ready: Before going any further, you should:

  1. Depending on your development environment, create either a directory or a project for this lab.

1. A Class of Immutable Objects: This part of the lab will give you some experience designing a simple class of immutable objects. 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?


                                        
          numerator
          denominator
          
                                     
    Expand
  2. What data type would you use for each?


                                        
          numerator        int
          denominator      int
          
                                     
    Expand
  3. What are the important verb phrases in this textual description.


          add a fraction to
          subtract a fraction from
          multiply a fraction by
          divide a fraction by
          
    Expand
  4. What parameters are required by each?


    A fraction.
    Expand
  5. What is the return type of each?


    A fraction.
    Expand
  6. Given the analysis above, what would your initial encapsulation be? (Do not include any constructors at this point.)


          public class Fraction
          {
              private int    denominator, numerator;
    
              public Fraction add(Fraction other);
              public Fraction divide(Fraction other);
              public Fraction multiply(Fraction other);
              public Fraction subtract(Fraction other);
          }
          
    Expand
  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?


          private int commonDenominator(Fraction other);
          
    Expand
  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.)


    A '-' or a '' depending on the sign, the numerator (in the narrowest possible field), a '/', and the denominator (in the narrowest possible field).
    Expand
  9. Given that Fraction objects are supposed to be immutable, should this class have "set" methods?


    No, since a user could then change a Fraction object.
    Expand
  10. Should this class have accessor (i.e., "get") methods for the numerator and/or denominator?


    No. A fraction is a coherent whole; the internal details should be hidden.
    Expand
  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.)


    There are three obvious possibilities, a boolean, a char (either '-' or ' '), or an int (either -1 or 1). To decide which is best, you need to think about the different methods that it will be used in.

    For the toString() method it's probably best to use a char. However, a char will be very awkward for all of the arithmetic operations.

    For the arithmetic operations it's probably best to use a char. This will create a little more work for the toString() method, but not much.

    The boolean, the most obvious of the three since it can take on two values as needed, is probably the worst choice.

    So, I'd use an int that can take the value -1 or 1.

    Expand
  12. What operation is very similar to addition?


    Subtraction
    Expand
  13. How would you use the add() method to perform this other operation?


    Negate the other fraction and then call add().
    Expand
  14. What operation is very similar to multiplication?


    Division
    Expand
  15. How would you use the multiply() method to perform this other operation?


    Invert the other fraction and then call multiply().
    Expand
  16. What explicit value constructor should this class certainly contain?


          public Fraction(int numerator, int denominator)
          
    Expand
  17. Do you think this class should contain a default constructor? Why or why not?


    No. Since Fraction objects are immutable, it doesn't make sense to have a default constructor.
    Expand
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.


          horizontal drawing dial
          vertical drawing dial
          
    Expand
  2. What are the important verb phrases in this textual description?


          shake
          turn the horizontal drawing dial clockwise
          turn the horizontal drawing dial counter-clockwise
          turn the vertical drawing dial clockwise
          turn the vertical drawing dial counter-clockwise
          
    Expand
  3. What parameters are required by each?


    The distance/amount/size of the turn which is probably an int.

    One could, alternatively, think about either percentages of the horizontal/vertical distance or percentages of a "turn" (measured in either degrees or radians).

    Expand
  4. What is the return type of each?


    void
    Expand
  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.)


          public class Esketcher
          {
              public void shake();
    
    	  public void turnHorizontalDialClockwise(int amount);
    	  public void turnHorizontalDialCounterClockwise(int amount);
    	  public void turnVerticalDialClockwise(int amount);
    	  public void turnVerticalDialCounterClockwise(int amount);
          }
          
    Expand
  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?


    The position of the virtual pen (i.e., its horizontal and vertical position, both of which are probably int values).
    Expand
  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?


    The size of the drawing area (probably two int attributes).
    Expand
  8. How could you combine the two "turn horizontal dial" methods into one?


    	  public void turnHorizontalDial(int direction, int amount);
          
    Expand
  9. How could you combine the two "turn vertical dial" methods into one?


    	  public void turnVerticalDial(int direction, int amount);
          
    Expand
  10. It is probably not a good idea to combine the public "turn vertical dial" and "turn horizontal dial" methods. Why not?


    The "real world" object being modeled actually has two dials. Hence, combining them would make this class more difficult to understand.
    Expand

Copyright 2020