KING FAHD UNIVERSITY OF PETROLEUM & MINERALS

advertisement
KING FAHD UNIVERSITY OF PETROLEUM & MINERALS
Information and Computer Science Department
ICS-201 Introduction to Computer Science
Lab 02: Review of ICS 102 II
Objectives: Review of ICS 102 material
- Object oriented concepts
Exercise 1
A class called Student has the following instance variable:
private int id;
private double [ ] quiz = new int[4];
Write the following members of the class Student:
a. 2 constructors
b. 3 mutator:
i. One that changes the value of one quiz.
ii. One that changes the value of all quizzes.
iii. One that change the value of id
c. 3 accessors
i. One that returns the value of one quiz.
ii. One that returns the value of all the quizzes.
iii. One that returns the value of id.
d. A method that checks whether 2 students have the same score in every
quiz.
e. An appropriate toString method
f. A method that returns the sum of quizzes of a student.
Exercise 2
A class called DriverDorm uses the following class called Dorm.
class Dorm {
int id, room, line;
Dorm(int a, int b, int c) {
id = a;
room = b;
line = c;
}
public int getId() {
return id;
}
public int getRoom() {
return room;
}
public int getLine() {
return line;
}
public void setId(int a) {
id = a;
}
public void setRoom(int a) {
room = a;
}
public void setLine(int a) {
line = a;
}
public boolean AreRoommates(Dorm a) {
return a.line == line && a.room == room && a.id != id;
}
public String toString() {
return id + "\t" + line + "\t" + room;
}
}
Write the class DriverDorm that does the following:
 Declares and initializes 1000 Dorm objects
 Prints the id of a student, id of his roommate and his line and room
number.
 Prints the number of students without roommate
Exercise 3
 Create classes: Player and Team as follows.
 A Player has a name (String), a position (String), a number of games he played
numGames (integer), number of goals he scored numGoalsScored(integer),
and the number of goals his team has conceded numGoalsConceded (integer).
The position can take one of three values: "Attacker", "Defender", and
"GoalKeeper". To evaluate how good a player is, a performance measure can
be computed for every player as follows:
 Attacker: numGoalsScored / numGames
 Defender: (numGames + numGoalsScored – numGoalsConceded) /
numGames
 GoalKeeper: (numGames – numGoalsConceded) / numGames

Write the code of class Player and provide the following:
 Constructor that takes a two strings for the name and position.
 Accessor and mutator methods for the position
 Equals method
 toString Method
 getPerformance which returns the performance of a Player as described
above.

The Team class has two data members: ListPlayers which is an array of
Players and numPlayers (integer) which indicates the current number of
players in the team.
Give the code for Team class with a no-argument constructor that initializes
the array with size 23. Provide the following methods:
 addPlayer: to add a Player to the team. If the player is already in the team,
the program should print "the player is already in the team". If the team
contains already 23 players, print "maximum number of player reached".
 RemovePlayer: to remove a Player from the team. If the player is not in
the team, print "player not in the team".
Important: ListPlayers should always be contiguous (no gaps).
 bestPlayer: which returns the player with the highest performance.
 teamPerformance: which returns the average performance of the players.

Download