Object-Oriented Programming with Java, part I + II

cc

This material is licensed under the Creative Commons BY-NC-SA license, which means that you can use it and distribute it freely so long as you do not erase the names of the original authors. If you make changes in the material and want to distribute this altered version of the material, you have to license it with a similar free license. The use of the material for commercial use is prohibited without a separate agreement.

Authors: Arto Hellas, Matti Luukkainen
Translators to English: Emilia Hjelm, Alex H. Virtanen, Matti Luukkainen, Virpi Sumu, Birunthan Mohanathas, Etiënne Goossens
Extra material added by: Etiënne Goossens, Maurice Snoeren, Johan Talboom

The course is maintained by Technische Informatica Breda



Exercise assignments-7-1: Airport

Every week, you will find one or more larger exercises, where you can design the program structure freely, the appearance of the user interface and the requred commands are predefined. The first exercise which you can design freely in Advanced Programming is Airport.

Attention: you can create only one Scanner object so that your tests would work well. Also, do not use static variables, the tests execute your program many different times, and the static variable values left from the previous execution would possibly disturb them!

In the airport exercises we create an application to manage an airport, with its airplanes and flights. As far as the planes are concerned, we always know their ID and capacity. As for the flights, we know the plane used for the flight, the departure airport code (for instance HEL) and the destination airport code (for instance BAL).

There can be various different flights and planes. The same plane can also go on various different flights (various different routes). The application must offer two different panels. First, the airport worker inputs the flight and plane information to the system in the airport panel.

When the user exits the airport panel, the user then proceeds to use the flight service. The flight service has three actions: printing planes, printing flights, and printing airplane information. In addition to this, the user can exit the application by choosing x. If the user inputs an invalid command, the command is asked again.

Airport panel
--------------------

Choose operation:
[1] Add airplane
[2] Add flight
[x] Exit
> 1
Give plane ID: HA-LOL
Give plane capacity: 42
Choose operation:
[1] Add airplane
[2] Add flight
[x] Exit
> 1
Give plane ID: G-OWAC
Give plane capacity: 101
Choose operation:
[1] Add airplane
[2] Add flight
[x] Exit
> 2
Give plane ID: HA-LOL
Give departure airport code: HEL
Give destination airport code: BAL
Choose operation:
[1] Add airplane
[2] Add flight
[x] Exit
> 2
Give plane ID: G-OWAC
Give departure airport code: JFK
Give destination airport code: BAL
Choose operation:
[1] Add airplane
[2] Add flight
[x] Exit
> 2
Give plane ID: HA-LOL
Give departure airport code: BAL
Give destination airport code: HEL
Choose operation:
[1] Add airplane
[2] Add flight
[x] Exit
> x

Flight service
------------

Choose operation:
[1] Print planes
[2] Print flights
[3] Print plane info
[x] Quit
> 1
G-OWAC (101 ppl)
HA-LOL (42 ppl)
Choose action:
[1] Print planes
[2] Print flights
[3] Print plane info
[x] Quit
> 2
HA-LOL (42 ppl) (HEL-BAL)
HA-LOL (42 ppl) (BAL-HEL)
G-OWAC (101 ppl) (JFK-BAL)

Choose operation:
[1] Print planes
[2] Print flights
[3] Print plane info
[x] Quit
> 3
Give plane ID: G-OWAC
G-OWAC (101 ppl)

Choose operation:
[1] Print planes
[2] Print flights
[3] Print plane info
[x] Quit
> x

Attention: for the tests, it is essential that the user interface works exactly as displayed above. In fact, it is a good idea to copy-paste the menus printed by the above program into your code exactly. The tests do not require that your program should be prepared to deal with invalid inputs. This exercise is worth three single excercise points.

The program must start by executing the main method in the exercise layout.

Still another remark: in order to make your tests work, your program has to create only one Scanner object. Also, avoid using static variables: the tests execute your program many different times, and the static variable values left from the previous execution would possibly disturb them!

Exercise assignments-7-2: Car Registration Centre

Exercise assignments-7-2.1: Registration Plate Equals and HashCode

European registration plates are composed of two parts: the country ID – one or two letters long – and possibly a regitration code specific for the country, which in turn is composed of numbers and letters. Registaration plates are defined using the following class:

public class RegistrationPlate {
   // ATTENTION: the object variable types are final, meaning that their value cannot be changed!
   private final String regCode;
   private final String country;

   public RegistrationPlate(String regCode, String country) {
      this.regCode = regCode;
      this.country = country;
   }

   public String toString(){
       return country+ " "+regCode;
   }
}

We want to store the registration plates into say ArrayLists, using a HashMap as key. As mentioned before, it means we have to implement the methods equals and hashCode in their class, otherwise they can’t work as we want.

Suggestion: take the equals and hashCode models from the Book example above. The registration plate hashCode can be created say combining the hashCodes of the country ID and of the registration code.

Example program:

public static void main(String[] args) {
   RegistrationPlate reg1 = new RegistrationPlate("FI", "ABC-123");
   RegistrationPlate reg2 = new RegistrationPlate("FI", "UXE-465");
   RegistrationPlate reg3 = new RegistrationPlate("D", "B WQ-431");

   ArrayList<RegistrationPlate> finnish = new ArrayList<RegistrationPlate>();
   finnish.add(reg1);
   finnish.add(reg2);

   RegistrationPlate new = new RegistrationPlate("FI", "ABC-123");
   if (!finnish.contains(new)) {
       finnish.add(new);
   }
   System.out.println("Finnish: " + finnish);
   // if the equals method hasn't been overwritten, the same registration plate is repeated in the list

   HashMap<RegistrationPlate, String> owners = new HashMap<RegistrationPlate, String>();
   owners.put(reg1, "Arto");
   owners.put(reg3, "Jürgen");

   System.out.println("owners:");
   System.out.println(owners.get(new RegistrationPlate("FI", "ABC-123")));
   System.out.println(owners.get(new RegistrationPlate("D", "B WQ-431")));
   // if the hashCode hasn't been overwritten, the owners are not found
}

If equals hashCode have been implemented well, the output should look like this:

Finnish: [FI ABC-123, FI UXE-465]
owners:
Arto
Jürgen

Exercise assignments-7-2.2: The Owner, Based of the Registration Plate

Implement the class VehicleRegister which has the following methods:

  • public boolean add(RegistrationPlate plate, String owner), which adds the parameter owner of the car which corresponds to the parameter registration plate. The method returns true if the car had no owner; if the car had an owner already, the method returns false and it doesn’t do anything

  • public String get(RegistrationPlate plate), which returns the car owner which corresponds to the parameter register number. If the car was not registered, it returns null
  • public boolean delete(RegistrationPlate plate), which delete the information connected to the parameter registration plate. The method returns true if the information was deleted, and false if there was no information connetted to the parameter in the register.

Attention: the vehicle register has to store the owner information into a HashMap<RegistrationPlate, String> owners object variable!

Exercise assignments-7-2.3: More for the Vehicle Register

Add still the following methods to your VehicleRegister:

  • public void printRegistrationPlates(), which prints out all the registration plates stored
  • public void printOwners(), which prints all the car owners stored. Each owner’s name has to be printed only once, even though they had more than one car