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:
- Make a directory for this lab.
- 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.
- What are the important noun phrases
in this textual description?
- What data type would you use for
each?
- What are the important verb phrases
in this textual description.
- What parameters are required by each?
- What is the return type of each?
- Given the analysis above, what would
your initial encapsulation be? (Do not include any constructors at this
point.)
- 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?
- 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.)
- Given that
Fraction
objects are supposed to be
immutable, should this class have "set" methods?
- Should this class have accessor (i.e., "get") methods for the
numerator and/or denominator?
- 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.)
- What operation is very similar to
addition?
- How would you use the
add(
)
method to perform this other operation?
- What operation is very similar to
multiplication?
- How would you use the
multiply(
)
method to perform this other operation?
- Given the discussion of the addition
method (and its related method) above, what private method would you add?
- Given the discussion of the
multiplication method (and its related method) above, what private method
would you add?
- What explicit value constructor
should this class certainly contain?
- Do you think this class should
contain a default constructor? Why or why not?
- 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
).
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.
- What are the important noun phrases
in this textual description.
- What are the important verb phrases
in this textual description?
- What parameters are required by each?
- What is the return type of each?
- 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.)
- 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?
- 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?
- How could you combine the two
"turn horizontal dial" methods into one?
- How could you combine the two
"turn vertical dial" methods into one?
- It is probably not a good idea to
combine the public "turn vertical dial" and "turn
horizontal dial" methods. Why not?
- 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?
- 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)?
- 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.