Professional Documents
Culture Documents
FirstExam PhoneBook SOL
FirstExam PhoneBook SOL
In this Exam you will write a phone book application that contains Contact class
and phonebook class.
PART 1:
class Contact:
The class represents a single contact in the phone book.
Here are the features of the class (private variables):
First name String
Last name String
Mobile Phone Number String
The class will have three constructors:
1. A classic constructor who receives all the features of the object and his signature
below:
public Contact (String firstName, String lastName, String number)
2. A constructor that receives an array of strings) in the first cell will be the first
name, in the second cell the family name and in the third cell the phone number
public Contact (String [] personalDetails)
2
3. A copy constructor who receives another contact and copies all of its features
public Contact (Contact other) {
Class Contact Methods:
1. Getters for all four features must be implemented.
2. Setter methods should be implemented solely for the phone number feature
3. An equals method public boolean equals(Contact other) that accepts another
contact and returns true (as a Boolean variable) is all their features are equal.
Otherwise it returns false
4. The toString() method that returns the string representing the contact should be
implemented as follows:
firstName lastName: - phoneNumber (means first name, space, last name, space,
colon, hyphen, space and phone number).
You may conclude that all attributes exist and are not equal to null
PART2 :
PhoneBook class:
Good Luck.
4
//A classic constructor who receives all the features of the object and his
signature below:
public Contact (String firstName, String lastName, String number){
FirstName = firstName; 1
LastName = lastName; 1
PhoneNumber = number; 1
}
//2. A constructor that receives an array of strings) in the first cell will be the first
name, in the second cell the family name and in the third cell the phone number
public Contact (String [] personalDetails){
FirstName = personalDetails[0]; 1
LastName = personalDetails[1]; 1
PhoneNumber = personalDetails[2]; 1
}
//3. A copy constructor who receives another contact and copies all of its
features
public Contact (Contact other) {
FirstName = other.FirstName; 1
LastName = other.LastName; 1
PhoneNumber = other.PhoneNumber; 1
5
}
//Getters for all four features
public String getFirst(){ 2
return FirstName;
}
public String getLast(){ 2
return LastName;
}
public String getNumber(){ 2
return PhoneNumber;
}
//2. Setter methods
public void setFirst(String first){ 2
FirstName = first;
}
public void setLast(String last){ 2
LastName = last;
}
* and of course must initialize the index of the last added contact to zero.
*/
public PhoneBook(){ 2
phonebook = new Contact[100];
last = 0;
}
/*A constructor that accepts the size of the phonebook,
* initializes the contacts array with the size of the number
* received and the last contact index added to zero.
*/
public PhoneBook(int size){ 2
phonebook = new Contact[size];
last = 0;
}
if (almostFull()){
System.out.println("Pay attention the phonebook is almost full");
return false;
}
phonebook[last] = new Contact(toAdd);
last++;
return true;
}
/* The printAllContacts method - does not accept parameters and does not return
a value.
* It prints the array of contacts one after the other (by using their toString().
* A line must be dropped between one contact to another.
*/
public void printContacts(){ 5
for(int i= 0; i<last; i++)
System.out.print(phonebook[i].toString()+"\n");
}
}