Chapter 3 |
Tasks (to be submitted by Mon 2.03)
Task 1
File Person.java contains object class Person:
public class Person { String name; // instance field for the name double height; // instance field for the height // constructor Person(String personName, double personHeight) { name = personName; height = personHeight; } }
Another file Test.java contains a client class with the main method:
public class Test { public static void main(String[] args) { Person a = new Person("John Oak", 1.99); System.out.println("Person a is " + a); } }
1. If we run the program, the information about the person will be in a form that is not readable. In the object class Person, add method toString() which prints information of an instance in readable way.
2. In the client class, create a few instances and print the info about the persons.
3. Add a few instance fields into the object class (each person should have at least the following properties: name, height, weight, and idNumber).
4. Add a constructor (with four parameters).
5. Add getters and setters for the instance fields. Note:
- getters and setters make sense if the instance fields have private modifier;
- the idNumber must be set in the constructor and it cannot be changed later;
- if the height or weight are changed, the new values have to be checked if they are valid (e.g. height range can be in the range of 45 to 230 cm).
6. Add at least two your own methods into the object class (e.g. a method for body mass index calculation or a method for the ski length calculation). At least one of the methods should have parameters. Above the method headers, add comments on the methods' functionality. Call the methods in the client class.
Task 2
File Book.java contains object class Book:
public class Book { private String author; // to be changed public Book(String author, String title) { this.author = author; this.title = title; } }
1. Change the object class so that the data type of the author would be Person (not String):
private String author; --> private Person author;
2. Create a client class with several instances of class Book.
3. Create an array called library and add all the books into the array.
Task 3
In object class PowerStation, there is a private instance field of double
type called powerPrice. The field is set in the constructor. The field can be accessed through the corresponding getter and setter methods.
Another object class - called ElectricCar has private instance fields called carMake (String
), powerConsumption (double
) per 100 km (kWh/100km), chargingTime in minutes (int
), distance (int
) that indicates the distance in km that is possible to drive per one charge, and station (PowerStation
). In the class, there is a constructor that sets all the fields. In the class, there are some methods as well:
- getters and setters for accessing chargingTime and station (in the setter of chargingTime, there should be a check for data validity)
- an instance method without parameters and of
double
return type called payment100 - the method returns the price per 100-km drive (formula: power consumption times price for the power) - an instance method of
double
return type called payment - the method has one parameter ofint
data type for the distance in km and returns the price for the trip by the car (formula: trip distance times the price per 100-km drive divided by 100) (use the corresponding method) - an instance method of
double
return data type called tripDuration - the method has two parameters: the distance of the trip in km (int
) and the average speed in km/h (double
); the method tripDuration returns the time spent on the trip (formula: (tripDistance/distance)*chargingTime (in hours!) + tripDistance/speed) - method toString that prints info about the car in a readable way; the output must contain info about the price per 100 km (use the corresponding method)
Finally, create a client class with the main method. Create an instance of class PowerStation who sells power for 0.15 eur/kWh. Also create instances of class ElectricCar. In the client class, demonstrate the calls of different instance methods. The output must always contain a clear and understandable comment about the output info.
Chapter 3 |