In this lab, we will build a couple of classes, where one will be composed into the other. Work in pairs if you wish.
We saw Point.java
in class last week. Do the
following, one step at a time, making sure it works before
you go on to the next step. Be sure to get help if you have any
difficulties.
Point.java
from last week or build a new
one of your own. If you are not confident that you can build
one of your own, try to build one anyway rather than using the one
from last week.Circle
which uses a Point
object as its center. This is where object composition
is used.Point
,
Circle
, and UseCircle
. You will not
need UsePoint
unless you want to test
your Point
using UsePoint
. To compile your
overall program, you can issue the javac
command only
once with UseCircle.java
, right?Circle
class, add the usual things such as
constructor(s), getter(s), setter(s), and other methods. You
should add only what is necessary for the class to work properly. Don't
blindly add constructors, getters, and setters. Other methods
that you will add is partly determined by what is used
in UseCircle.java
. Why did I say partly
instead of entirely? Well, the fact that some things are
not used in UseCircle.java
does not necessarily mean
that they should not be added to Circle
. Think about
it! If UseCircle
is written to test all possible
things that Circle
should provide, then yes, but it
may not be written that way. In other words, the person who
designs Circle
should think about what should be
included in it. Think about what would be useful in different cases for
someone else who will use Circle
. In most cases for
us, the person who designs the class X
and the the
person who writes UseX
are the same person. But, in
general that is not the case, e.g., we did not
write java.util.Random
but we use it.UseCircle
though, I want you to include a call to do
each of the following at a minimum:Point
object and the radius of the circle
object that you are creating.Point
object. The fact that you passed
a Point
object when you called the constructor
naturally matches the fact that the getter should be
returning a Point
object. Once you get the point
object, print the x and y values in it with two separate
calls to the getters in Point
.area
that computes the area of
the circle and print it.scale
to enlarge or shrink the circle
by a scale_factor
. You will have to
pass a scale factor as an argument into the method
scale
. How many parameters will the scale
method have? If you said 2, you would be wrong. If you
said 1, you would be right. Think about it! After you
scale it, make sure your scale method did the right thing by
reexamining the radius of the circle (use the getter method and
then print the value).translate
to move the circle
to a new location by passing a delta_x
and delta_y
to move in both x- and y-directions by
that much. Use the method to translate the circle by some distance
along the x and y axis. Again make sure your changes were done
correctly by using getters in Point to fetch the x- and y-values
of the new location. Here again you will be getting a
Point
object and look inside to get the x- and y-values,
right?When you are done with these, feel free to leave the meeting.