:: Enseignements :: ESIPE :: IMAC2 :: IMAC2 2020-2021 :: Object oriented programming in Java ::
![[LOGO]](http://igm.univ-mlv.fr/ens/resources/mlv.png) |
#6.en Inheritance, mutability, override, polymorphism, varargs
|
Exercice 1 - Circle
For the whole exercise, you will write your test
code in a class
Main in the package
fr.umlv.geom .
-
The code of the class fr.umlv.geom.Point does not compile.
Explain why.
Describe the implementation choices and their influences
on the prototype of the translate method (Point mutable or Point immutable).
-
For the rest of the exercise, we will choose the mutable version
of the class fr.umlv.geom.Point .
Write a class fr.umlv.geom.Circle , a circle being defined
by a Point corresponding to the center and a radius of type int .
What key-word should we specify when declaring these two fields?
-
Write a constructor of Circle that accept a Point and a radius.
-
Write the toString () method which displays the center and
the radius.
-
Write the translate (int dx, int dy) method which
translate the circle.
-
What does the following code display on the console?
var point = new Point(1, 2);
var circle = new Circle(point, 1);
var circle2 = new Circle(point, 2);
circle2.translate(1, 1);
System.out.println(circle + " " + circle2);
What should we do to prevent this from happening?
-
What's wrong with the getter getCenter () coded
as follows:
public Point getCenter() {
return center;
}
To help you, look at the following code:
var p = new Point(1, 2);
var c = new Circle(p, 1);
var p2 = c.getCenter();
p2.translate(1,1);
System.out.println(c);
What must be done to correct the code?
-
Write the area () method which returns
the area of the ring.
Modify the method toString () to also display
the area.
-
Write the contains (Point p) method which returns true if a point is
contained in the circle.
-
Write the method contains (Point p, Circle ... circles) which returns
true if a point is contained in one of the circles.
What does "..." mean in the prototype of the method?
Exercice 2 - One Ring for ...
The goal of this exercise is to build a ring like
being a circle from which a defined circular area has been hollowed out
by its internal radius.
For the whole exercise, for your tests, reuse the
class
Main seen previously.
-
Remember first, in which case it makes sense
to use inheritance.
-
Write the class Ring which inherits from the
Circle class.
-
Write a constructor of the class Ring taking
as parameters, a center, a radius and an internal radius.
Pay attention that the internal radius is smaller
to the radius of the ring.
What to do otherwise?
Note: All fields must be private.
-
We now want a ring to be displayed as a circle,
with in addition at the end of the line "InternalRadius: ir" where ir is the internal radius of the ring.
Change what it takes for it to work. Try with the following code (is everything seems correct to you? If not, what should be done to correct it?):
var point = new Point(1, 2);
var circle = new Circle(point, 2);
System.out.println(circle);
var ring = new Ring(point, 2, 1);
System.out.println(ring);
Then...
-
Implement a contains (Point) method
avoiding allocating objects or duplicating code.
PS: there are two solutions, one more elegant than the other.
-
Write the method contains (Point p, Ring ... rings)
which returns true if a point is contained in one of the rings.
-
What can you conclude from this lab?
© Université de Marne-la-Vallée