:: Enseignements :: ESIPE :: IMAC2 :: IMAC2 2020-2021 :: Object oriented programming in Java ::
![[LOGO]](http://igm.univ-mlv.fr/ens/resources/mlv.png) |
#8.en Overload, Override, Polymorphism, Late Binding
|
Exercice 1 - Override, late binding
class A {
int x = 1;
public int getX() {
return x;
}
static void printX(A a) {
System.out.println("in A.printX");
System.out.println("x " + a.x);
System.out.println("getX() " + a.getX());
}
int m(A a) { return 1; }
}
class B extends A {
int x = 2;
public int getX() {
return x;
}
static void printX(B b) {
System.out.println("in B.printX");
System.out.println("x " + b.x);
System.out.println("getX() " + b.getX());
}
int m(B b) { return 2; }
}
public class Overridings {
public static void main(String[] args) {
A.printX(new A()); // 1
//B.printX(new B()); // 2
//A.printX(new B()); // 3
A a = new B();
//a.m(a); // 4
}
}
Explain, for each of the four numbered calls // 1, // 2, // 3 and // 4, what should be displayed, simply by reading the code. Then, for each call, run the code to confirm (or deny) your intuition.
Note: we can notice that in Java, we can put several classes in a Java file provided that only one class is declared public; we will not do it again because it disturbs everyone, IDEs included.
In this case, the name of the .java file must be the same as the name of the public class.
Exercice 2 - Blessed be the fruit
We want to write a simple program to manage the invoicing of a
fruit cooperative selling
organic apples. A person wanting to buy
apples takes a basket, chooses several apples and pays. Each apple has
a weight, in grams. The price of an apple, whatever its type, is equal
to its weight (measured in grams) divided by 2.
The display of a basket must indicate one apple per line with, for each apple,
the type of apple, the weight of the apple, its quantity (1 for now),
then a line indicating the total price of the basket.
Golden 20 g x 1
Pink Lady 40 g x 1
price: 30
There is no maximum size for the basket.
You will therefore use the class
java.util.ArrayList to store the apples.
-
We want to have a main method allowing the following calls:
public static void main(String[] args) {
var apple1 = new Apple(20, "Golden");
var apple2 = new Apple(40, "Pink Lady");
var basket = new Basket();
basket.add(apple1);
basket.add(apple2);
System.out.println(basket);
}
Write the Apple and Basket classes and their necessary methods.
-
Check that the following code also works; (the result must be true); if not, correct your code:
public static void main(String[] args) {
var set = new HashSet<Apple>();
set.add(new Apple(20, "Golden"));
System.out.println(set.contains(new Apple(20, "Golden")));
}
-
Eventually, the cooperative begins to be profitable. So it's time to diversify by also selling
pears. A pear just has a value from 0 to 9 indicating a juice factor. The price of a pear
is equal to 3 times its juice factor.
We want this main to work:
public static void main(String[] args) {
var apple1 = new Apple(20, "Golden");
var apple2 = new Apple(40, "Pink Lady");
var pear = new Pear(5);
var basket = new Basket();
basket.add(apple1);
basket.add(apple2); // une pomme
basket.add(pear); // une poire
System.out.println(basket);
}
Write and modify the classes as needed.
-
We want to allow adding several apples or several pears to our basket at once by indicating
an additional parameter to the add method, indicating the quantity of apples or pears
that we want to put in the basket.
The basket display should indicate the quantity of each apple and pear:
public static void main(String[] args) {
var apple1 = new Apple(20, "Golden");
var apple2 = new Apple(40, "Pink Lady");
var pear = new Pear(5);
var basket = new Basket();
basket.add(apple1, 5); // 5 pommes
basket.add(apple2);
basket.add(pear, 7); // 7 poires
System.out.println(basket);
}
Modify the code accordingly, knowing that to represent an apple (or a pear) and a quantity,
the easiest way is to use a hash table (also called dictionary) like java.util.HashMap
to associate an apple (or a pear) with a quantity (a whole value).
Reminder: HashMap.put () allows you to store elements, HashMap.get () or getOrDefault () allows you to output elements. To browse, we will use
for (var entry: map.entrySet ()) {
...
entry.getKey()
...
entry.getValue ()
...
}
.
-
Finally, instead of representing apple types by String ,
we would like to avoid typing errors by representing the values of an enumeration.
public enum AppleKind {
Golden, PinkLady, GrannySmith;
}
Modify your code so that the following main code works:
public static void main(String[] args) {
var apple1 = new Apple(20, AppleKind.Golden);
var apple2 = new Apple(40, AppleKind.PinkLady);
var pear = new Pear(5);
var basket = new Basket();
basket.add(apple1, 5);
basket.add(apple2);
basket.add(pear, 7);
System.out.println(basket);
}
Please note, the display must remain identical (with the space between Pink and Lady).
Note: in Java, we have the right to redefine the toString () method of an enum. Yes, Yes, you just have to try!
© Université de Marne-la-Vallée