Lab 1: Classes and Methods
This lab contains some exercises to get you more familiar with classes and
methods. It is due Friday (9/9) at the beginning of class. You are
welcome to use your text as a reference.
To turn in: Create a directory called "lab1" inside your submit
directory. Copy all of your .java files to this directory.
- For this question, you'll modify the point class that we worked
with in lecture.
- Add a method called distance to the point class. It should take
as input another point, and return the distance between them. The
formula for the distance between two points is: sqrt((x2 - x1)^2 + (y2
- y1)^2)
- Add a method called compareTo() to the point class. It should
take as input another point. It should return -1 if the x and y values
are both smaller than the point passed in, 1 if they are both greater,
and 0 if they are the same or if one is bigger and one is smaller.
- Write a main method that tests these out. It should allow the
user to enter x and y values for two points, compute the distance
between them, and tell whether one point is less than another.
- For this question, you'll modify the circle class we worked with
in lecture.
- Add a method called getArea() that computes the area of the
circle.
- Add a method called isContained(). It should take as input a
point. It should retrun true if the point is within the circle, and
false otherwise. (hint: what is the distance between the point and the
circle's center?)
- Write a main method that allows the user to test getArea() and
isContained()