Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

1

Advanced Programming (0303204)


First Exam
Duration: 1.5 Hour
Student name:-------------------- Student#:-------------------
This exam has one test question with two parts. Try to maintain the simplicity of the writing .

Write your answers on the answer notebook.

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:

The class represents the phone book itself,


The phone book will have only two features (private of course):
array of contacts Contact[]
The index of the last contact added int
The constructors of PhoneBook:
The class will have only two constructors:
1. A default constructor that does not accept parameters and initializes the contacts
array to size 100 (final of course) and of course must initialize the index of the last
added contact to zero. and its signature: public PhoneBook()
2. 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.
3

public PhoneBook(int size)


Methods of the PhoneBook class:
1. The addContact method - receives a pointer to an instance of a contact, copy it
using a the copy constructor and inserts the copy into the contacts array in the
appropriate place, i.e. after the last contact added. The method returns true if the
contact was entered into the array otherwise it returns false. Basically the method
will return false only if the array is full and there is no room to insert the contact.
Note that you must update the index attribute of the last contact added.
If the array is almost full (hint: the almostFull method below) a message must be
printed to the user: "Pay attention the phonebook is almost full”
Method signature:
public boolean addContact(Contact toAdd)
2. 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. Method signature:
public void printContacts()
3. The almostFull method - does not accept parameters, returns true if there are less
than 10% of free places in the contacts array: public boolean almostFull()

Good Luck.
4

public class Contact {


private String FirstName; 2
private String LastName; 2
String PhoneNumber; 2

//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;
}

public void setNumber(String number){ 2


PhoneNumber = number;
}
/* An equals method that accepts another contact and returns true
*(as a Boolean variable) is all their features are equal.
* Otherwise it returns false
*/
6

public boolean equals(Contact other){ 4


if(this.FirstName.equals(other.getFirst())&&
this.LastName.equals(other.getLast())
&& this.PhoneNumber.equals(other.getNumber()))
return true;
return false;
}
// The toString() method that returns the string representing the contact

public String toString(){ 3


return FirstName+" "+ LastName+" :-" + PhoneNumber;
}

public class PhoneBook {


Contact[] phonebook; 1
int last; 1

/* A default constructor that does not accept parameters


* and initializes the contacts array to size 100 (final of course)
7

* 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;
}

/* The addContact method - receives a pointer to an instance of a contact,


* copy it using a the copy constructor and inserts the copy into the contacts array
in the appropriate place, i.e. after the last contact added. The method returns true if
the contact was entered into the array otherwise it returns false. Basically the
method will return false only if the array is full and there is no room to insert the
contact.
*Note that you must update the index attribute of the last contact added.
* If the array is almost full (hint: the almostFull method below) a message must
be printed to the user:
* "Pay attention the phonebook is almost full”
*/
public boolean addContact(Contact toAdd){ 5
8

if (almostFull()){
System.out.println("Pay attention the phonebook is almost full");
return false;
}
phonebook[last] = new Contact(toAdd);
last++;
return true;
}

public boolean almostFull(){ 2


return last > phonebook.length;
}

/* 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");
}
}

You might also like