0% found this document useful (0 votes)
3 views3 pages

Java Object-Oriented Programming Exercises

This document presents four exercises on object-oriented programming in Java. Exercise 1 describes the modeling of a pencil and a colored pencil with inheritance. Exercise 2 focuses on the modeling of concepts related to cities and capitals. Exercise 3 concerns document management in a library with inheritance between document classes. Exercise 4 introduces the Library and Livrothèque classes for the management of document collections.

Translated by

ScribdTranslations
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)
3 views3 pages

Java Object-Oriented Programming Exercises

This document presents four exercises on object-oriented programming in Java. Exercise 1 describes the modeling of a pencil and a colored pencil with inheritance. Exercise 2 focuses on the modeling of concepts related to cities and capitals. Exercise 3 concerns document management in a library with inheritance between document classes. Exercise 4 introduces the Library and Livrothèque classes for the management of document collections.

Translated by

ScribdTranslations
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

TD 3 java

Aroua Hedhili Sbai


March 2020

Object-Oriented Programming
ING3

Exercise 1
This involves modeling a colored pencil. To do this, write the two classes:

1. a Crayon class that uses two private attributes thickness and length which will be initialized to
the help of the constructor of this class,

2. a ColorPencil class that inherits from the Pencil class and will have an additional attribute
color which will be a string of characters.
These two classes have constructors that allow initializing their attributes.

3. The operations that one wishes to perform on a pencil (colored or not) are:

a) modify the value of its attribute length through a method named changeLength which
returns nothing; we ensure that the length of the pencil cannot be negative.
(b) modify the value of its thickness attribute using a method named changeThickness which
returns nothing; we ensure that the thickness of the pencil cannot be negative.
display the characteristics of a pencil through a method named description.

4. The operations we want to perform on a colored pencil are:

a) modify the value of its color attribute through a method named changeColor which does not
return nothing.
b) modify the values of all its attributes (length, thickness, and color) through a method
namedChangeCharacteristics that returns nothing and will use the other methods.
display the characteristics of a colored pencil, by redefining and using it
method description of a standard pencil.
´
5. Write a class TestCrayon containing a main method to test these classes.

1
Exercise 3
1. Modeling the concept of city
A city is described by its name and its population.

The name of a city cannot vary; it must be known from the instantiation of the object. By the
This name will serve as a key for information search. Therefore, this name needs to be stored.
in uppercase. For this, you have the method toUpperCase of the String class.
The number of inhabitants may be unknown at the time of instantiation. It must always be
must be greater than 0. It must be able to vary for the same objectCity.
A Ville object must provide a method String getInformation() that allows to obtain
to derive the information (attributes) encapsulated in the form of a string of characters.

2. Modeling the concept of capital


A capital is a city and contains the information of the country where it is located. An objectCapital.
must provide a method String getInformation() that allows to know the information
(attributes) encapsulated in the form of a string.

3. Modeling of an Atlas
We now want to memorize a set of cities (and the capitals). Write the class Atlas.´
which implements an array structure for storing cities. This class must provide
the following two methods:

(a) void insert(Ville v) insert a City based on its name.


(b) City search(String key) that returns the City object whose name is passed as a parameter.
´
4. Write the class TesAtlas containing a method main to test these classes.

Exercise 4
In view of managing a library, we are asked to write an application to process some
documents of various nature: books, which can be novels or manuals, magazines,
dictionaries, etc.
All documents have a registration number (an integer) and a title (a string of characters).
Books also have an author (a string) and a number of pages (an integer). Novels have
possibly a literary prize (an integer, among: GONCOURT, MEDICIS)1 , NOBEL2 , etc), while
that the manuals have a school level (whole). The magazines have a month and a year (integers) and
Dictionaries have a language (an integer, among ENGLISH, GERMAN, SPANISH, etc.).
These various types of things must be able to be handled as documents.
´
1. Write the classes Document, Book, Novel, Manual, Magazine, and Dictionary, between which exists
they will break the heritage links that the previous description suggests.
1 French literary prize established in 1958 and awarded to a novel or a collection of short stories by a language author.
French. Since 1970, it has also awarded a foreign writer.
2 Award given by various Swedish or Norwegian institutions or academies. It is awarded every year to authors.
notable contributions in various fields: physics, chemistry, physiology or medicine, literature, peace,
economic sciences (since 1969).

2
In each of these classes, define the constructor that takes as many arguments as there are attributes.
and which is limited to initializing these with the values of the arguments. Also define a
method toString producing a description in the form of a string of characters of instances
from the class. Finally, if the instance variables have been declared private, also define
public accessors allowing to consult the values of these variables.
Ẃrite a class TestDocuments that builds and displays a series of class documents.
differents.

2. A library will be represented by an array of documents. Define a class Library,


with the methods:

Library(int capacity): constructor that creates a library with the capacity (number
maximum number of documents indicated,

void displayDocuments(): displays all the works in the library,


void displayAuthors(): displays the list of authors in the library (if needed, use
the operator instanceof
boolean add(Document doc): adds the document referenced by doc and returns true (false
in case of failure)
Document document(int i): returns the i-th document,
•boolean delete(Document doc): deletes the document referenced by doc and returns true
(false in case of failure)
´
3. Write, with minimal effort, a class Library that has the same functionalities as Bib-
the library is entirely made up of books.

You might also like