0% found this document useful (0 votes)
14 views5 pages

RSI2 TD2 Correction Java Inheritance

The document presents a Java program implementing a class hierarchy representing different types of works (books, albums, comics). The program allows the creation of instances of these classes, displaying their information, and testing some of their methods such as coloring a page of an album.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

RSI2 TD2 Correction Java Inheritance

The document presents a Java program implementing a class hierarchy representing different types of works (books, albums, comics). The program allows the creation of instances of these classes, displaying their information, and testing some of their methods such as coloring a page of an album.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercise 1: Analysis of a Java program.

class Album extends Book {


Let's consider the following Java program: booleanpage_coloriee[];
packageexo1; publicAlbum(String t, String a, double p, int n) {
super(t,a,p,n);
classBook { page_coloriee=new boolean[n];
//
you
the attributes
for(i=0 ; i<100 ; i++)
// page_coloriee[i] =false;
title
}
protected int nb_page;
public void Color(int num_page) {
double price
if((page_coloriee[num_page] == false) && !Is_new()){
// page_coloriee[num_page] =true;
the methods else {
//
page already colored
publicLivre(String t, String a, double p, int nb) {
}
t
}
p
}
nb;
}
// the main calling class
public void Display() {
[Link]("Title: " + title);
public class Test {
[Link]("Author: " + author);
public static void main(String[] args) {
[Link]("Price : " + price);
[Link]("Number of pages: " + nb_page); Book l1 = newBook("The Little Prince", "St
if(this.Is_new()) {
Exupéry",10.40, 50)
No owner Book l2 = newBook("Tales", "Grimm", 14.40, 254);
else {
[Link]();
[Link]("Owner: " + owner);
buy("me");
}
[Link]();
[Link]() ;
[Link]= 0.0 ;
}
Buy("him");
public boolean is_new() {
[Link]();
if(owner == "") return true;
else return false; BD b1 = new BD("Lucky Luke", "Morris", 10.40, 45, true);
} new BD("Tintin", "Herge", 200.40, 45, false);
public void Buy(String name) {
buy("me");
name
[Link]();
}
[Link]();
}
Album a1 = new Album("Dora", "Dora", 300, 3.5);
classBDextendBook {
[Link]();
private boolean color;
[Link](23);
publicBD(String t, String a, double p, int nb, boolean c) {
buy("me");
super(t,a,p,nb) ;
[Link](23);
c
}
}
}
}

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.

Exercise 2: Class inheritance and constructors


1) A website specializes in selling children's books. These books are either comic strips
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 black & white, so the user has the option
to color a page of a presented album. Propose, implement, and test a solution in Eclipse.
this problem.
The website aims 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.
Finally, the website wants to expand its cultural offerings to films (DVDs) which are defined themselves
also by a title, an author, and a price but with additional information about the duration of the film. How
modify the class hierarchy to integrate these changes? Program it.
Exercise 1: Analysis of a Java program. Objectives: class hierarchy, inheritance
packageexo1;
class Album extends Book {
class Book { booleanpage_coloriee[];
// publicAlbum(String t, String a, double p, int n) {
the attributes super(t,a,p,n);
// page_coloriee=new boolean[n];
protected string title, author, owner; you
protected int nb_page; for(i=0; i<100; i++)
double price page_coloriee[i] = false;
// }
the methods public void Colorie(int num_page) {
// if((page_coloriee[num_page] == false) && !Is_new()){
publicLivre(String t, String a, double p, int nb) { page_coloriee[num_page] = true;
t else { [Link]("page already colored");
p }
nb; }
} }
public void Show() {
[Link]("Title : " + title) ; // the main calling class
[Link]("Author : " + author);
Price: public class Test {
[Link]("Number of pages: " + nb_page); public static void main(String[] args) {
if(this.Is_new()) { Book l1 = new Book("The Little Prince", "St
No owner Exupéry
} Book l2 = newBook("Tales", "Grimm", 14.40, 254);
Owner: [Link]();
} [Link]("me");
[Link](); [Link]();
} [Link]= 0.0 ;
public boolean is_new() { buy("him");
if(owner==" ")return true; [Link]();
else return false;
} BD b1 =newBD("Lucky Luke","Morris",10.40, 45,true);
public void Buy(String name) { BD b2 = new BD("Tintin", "Herge", 200.40, 45, false);
name buy("me");
} [Link]();
} [Link]();

class BD extends Book { Album a1 = new Album("Dora", "Dora", 300, 3.5);


private boolean color; [Link]();
publicBD(String t, String a, double p, int nb, boolean c) { [Link](23);
super(t,a,p,nb); [Link]("me");
c [Link](23) ;
} }
} }
Questions:
1) Explain the information that the program will display during its execution.
The Little Prince Morris
The Little Prince Prix : 10.4
St Exupéry Number of pages: 45
Prix : 10.4 Owner: me
Number of pages: 50
No owner Tintin
Herge
Title: The Little Prince Prix : 200.4
St Exupéry Nombre de pages : 45
Prix : 10.4 No owner
Number of pages: 50
Owner: me Dora
Dora
Tales Price: 3.5
Grimm Number of pages: 300
Price: 14.4 No owner
Nombre de pages : 254
him page 23 already colored

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

The Album class that inherits from Book Lucky Luke


class Album extends Book { Morris
Prix : 10.4
only one additional attribute: an array of Owner: me
boolean for the number of pages size
Defines whether the page has been read or not Tintin
Author: Herge
booleanpage_coloriee[] ; Prix : 200.4
the constructor that calls that of Book me
Note that the size of the boolean array is
Tintin
initialized in this constructor based on the Herge
//parameters passed to the constructor Prix : 10.4
publicAlbum(String t, String a, double p, int nb) { Owner: him
super(t, a, p, nb); Equal prices: exchange possible
page_coloriee=new boolean[nb] ; AFTER EXCHANGE OF Lucky Luke and Tintin
you Lucky Luke
Morris
for(i=0; i<nb; i++) 10.4
page_coloriee[i] = false; Owner: him
}
Tintin
the method that colors a page: set to true the Herge
value of the colored_page array if it has not been done Prix : 10.4
public void Colorie(int num_page) { Owner: me
if ((page_coloriee[num_page] == false) && !Is_new()) Dora
page_coloriee[num_page] = true; Dora
else{ Prix : 3.5
[Link]("page"+num_page+"already colored");No owner
[Link](); page 23 already colored
}
Taxi
} Besson
} Prix : 20.0
No owner
//the main calling class
public class Test {
public static void main(String[] args) {
//creation of two variables of type Book
Book l1 = newBook("The Little Prince", "St
Exupéry, 10.40, 30
Book l2 = newBook("Tales", "Grimm", 14.40, 250);
We display the characteristics of each one.
[Link]();
buy("me");
[Link]();
[Link]= 0.0 ;
buy("him");
[Link]();
creation of two variables of type DB
BD b1 =newBD("Lucky Luke","Morris",10.40, 45,true);
BD b2 = new BD("Tintin", "Herge", 200.40, 50, false);
I buy two comics b1 and b2

You might also like