Lecture 5 OOP Exercises

Embed Size (px)

Citation preview

  • 8/14/2019 Lecture 5 OOP Exercises

    1/10

  • 8/14/2019 Lecture 5 OOP Exercises

    2/10

    Exercise 1Team and Players Program

  • 8/14/2019 Lecture 5 OOP Exercises

    3/10

    Interface HumanInterfaceWrite a Java program according to the following specifications:

    1. Develop an interface HumanInterface that includes four public methods:String getName(), String getAge(), void setName(String s),and voidsetAge(double d).

    2. When these methods are implemented, they should set or return the name orage of their object.

  • 8/14/2019 Lecture 5 OOP Exercises

    4/10

    Abstract Class HumanDevelop a public abstract class Human to the following specifications:

    1. The class implements HumanInterface.

    2. The class has two private instance variables: String name, and double age.

    3. The class has a two-argument constructor to set its instance variables to givenvalues.

    4. The class overrides the Objects toString()method in order to return a stringrepresentation of a Human object similar to format given below: Ali, 16 years old

    5. The class implements any additional necessary methods, if applicable.

  • 8/14/2019 Lecture 5 OOP Exercises

    5/10

    Class PlayerDevelop a public class Player to the following specifications:

    1. The class is a subclass of Human.

    2. The class has one private instance variable: int number.

    3. The class has a three-argument constructor to set its instance variables to givenvalues. This constructor should invoke the superclass constructor.

    4. The class has setter and getter methods for number.

    5. The class overrides the Objects equals method in order to compare the contentsof two objects of the type Human and return true if the states of the two objectsare identical, and false otherwise.

    6. The class overrides the toString()method in order to return a string representationof a Player object similar to format given below: Ali, 16 years old, Number: 3

    7. the toString ()method should invoke the parents toString()method to get the firstpart of the returned value.

  • 8/14/2019 Lecture 5 OOP Exercises

    6/10

    Class TeamDevelop a public class Team, that represents a list of players in a team, to thefollowing 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 players , which will represent the collection of players in the team.

    The class has a zero-argument constructor that does the following whenevercreating 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 theusers answer is yes, then the system should add all players using the addPlayers() methodwhich will be described shortly.

  • 8/14/2019 Lecture 5 OOP Exercises

    7/10

    1. The class has getter methods for name, size, and count.

    2. The class has a public method boolean addPlayer(Player pl) which is used to add oneplayer to the team if the team is not full. The method should add the player pl to theplayers, increment count, display a message that the player is added, and finally returntrue. If the team is full, an appropriate message should be displayed, and the methodshould return false.Hint : to check whether the team is full or not, use count and size.

    3. The class has a static method Player createPlayer(). The method should ask the user fora players name, age, and number, and then return an instance of Player initialized tothe obtained values. You must either use Scanner or BufferedReader to get users input.

    4. The class has a public method void addPlayers() which is used to add the remainingplayers to the team. For example, if the teams size is 12 and it already includes 5players (i.e. count = 5), then this method should add 7 more players to the team usingboth addPlayer and createPlayer methods. Note that the method should only addplayers if the team is not full, otherwise it should display an error message.

    5. The class overrides the toString()method in order to return a string representation of aTeam object, showing the teams name, number of players, and list of all players whojoined the team. For example:

    Team Egypt has 2 player(s)

    1- Ali, 18.0 yrs old, Number: 1.

    2- Ahmed, 19.0 yrs old, Number: 2.

  • 8/14/2019 Lecture 5 OOP Exercises

    8/10

    Class MainDevelop a public class Main to test the classes in Q1.1. This class will onlyhave the main method that should:

    1. create a Team instance with any appropriate state,2. add one player using the teams addPlayer method. Hint: use the static

    method createPlayer to create a player instance.

    3. Display the teams information.

    4. A sample run is shown in the figure below.

  • 8/14/2019 Lecture 5 OOP Exercises

    9/10

    Exercise 2

    Personid: intname: string

    setID (),setName (),toString()

    Employee

    job : StringplaceOfWork: String

    setJob(),setPlaceOfWork (),toString()

  • 8/14/2019 Lecture 5 OOP Exercises

    10/10

    END