RSI2 TD2 Correction Java Inheritance
RSI2 TD2 Correction Java Inheritance
Questions:
1) Explain the information that the program will display during its execution.
2) In the Book class, the price attribute has been defined without any encapsulation rule (public, private...) How
Will Java interpret it? How can you test it or how is it tested in the program?
3) Explain how we test in this program whether a book is new or not.
4) Describe the class hierarchy outlined in this program and explain the call process between them.
constructors.
5) Explain how this program handles coloring a page of a coloring book.
6) Discover the typos present in the code. Correct them by coding and commenting on these.
a few lines under Eclipse.
Lucky Luke
2) In the Book class, the price attribute has been defined without any encapsulation rule (public, private...) How
Will Java interpret it? How can you test it or how is it tested in the program?
Java interprets it as a public attribute: we can access it.
in the Test class directly in the statement [Link] = 0.0.
3) Explain how we test in this program whether a book is new or not.
In fact, a book is new if it has no owner. Hence
the interest of the constructor which initializes the owner attribute to
an empty string. All must be initialized
variables ! Otherwise, the test may not work.
systematically.
4) Describe the class hierarchy outlined in this program and explain the call process between them.
constructors.
Three classes: Book, Comic, and Album, the last two inheriting from
properties of the first. The Book class having a constructor
(title, author, price, and number of pages parameters), the subclasses
must have their own constructor that calls (super method) the
constructor of the Book class. The Comic class has an attribute of
boolean type (in color) that specifies whether the comic is or
not in color.
5) Explain how this program handles coloring a page of a coloring book.
A boolean array allows for storing the information that a page
has been colored. This table is initialized to false in the
constructor of the Album class. Each call to the method of
coloring test if the page has been colored or not.
6) Discover the typos present in the code. Correct them by coding and commenting on them.
a few lines under Eclipse.
Five errors are in the text. They are in red in the code.
source above.
Here is the correction:
package packageexo1_correct; class Album extends Book {
booleanpage_coloriee[];
classBook { publicAlbum(String t, String a, double p, int n) {
// super(t,a,p,n);
// the attributes page_coloriee=new boolean[n];
// you
protectedStringtitre,auteur,proprietaire; for(i=0 ; i<n ; i++)
protected int nb_page; page_coloriee[i] =false;
double price }
//
the methods public void Color(int num_page) {
// if( (page_coloriee[num_page] == false) &&
publicLivre(String t, String a, double p, int nb) { !Is_new() ) {
t page_coloriee[num_page] =true;
p else {
nb [Link]("page" + num_page + " already
} coloriee
public void display() { }
[Link]("Title : " + title); }
Author : }
[Link]("Price : " + price);
[Link]("Number of pages: " + nb_page); //the main calling class
if(this.Is_new() ) {
No owner public class Test {
else { public static void main(String[] args) {
[Link]("Owner:" + owner); Book l1 = new Book("The Little Prince", "St
} Exupéry",10.40, 50) ;
[Link]() ; Book l2 = new Book("Tales", "Grimm", 14.40, 254);
} [Link]() ;
public boolean is_new() { buy("me");
if(owner == "") return true; [Link]();
else return false; [Link]= 0.0 ;
} buy("him");
public void Buy(String name) { [Link]();
name
} BD b1 = new BD("LuckyLuke", "Morris", 10.40, 45, true);
} BD b2 =newBD("Tintin","Herge",200.40, 45,false);
Buy("me");
class BD extends Livre { [Link]();
private boolean color; [Link]();
publicBD(String t, String a, double p, int nb,
booleanc) Album a1 =newAlbum("Dora","Dora", 3.5, 300) ;
super(t,a,p,nb); [Link]();
color = c ; [Link](23);
} buy("me");
} [Link](23) ;
}
}
Exercise 2: Example of class, constructor, and polymorphism
1) A website specializes in selling children's books. These books are either comics
drawn, either coloring books. A book is defined by its title, its author, its price and its
number of pages. The comics are either in color or in black & white so the user has the
possibility to color a page of a presented album. Suggest and implement and test under Eclipse
a solution to this problem.
It is intentionally the statement of exercise 1.
The website wants to give users the opportunity to resell a book and exchange two bands.
drawn if they have an equivalent price. Modify the previous program to take these into account.
additional functions.
The following method is added to the Book class
public void resell(String buyer, double used_price) {
buyer
price = used_price;
}
We chose to resell a book to a new one.
owner and at a new second-hand price (the two parameters of
the method) but we could have chosen other parameters like nothing
is not clearly specified.
The following method is to be added in the BD class
public void Exchange(BD b) {
if([Link]==[Link]){
Equal prices: exchange possible
String ProTmp = [Link];
[Link] = [Link];
[Link](ProTmp);
else // do not exchange the databases
Different prices: exchange impossible
}
This is the first time we see one of the constraints of languages.
object. The variable b being of another type
Finally, the website wants to expand its offering of cultural works to films (DVDs) that are also defined.
by a title, an author and a price but with additional information about the duration of the film. How
modify the class hierarchy to incorporate these changes? Program it.
The hierarchy of classes must be profoundly changed. Book and
films must inherit from a 'superior' class Work. In this case,
the attributes author, title, owner, and price "rise in the
class Work. The class Book retains only the attribute
nb_pages in the same way as the film class has a duration. The
the constructors of these two classes need to be modified. The two
subclasses BE and Album of the Book class do not change.
packagetp3et4_Ex2; the buy method gives a name of property to the object
//the class Work public void buy(String name) { owner = name; }
classWork { }
//the attributes
protectedStringtitre,auteur,proprietaire; The Film class inherits from the Work class
protected double price; class Film extends Oeuvre {
the constructor with the title as a parameter, only one additional attribute: the duration of the film in minutes
the author and the price protected int duration;
The owner attribute is initialized the constructor of this class calls that of the
publicOeuvre(String t, String a, double p) { superior class, that is to say the Work class
t publicFilm(String t, String a, double p, int d) {
} super(t, a, p);
A work is new if it does not yet have d
owner }
hence the interest in initialization in the The resale method simply changes the owner.
builder and the price of the book
public boolean is_new() { public void Resell(String buyer, double used_price){}
if(owner == "") return true; buyer
else return false; price = used_price;
} }
Show the information about the work public String Owner() {
public void show() { return(owner);
[Link]("Title: " + title); }
[Link]("Author : " + author); }
[Link]("Price : " + price);
if(this.Is_new()) The Book class inherits from the Work class
No owner class Book extends Work {
else only one additional attribute: number of pages of the book
[Link]("Owner : " + owner); protected int nb_pages;
[Link]() ; //the constructor of this class calls that of the
} upper class, that is to say the class Work
publicLivre(String title, String author, double price, int nb) buy("me");
super(t, a, p); [Link]();
nb [Link]("me");
} [Link]();
the resale method simply modifies the "me" resells to "him" b2
owner and the price of the book [Link]("him", 10.4);
public void Resell(String buyer, double used_price) { [Link]();
buyer //we test the swap function of b1 and b2
price = used_price; [Link](b2) ;
} [Link]("AFTER EXCHANGE OF "
public String Owner() { +[Link]+" and "+[Link];
return(owner); [Link]();
} [Link]();
} We test the method of coloring a page.
The class BD inherits from the class Livre //works
class BD extends Book { Album a1 =newAlbum("Dora","Dora",3.5, 200) ;
only one additional attribute: a boolean that [Link]();
defines whether the database is in color or not [Link](23);
private boolean color; [Link]("me");
//The constructor that calls that of the Book class [Link](23);
And so that of the class Work We create a film object
publicBD(String t, String a, double p, int n, boolean c) { Film f = new Film("Taxi", "Besson", 20, 90);
super(t,a,p,n); [Link]();
c }
} }
The method that swaps the names of the owners if
The price of the comics is the same.
Editions :
public void Swap(BD b) {
if([Link]==[Link]){ The Little Prince
St Exupéry
Equal prices: exchange possible Prix : 10.4
String ProTmp = [Link]; No owner
[Link] = [Link];
The Little Prince
[Link](ProTmp); St Exupéry
}else// do not exchange the databases 10.4
Different prices: exchange Owner: me
impossible Tales
} Grimm
} Prix : 14.4
Owner: him