Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
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 of int 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.

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment