Vous êtes sur la page 1sur 10

Lecture 5

Exercises on Object oriented

Exercise 1
Team and Players Program

Interface HumanInterface
1.

Write a Java program according to the following specifications: Develop an interface HumanInterface that includes four public methods: String getName(), String getAge(), void setName(String s),and void setAge(double d). When these methods are implemented, they should set or return the name or age of their object.

2.

Abstract Class Human

Develop a public abstract class Human to the following specifications:


1.
2. 3.

The class implements HumanInterface.


The class has two private instance variables: String name, and double age. The class has a two-argument constructor to set its instance variables to given values.

4.

The class overrides the Objects toString()method in order to return a string representation of a Human object similar to format given below: Ali, 16 years old
The class implements any additional necessary methods, if applicable.

5.

Class Player

Develop a public class Player to the following specifications:


1. 2.

The class is a subclass of Human. The class has one private instance variable: int number.

3.

The class has a three-argument constructor to set its instance variables to given values. This constructor should invoke the superclass constructor.
The class has setter and getter methods for number. The class overrides the Objects equals method in order to compare the contents of two objects of the type Human and return true if the states of the two objects are identical, and false otherwise. The class overrides the toString()method in order to return a string representation of a Player object similar to format given below: Ali, 16 years old, Number: 3 the toString()method should invoke the parents toString()method to get the first part of the returned value.

4. 5.

6.

7.

Class Team

Develop a public class Team, that represents a list of players in a team, to the following specifications: (21 marks)
The class has four private instance variables:
String name, which represents the name of the team. int count, which indicates the current number of players enrolled in the team. int size, which indicates the maximum number of players that can be enrolled in the team. This number will be obtained later from the user. ArrayList<Player> players, which will represent the collection of players in the team.

The class has a zero-argument constructor that does the following whenever creating a new team instance:

Asks the user about the team name, and sets name accordingly. Asks the user about the maximum number of players, and sets size accordingly. Declares the players ArrayList with an initial capacity equal to size. Asks the user whether s/he wants to enter the information of all teams players. If the users answer is yes, then the system should add all players using the addPlayers() method which will be described shortly.

1. 2.

The class has getter methods for name, size, and count. The class has a public method boolean addPlayer(Player pl) which is used to add one player to the team if the team is not full. The method should add the player pl to the players, increment count, display a message that the player is added, and finally return true. If the team is full, an appropriate message should be displayed, and the method should return false. Hint: to check whether the team is full or not, use count and size. The class has a static method Player createPlayer(). The method should ask the user for a players name, age, and number, and then return an instance of Player initialized to the obtained values. You must either use Scanner or BufferedReader to get users input.

3.

4.

The class has a public method void addPlayers() which is used to add the remaining players to the team. For example, if the teams size is 12 and it already includes 5 players (i.e. count = 5), then this method should add 7 more players to the team using both addPlayer and createPlayer methods. Note that the method should only add players if the team is not full, otherwise it should display an error message.
The class overrides the toString()method in order to return a string representation of a Team object, showing the teams name, number of players, and list of all players who joined the team. For example:

5.

Team Egypt has 2 player(s) 1- Ali, 18.0 yrs old, Number: 1. 2- Ahmed, 19.0 yrs old, Number: 2.

Class Main

Develop a public class Main to test the classes in Q1.1. This class will only have the main method that should: create a Team instance with any appropriate state,

1.

2.

add one player using the teams addPlayer method. Hint: use the static method createPlayer to create a player instance.
Display the teams information. A sample run is shown in the figure below.

3. 4.

Exercise 2
Person id: int name: string setID (), setName (), toString() Employee job : String placeOfWork: String setJob(), setPlaceOfWork (), toString()

END

Vous aimerez peut-être aussi