Week 4 session exercises
NB! The set of practice problems will always be somewhat larger, so that those who are quicker or have more experience still have something to do. Don't worry if you don't reach the end. Take your time and solve those exercises that seem interesting and/or reasonably challenging to you.
Task 1. Devices
Write an object class Device that has the following private instance fields: code (int
), name (String
) and price without the sales tax (double
). In addition, the object class has a static field (double
) for the rate of sales tax (in percent). In Estonia, the sales tax is 20%.
The object class should have a constructor and the following methods:
- get and set methods for all instance fields.
- Method priceWithTax with return type
double
, returning the total price of the item. The total price is item price + the sales tax. For example, if the price without the sales tax is 100€, then the total price is 120€. - Method toString which returns the information about the item in a readable way. For example Android Smart LED TV, which has the price 120€ with the sales tax.
In the main class, create several devices. Print info about them.
Task 2. Static or Nonstatic
Try to run the following code:
public class Person { private String name; private double height; // in meters public Person(String personName, double personHeight) { name = personName; height = personHeight; } // ski length in cm public int skiLength() { return (int) Math.round(0.85 * height * 100); } public static void main(String[] args) { System.out.println("Ski length is " + skiLength()); } }
Why doesn't the program compile? Make the necessary changes so that the program compiles.
Hint: adding the static
keywords is wrong solution.
Task 3. Flights
1) Write an object class Plane that has private instance fields for the name (String
), average speed (double
) and maximum number of passengers (int
).
The object class also has a constructor and the following instance methods:
- get and set methods for all instance fields;
- method toString which returns the info about the plane in readable way.
2) Write an object class Flight that has private instance fields for destination name (String
), distance to the destination (int
), plane (Plane
) and the number of tickets sold (int
).
The object class has a constructor and the following instance methods:
- all instance fields have get and set methods;
- a method (
int
) which returns the duration of the flight in minutes (use plane's average speed to calculate the duration of the flight); - a method (
int
) which returns the number of free seats in the plane (use the number of sold tickets and the maximum number of passengers in the plane); - method toString which returns info about the flight in a readable way.
3) In the main class, create at least two planes. For example a Fokker 50 with average speed 530 km/h and maximum number of passengers 46; a BN-2 with average speed 170 km/h and maximum number of passengers 9. Also create at least three flights.
Demonstrate the use of different instance methods: find the durations of flights and numbers of free seats, using the instance methods of the corresponding objects.
Task 4. Hedge
1) Class Shrub has private instance fields for shrub's height (double
) and width (double
). The class has a constructor and at least the following methods.
- get and set methods for all instance fields;
- a method (
double
) with no arguments, returning estimated yearly growth of the shrub. This is a random number between 0 and 0.1*height (no need to round it). - method toString for displaying shrub's data on the screen. Among the data also the estimated yearly growth is shown (use the method above).
2) Class Hedge has instance fields for the name of the hedge (String
) and for the array of shrubs (Shrub[]
) in the hedge. The class contains a constructor and at least the following methods.
- A method with return type
double
, which returns the height of the tallest shrub in the hedge. - A method with return type
double
, which returns the length of the hedge, i.e. the sum of all widths of the shrubs in the hedge. - Method toString that displays the data about the hedge on the screen; it should print the name of the hedge, height of the tallest shrub and the length of the hedge (use corresponding methods).
3) In the main class the following activities are performed.
- Five shrubs are created (instances of class
Shrub
). - All shrubs are added to an array of type
Shrub[]
. (The array can also be created before and filled with instances successively.) - Using the abovementioned array, one instance of class
Hedge
is created. - The data about the hedge are printed on the screen.
- Using the loop the shrubs in the array are printed on the screen.