CMPS 200 Final, Spring 2015
American University of Beirut · Introduction to Programming · 4 pages · Dr. Marcel Karam
More CMPS 200 files
- Final Exam Fall 2015 · Fall 2015Final examView
- Final · Summer 2015Final examView
- Final Answers · Fall 2015Final exam · with solutionView
- Final · Fall 2015Final examView
- Final · Fall 2014Final examView
- Lab Final Quiz Solution (no Question) · Fall 2012Final exam · with solutionView
- Final Questions- Answers · Fall 2012Final exam · with solutionView
- Final · Fall 2005Final examView
Show the text of this final exam
Faculty of Arts & Sciences
Department of Computer Science
CMPS 200—Introduction to Programming
Final Exam (2 hours), Sections 4, 5, and 6
1866
Problem1 PhonebookEntry. java & Phonebook. java (78%)
tn this problem, you will need to create a program that emulates a phonebook. Specifically, you will first
create the data type PhonebookEntry, which stores and maintains information about a phonebook
entry. You will then create the data type Phonebook that keeps track of all entries such that duplicate
entries are allowed. Finally, you will create a program that allows the user to manipulate the phonebook
through a command line menu, which will test all the methods that you will need to implement.
The class PhonebookEntry is represented by the following API:
public class PhonebookEntry
// Private Fields
firstName: String
lastName: String
phoneNum: int // the phone number would have 8 digits (e.g., 01350000)
// Full constructor
PhonebookEntry(String fName, String 1Name, int num)
// Changes the number of the current object to newPhoneNum
void changeNumber (int _newPhoneNum)
Hf Changes the last name of the current object to newLastName
void changeLastName(String newLastName)
// Returns true iff the object has the same last name as lastName
boolean | hasSameLastName (String lastName)
# String representation of a an entry with the following format:
H/ FirstName LastName: xx-xxxxxx (Note: you need to format the number properly)
// Example: Lionel Messi: 71-922234
String | toStringQ)
The class Phonebook is represented by the following API:
public class Phonebook
H/ Private Fields
phoneBook: PhonebookEntry [] // an array of phonebook entries
currentEntry: int // keeps track of the next available entry in the phonebook
// Also used to know where to insert an entry.
size: int // size of the array
// Full constructor, takes the size of the array as parameter, and sets it to the corresponding field
// initializes the array accordingly, and sets the currentEntry to 0
Phonebook(int STZé)}
Hf Returns true iff the phonebook is full {i.e., if the currentEntry is equal to the size)
boolean | isFull()
// Adds an entry into the phonebook. Returns true if the add was done successfully;
// If fatse is returned, no entry is added to the current object.
// Hint: an entry cannot be added if the phonebook is full
// The method must create a new entry and fill its information using the Scanner object
boolean | add(Scanner console) throws IOException
// Prints out all entries that have the last name lastName with the following format.
7/ For example if the phonebook contains three entries:
// 0. tionel Messi: 71-922234
// 1. Cristiano Ronaldo: 03-923219
Hf 2. Eden Hazard: 70-111100
void searchAndPrint (String lastName)
// Returns the number of entries with the last name matching lastName.
int searchNumMatches (String lastName)
Spring 2014-15 1 of 4
Final Exam Introduction 1o Computer Programming (CMPS 200)
// Returns the index of the first entry in the phonebook with lastName.
// If no such entry exists, -1 is returned.
int getIndex (String lastName)
// Changes the phone number of a phonebook entry at index, to newNum
void changeNumber(int index, String newNum)
// Asks the user for the entry to change its phone number based on the last name.
// if the entry's last name is not available, the method must notify the user
// If one entry exists, the method must ask the user to input the new phone number,
// and then perform the change
// if multiple entries exist, the method must ask the user to specify which index/number
. // to change its phone number
void doChangeNumber(Scanner console) throws IOException
// String representation of the phonebook with the following format.
// For example if the phonebook contains three entries, the sting representation will be as follows
// (with one entry per line):
// 0. Lionel Messi: 71-922234
// 1, Cristiano Ronaldo: 03-923219
. // 2. Eden Hazard: 70-111100
String toString()
Write a main method (in the same class as Phonebook) that provides the user with a menu to create and
manipulate a phonebook. You must also create appropriate static methods (if need be), which shows
structure in your code and eliminates redundancy.
Sample run: the program will first ask the user to enter the size of the phonebook (e.g., 10) as follows:
Enter the size of the Phonebook: 10
it then keeps printing the following menu and waiting for the user’s input (as long as the input is not 5):
Menu:
1. Add an entry to the phonebook,
2. Print out all phonebook entries.
3. Search for an entry.
4, Change the phone number of an entry.
S. Quit..
If the user enters 1 (and the phonebook is not full), the following gets executed:
What is the first name of the entry?
Lionel
What is the last name of the entry?
Messi
What is the phone number of the entry?
71922234
The entry has been added.
if the user enters 1 (and the phonebook is full), the following gets executed:
Sorry, the phonebook is full, no entry can be added.
if the user enters 2, the following gets executed (assuming that the phonebook has three entries):
®. Lionel Messi: 71-922234
1. Cristiano Ronaldo: @3-923219
2. Eden Hazard: 7@-111106
Spring 2014-15 2 of 4
Final Exam Introduction to Computer Programming (CMPS 200)
ff the user enters 3, the following gets executed (assuming two entries for Ronaldo exist):
What is the last name of your search?
Ronaldo
Here are the entries that matched your search:
®. Cristiano Ronaldo: @3-923219
1. Luis Ronaldo: @1-125212
Again, if the user enters 3, the following gets executed (assuming Zidane has not been added):
What is the last name of your search?
Zidane
Sorry, no entries matched your search.
if the user enters 4, and no entries for Ronaldo exist, the following gets executed:
What is the last name for the number you want to change?
Ronaldo
Sorry no entries match that last name.
if the user enters 4, and only one entry for Ronaldo exists, the following gets executed:
What is the last name for the number you want to change?
Ronaldo
Exactly one entry matched.
@. Cristiano Ronaldo: @3-923219
What is the new phone number?
93123219
Number changed.
if the user enters 4, and more than one entry for Ronaldo exist, the following gets executed:
What is the last name for the number you want to change?
Ronaldo
Here are the matching entries:
®. Cristiano Ronaldo: @3-923219
1. Luis Ronaldo: @1-125212
Which entry would you like to change?
1
What is the new phone number?
@3123219
Number changed.
if the user enters an invalid menu option (e.g., 10), the following gets executed:
Sorry, invalid menu choice.
Spring 2014-15 3 of 4
Final Exam
Introduction to Computer Programming (CMPS 200)
Problem 2 MatrixMultiplication.java (30%)
Write a program MatrixMultiplication.java that m
es two matrices. Matrices A and B cen be multiplied Write
@ program MatrixMultiplication.java that multiplies two matrices. Matrices A and B can be multiplied together if
the number of columns in A is equal to the number of rows in B. For example, Matrices A and B below can be
multiplied and the result is stored in Matrix C.
y3
The elements of Matrix C are calculated as
Matrix A(2x3) Matrix B (3x1) Matrix c (2x1) follows:
XL | x2 | x3 yi z1 Z1=X"¥1 + X2*V2 + x3*y3
22=Xq*y; +Xs"y2 + Xe*y3
*
x4 | x5 | x6 v2 ze.
As depicted in the Figure below, your program must:
1. Read the number of rows and columns
for Matrix A using System. in;
2. Read the elements of Matrix A using
System.in;
3. Read the number of rows and columns
for Matrix B using System. in;
4. Read the elements of Matrix B using
System. in;
5. Check that A and B can be multiplied. If
Aand B cannot be multiplied, then
return an error and ask the user to
input the data again;
6. Perform the multiplication of A and B
and store the results in Matrix C.
Your multiplication method, which should
be called from main, should take in A and
B as arguments and return C. You are to
print C from main.
EEE CAWindows\system32\cmid.exe
MatrixMultiplication. java
Fenter the elements of second matrix
9 8
of entered matrices
24 15.
63 54
a4 30
Submission Instructions and Guidlines
° Your submission must consist of a single zip file that contains the following .java files only:
PhonebookEntry. java,
should exist in the .zip file/folder.
Phonebook. java,
* Give meaningful names to your methods and variables in your code.
and MatrixMultiplication. java. No additional files
Include a comment at the beginning of your program with basic information about yourself and a description
of the program. Include also a comment at the start of each method.
The name of the zip file must adhere to the following naming convention s#_finalExam_netid, where #
stands for your section number (between 1 and 6) and netid stands for your AUBnet user name. For example,
if your AUBnetid is abc65 and you are in section 4, you should submit the following file:
s4_finalExam_abc65.zip.
Failing to follow these guidelines will result in deducting marks form your grade.
Spring 2014-15 4of4