Java Object-Oriented Programming Exercises
Java Object-Oriented Programming Exercises
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.
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.
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:
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.
Library(int capacity): constructor that creates a library with the capacity (number
maximum number of documents indicated,