wiki:k2014/demot/demo5english
Last modified 3 years ago Last modified on 2015-02-13 11:12:50

Demot » 16.2.2015

Task 1*. Persons toString and parse

Continue from the last demos Person task (7-8) adding:

  1. Add parameterless constructor to the class.
  2. Add method parse, which will take the information from a string that has "|" as the differentiator (for example Donald|Duck|1999).
  3. Write a constructor that takes a string as parameter and invoke parse-method on the string.
  4. Finally add print-method to the class:
    public void print(OutputStream os) {
        PrintStream out = new PrintStream(os);
        out.println(firstName + " " + lastName + " " + birthYear);
    }
    

This kind of main should work with the class:

public static void main(String[] args) {
    Person per = new Person();
    Person donald = new Person("Donald", "Duck", 1934);
    System.out.println(per);
    per.parse("Sepe|Susi|1933");
    per.print(System.out);
    donald.print(System.out);
    Person mickey= new Person("Mickey|Mouse");
    System.out.println(mickey);
}

Should print

||0
Sepe Susi 1933
Donald Duck 1934
Mickey|Mouse|0

Task 2*. Date-class

Add to the date-class getters for day, month and year(Pvm.java). You do not have to do the TODO-parts yet.

Usage example:

Pvm pvm = new Pvm();
pvm.parse("3.4.2011");
System.out.println(pvm.getPv()); // prints 3

Attention! The needed mjonot is found in Ali.jar package.

Task 5*. Bus

Create a class Bus that stores the amount of maximum amount of seats and the amount of free seats. Create methods print and add and reduce to change the amount of passengers. Write a test main.

Task 6*. Bus extended

Modify task 5's answer so that the class Bus can be used in the next main method:

public static void main(String[] args) {
    Bus littleBus = new Bus(10);
    Bus bigBus = new Bus(45);
    littleBus.add(4);    littleBus.print(System.out); // 10,4,6
    bigBus.add(30);      bigBus.print(System.out);   // 45,30,15
    int residue = littleBus.add(15);
    bigBus.add(residue );    
    littleBus.print(System.out);                         // 10,10,0
    bigBus.print(System.out);                           // 45,39,6
    if ( littleBus.getSpaceLeft() > 0 )
        System.out.println("There is room in the bus!");   // won't be printed
    if ( bigBus.isRoom() )
        System.out.println("There's room left");     // will be printed
    int tooMany = littleBus.reduce(12);                  // tooMany = -2
    if ( tooMany < 0 )                                      
        System.out.println("There's not even this many people in the bus!");
    littleBus.print(System.out);                         // 10,0,10
}