next up previous
Up: Unit 03 Previous: Inheritance or composition? (optional)

Exercises

Exercise 03.1 Define a classe Book to handle the information associated to books. The information of interest for a book are: the title, the authors, and the price. The methods of interest are:

Exercise 03.2 Write a Java class implementing an example client for the class Book of Exercise 3.1. The example class should perform the following operations:

  1. read from the keyboard the title and authors of a first book, and create a corresponding object;
  2. read from the keyboard the title and authors of a second book, and create a corresponding object;
  3. show the information about the first book;
  4. show the information about the second book;
  5. read the price of the first book and update the object accordingly;
  6. show the information about the first book, including the price.

Exercise 03.3 We want to realize a system for composing messages to send via a cell phone. Each message corresponds to a code. For example, to the code "ily" corresponds the complete message "I love you, darling.". Define the class MessageText to handle messages. The class must have:

Exercise 03.4 A message must contain the number to call, the number of the sender, and the message text. Define a class Message, with:

Exercise 03.5 Write an example program that uses the two classes MessageText and Message. The program should:

  1. inizialize a variable of type String that corresponds to the phone number of the sender;
  2. read the text and the code of a message, and store them in an object of type MessageText;
  3. read the phone number of the receiver;
  4. create an object of type Message;
  5. show the information about the object of type Message.

Define (and use) an auxiliary static method for reading a phone number from the keyboard. Solve the exercise so as to use all methods that have been defined in the classes MessageText and Message (possibly by creating and printing various messages).

Exercise 03.6 Modify the classes Book (Exercise 3.1) and MessageText (Exercise 3.3) by defining static methods to read the input from the keyboard. Test these methods by suitably modifying the example programs.

Exercise 03.7 Extend the class Book (Exercise 3.1), by defining other classes:

For each of the above classes, define the constructor and suitably redefine the method for the visualization of the object. Describe the hierarchy of classes.

Exercise 03.8 Write an example program for the classes defined in Exercises 3.6 and 3.7, that realizes the following operations.

  1. for a first book, read the title, the authors, the course, and the faculty adopting it, and store this information in an object;
  2. show the information about the first book;
  3. for a second book, read the title and the authors, and store them in a suitable object;
  4. read the topic of the second book, and create a third object of type novel;
  5. show the information about the two objects used to create the second book;
  6. read the price of the first book and update the corresponding object accordingly;
  7. show the information about the first book, including the price.


next up previous
Up: Unit 03 Previous: Inheritance or composition? (optional)