Session 3 |
Tasks (to be submitted by Sun 4.03 23:55) 1 point
Taks 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[] argv) { Person a = new Person("Juhan Juurikas", 1.99); System.out.println("Person a is " + a); } }
1. If we run the program, the information about the person will not be readable. Update the object class Person with method toString().
2. In the client class, create a few instances and print 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, idNumber and weight).
4. Add a constructor (with four parameters).
5. Add to all instance fields private modifier. Add getters and setters for instance fields. Note:
- the idNumber must be set in the constructor and later it cannot be changed;
- if the height or weight are changed, the new values have to be checked if they are valid.
6. Add at least two methods of your own into the object class (e.g. a method for body index calculation or a method for the ski length calculation). At least one methods should take in parameters (the arguments must be new data, not the instance fields). 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; private String title; public Raamat(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 array called library and add all the books into the array.
Task 3.
In class PowerStation, there is an instance field of double
type called powerPrice. The field is set in the constructor. The field has the corresponding getter and setter methods.
In another class - called ElectricCar, there are private instance field 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 also some methods:
- getters and setters for chargingTime and station (in the setter of chargingTime, there should be a check for data validity)
- an instance method without arguments and of
double
return type called payment100 - the method shows 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 takes in one parameter ofint
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 type called tripDuration - the method takes in 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 cars 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.