Session 6 |
Tasks
Task 1. A nice day in the amusement park (study the code)
The director of the amusement park has signed a contract with you to develop a piece of software for his park. The software has to check children's age. In the amusement park, only 12+ years old teenagers can take a ride. To check the age, teenagers have to show a document. Usually, teenagers bring ID cards or students cards with them. To speed up the process, the documents are checked in bulk.
public class IdCard { private String idNumber; public IdCard(String idNumber) { this.idNumber = idNumber; } public boolean isAtLeast12YearsOld() { int currentYear = LocalDate.now().getYear(); return birthYear() <= currentYear - 12; } private int birthYear() { int firstDigit = Integer.parseInt(idNumber.substring(0, 1)); int bYear = Integer.parseInt(idNumber.substring(1, 3)); if (firstDigit <= 2) return 1800 + bYear; if (firstDigit <= 4) return 1900 + bYear; if (firstDigit <= 6) return 2000 + bYear; return 2100 + bYear; } }
public class StudentCard { private int birthYear; public StudentCard(int birthYear) { this.birthYear = birthYear; } public boolean isAtLeast12YearsOld() { return birthYear + 12 <= LocalDate.now().getYear(); } }
public class RussianMountains { public boolean ageControlOK(IdCard[] idCards, StudentCard[] studentCards) { for (IdCard id : idCards) { if (!id.isAtLeast12YearsOld()) return false; } for (StudentCard card : studentCards) { if (!card.isAtLeast12YearsOld()) return false; } return true; } }
Read about the LocalDate
class here and here.
In a week, the director of the amusement park contacts you again and says that some people bring driving license with them. Therefore, some changes must be done in the program.
If we have a look at the ageControlOK
method in the class RussianMountains
, then it is easy to change so that the method would take in all objects that have an instance method isAtLeast12YearsOld
. Let us create an interface for this method:
public interface Document { boolean isAtLeast12YearsOld(); }
Change the RussianMountains
class as follows:
public class RussianMountains { public boolean ageControlOK(Document[] documents) { for (Document doc : documents) { if (!doc.isAtLeast12YearsOld()) return false; } return true; } }
Furthermore, the classes IdCard
and StudentCard
have to implement the interface:
public class IdCard implements Document { // the rest code is not changed }
public class StudentCard implements Document { // the rest code is not changed }
public class Test { public static void main(String[] args) { RussianMountains mountain1 = new RussianMountains();//create a ride //create a bulk of different documents and assign to a variable to interface Document data type Document[] documents = { new IdCard("39108071234"), new StudentCard(1991) }; //check if all visitors can take a ride and print the result System.out.println("is age OK?: " + mountain1.ageControlOK(documents)); } }
Task 2. Return the documents (modify the previous code)
If a person has not passed the age control, the document has to be returned to the person. In the RussianMountain
class, create a method called ageControlFail
. The method takes in an array of documents and returns the list documents (List<Document>
) that fail the age control.
In the main method, print all the documents that failed the age control. Make sure that the output is readable (add a method called toString
into classes IdCard
and StudentCard
).
Hint. List
is an interface. That means it is not possible to create objects of this type. ArrayList
implements all the methods of List
and we can create objects of that class. Do not forget about a generic data type: ArrayList<Document>
.
Task 3. Ferris wheel
The amusement park has decided to open a new ride - Ferris wheel. The age restriction is 14 on this ride (the ride might be dangerous to small kids). In the Document
interface, delete the isAtLeast12YearsOld
method and replace it with a new method - isAtLeastXYearsOld
. The latter method should check if the person is at least x years old (x is a parameter).
Change classes IdCard
and StudentCard
so that they would implement the changes of the interface.
The RussianMontain
class must become more general. Therefore, rename the class to AmusementRide
(in IntelliJ: Refactor -> Rename). In the class, add private instance fields for the ride name (String
) and the minimum age when a person is allowed to take a ride on it (int
). Also add a constructor and the toString
method. Do not forget to change some signatures.
Create a new test class called Test2
. In its main method, create at least two amusement rides (one has at least 14 years old restriction, and the other one has at least 12 years old restriction). Also create an array with at least two documents (a student card whose holder is 13 years old and ID card whose holder is 15 years old). Print out information about each person if they can take a ride on each amusement ride.
Task 4. Fortune's wheel
The sales manager of the amusement park has decided to raffle off some special gifts among the most active visitors of the park. Write a program which returns a list of the most active visitors and chooses the winner out of them. In the final raffle off, only three visitors participate.
First of all, create Visitor
class which has a private instance field for visitor's name (String
) and a private instance field which keeps the number of rides (int
) made in the park. Add a constructor which instantiates all the instance fields. Also add the get
methods and the toString
method. The latter has to show visitor's name.
Create a class called FortuneWheel
. Create a private instance field which holds a list of visitors in an appropriate data structure. Using the overloading, create two versions of method addVisitor
: 1. the method takes in objects of the class Visitor
; 2. the method takes in two parameters: the name of a visitor and the number of rides the visitor has taken in the park.
In the class, also add the theMostActive(int n)
method. The method has to return n the most active visitors out of the list (or the whole list if n is larger than the size of the list). The most easier way to do that is to sort the list by the number of rides. We can use the method sort
of java.util.Collections
class. However, the sort
method needs some help from us. In Visitor
class, we have to explain Java what and how exactly we want to compare (check the example at the end of the page). Also pay attention to the fact that the parameter of the Collections.sort
method is the List
interface. Since ArrayList
implements the interface, we can use it as a parameter.
The method winnerOfLottery
does not have parameters and returns one out of the three the most active visitors (Hint: use method Math.random
).
Create a test class Test3
. In its main method create a Fortune's wheel and at least 5 visitors. Register the visitors to the lottery, test the fortune's wheel methods and print winner's name.
Submit at least the following files (preferably zipped) into the Moodle:
- RussianMountain.java
- AmusementRide.java
- Document.java
- IdCard.java
- StudentCard.java
- Visitor.java
- FortuneWheel.java
- Test.java
- Test2.java
- Test3.java
Session 6 |