Faculty of Computer Science - Bachelor in Applied Computer Science

Bachelor in Production Engineering

Introduction to Programming - A.A. 2006/2007

**Exercise 5A**

Implement a class `Bookstore`

for representing bookstores having
books in two languages: Italian and German. Each `Bookstore`

object
should maintain the following information:

- the number of books in Italian language;
- the number of books in German language.

The `Bookstore`

class should export to its clients the following
methods:

- A constructor to create a
`Bookstore`

object with a given initial number of books. The constructor has two parameters: one indicates the initial number of books in Italian language, another the initial number of books in German language. -
`addBookI`

, which takes one parameter of type`int`

and increases the number of books in Italian language by its value. -
`addBookG`

, which takes one parameter of type`int`

and increases the number of books in German language by its value. -
`getOverallBooksNumber`

, which returns the total number of books (both in Italian and German language) stored in a bookstore. -
`getPercentageOfBooksI`

, which returns the percentage of books in Italian language. The required percentage is a real number (i.e.,`float`

) and is equal to:*100.0 * number_of_books_in_italian_language / overall_number_of_books*

The method should work correctly also in the case where the overall number of books is 0, and in this case it should return 0 as percentage. -
`getPercentageOfBooksG`

, which returns the percentage of books in German language. The required percentage is a real number (i.e.,`float`

) and is equal to:*100.0 * number_of_books_in_german_language / overall_number_of_books*

The method should work correctly also in the case where the overall number of books is 0, and in this case it should return 0 as percentage.

Finally, implement a client class which:

- Reads from the keyboard the initial number of books in Italian language, and the number of books in German language
- Creates a
`Bookstore`

object with a given initial number of books. - Reads from the keyboard an additional number of books in Italian language and updates the object with its value.
- Reads from the keyboard an additional number of books in German language and updates the object with its value.
- Prints the percentage of books in Italian language.
- Prints the percentage of books in German language.
- Depending on the percentage of books in each language, prints one of the
messages:
- "
`The majority of books is in Italian`" - "
`The majority of books is in German`" - "
`The numbers of books in Italian and German are equal`"

- "

**Exercise 5B**

Plan and implement a class `Car`

with the following properties:

`licensePlate`

is a string holding the car's license number;`mileage`

is an integer holding the car's total covered distance in km;`acquisitionYear`

is an integer holding the year the car was first bought;`basePrice`

is a double holding the car's base price when it was first bought;`owner`

is the current car's owner.

The class `Car`

should define the following methods:

- the constructor: it takes the license number, the acquisition year and
the base price as parameters and sets
`mileage`

to`0`

and`owner`

to`null`

; - so called
*"getters"*: i.e. methods for all attributes that return the field's value; - a method
`addToMileage`

that increments`mileage`

by an integer value, passed as parameter; - a method
`setOwner`

that sets the`owner`

field to a string value, passed as parameter.

Furthermore, design and implement a client class with a *static* method
`getCurrentValue`

that computes the car's current value, given
a car object and the current year. Use the equation:

where "

`pow`

method from the `java.lang.Math`

package.