You are on page 1of 10

NATIONAL UNIVERSITY OF COMPUTER & EMERGING

SCIENCES ISLAMABAD
Object Oriented Programming Fall 2023
ASSIGNMENT # 02

Due Date: 22-oct-2023


Total Marks: 100
Mark your calendar as this is a hard deadline
Instructions:
• Make sure that you read and understand each and every instruction
• Create each problem solution in a separate header file and functions in separate cpp file
With your roll no. For example: ’ 22i-0001_Q1.h’ and ’ 22i-0001_Q1.cpp’ for problem 1
• Keep a backup of your work always that will be helpful in preventing any mishap.
• Combine all your work in one .zip file.
• Declare functions outline.
• Name the .zip file as ROLL-NUM SECTION.zip (e.g. 22i-0001 B.zip).
• Submit the .zip file on Google Classroom within the deadline.
• Avoid last hour submissions
• Start early otherwise you will struggle with the assignment.
• You are not allowed to use any built-in functions.
• You must follow the submission instructions to the letter, as failing to do so will get you a zero in
the assignment.
• All the submitted evaluation instruments will be checked for plagiarism. If found plagiarized, both
the involved parties will be marked 0
[20 Marks]

Q1. A program calculates performance-based salary of customer support representatives (CSRs)in an organization.
Implement a class CSR (Customer support representative) with the following private datamembers:
1. int csrID – a integer to hold employee identification numbers between 1 and 7.

2. char *csrName – a string to hold the name of each CSR

3. int hours – an integer to hold the number of hours worked by each CSR
4. int complaintsResolved – an integer to hold the number of complaints successfullyresolved by each CSR
5. float payrate – a float to hold the employee’s hourly pay rate, where payRate is calculatedas following:
payRate = $25 + 25*(complaintsResolved by each CSR/total complaints resolved)

6. float wage – a float to hold the employee’s wage, calculated aswages = hours * payrate
7. static int totalComplaintsResolved - a static integer to hold the total number ofcomplaints resolved by all CSRs

The class should implement the following public methods:

1. int getCSRID() – a getter for CSRID

2. char* getName() – a getter for employee’s name

3. int getHours() – a getter for hours worked

4. int getComplaintsResolved() – a getter for complaints resolved by the employee

5. float getPayrate() – a getter for payrate of the employee

6. float getWage() – a getter for the employee’s wage

7. void setCSRID(int ID) – a setter for CSRID

8. void setName(char* n) – a setter for name

9. void setHours(int hours) – a setter for hours

10. void setComplaintsResolved(int cpsResolved) – a setter for complaintsResolved

11. static void setTotalCpsResolved(int totalCpsResolved) – a setter fortotalComplaintsResolved


12. void calcPayrate() – a function to calculate the employees payrate
13. void calcWage() – a function to calculate the employee’s wage

14. static int getTotalCpsResolved() – a static function to get total complaints resolved
Implement the following functions in global scope. All functions will take an array of CSR objectsas an argument.

e.g. CSR employees[7] – an array of 7 CSR objectsFunctions:


1. CSR getCSR_at(CSR employees[7], int index) – returns the CSR object at the given arrayindex
2. void calcTotalComplaints (CSR employees[7] ) – a function that sums the complaints ofresolved by all
employees and assigns to the totalComplaintsResolved variable.

3. void calcAllEmployeeWages(CSR employees[7]) – a function that calculates the wages ofall employees

4. void SortByHours(CSR employees[7]) – sorts employees in descending order based onhours worked.

5. void SortByComplaintsRes(CSR employees[7]) – sorts employees in descending orderbased on complaints


Resolved

6. void SortByWages(CSR employees[7]) – sorts employees in descending order based onwages.

Note: Ensure all necessary input validation, for example, hours, complaintsResolved cannot be negative numbers. Also assume, no
two CSRs will have the same number of hours, complaintsResolved and wages.
Total Marks: 60

Q2: Implementation of Matrix Class – Your goal is to implement a generic “Matrix" class. You will need
to write two files (matrix.h, matrix.cpp). Your implemented class must fully provide the definitions of
following class (interface) functions.

class Matrix{
1. int **matrix – a double pointer of type integer to create a 2D array which is your matrix
2. int row – an integer to store the rows in the matrix
3. int col – an integer to store the columns in the matrix
public:
//include all the necessary checks before performing the operations in the functions
Matrix();// a default constructor
Matrix(int, int);// a parametrized constructor
Matrix(const Matrix &);// copy constructor
void set(int i, int j, float val);//set value at (i,j)
float get(int i, int j)const; //get value at (i,j)
Matrix multiplyElement(const Matrix);//Elementwise multiplies two Matrices and returns the result
void makeZero(); //if the any element in the matrix is zero than make the entire row and column
zero.
Example:

5 8 8 9 5 5 8 0 9 5

1 2 0 6 3 0 0 0 0 0

8 4 3 9 4 8 4 0 9 4

bool searchNumber(int number); // if the number exist in the matrix return true. Else return false.
You can make the number by using digits from cells that are next to each other, either up and
down or side to side. But you can't use the same cell with the same letter more than once.
Each cell contains single digit at max.

Example: In this case your function should return true. Number = 2581349

9 6 4 4 2
9 6 4 4 2
3 9 7 8 5
3 9 7 8 5
6 4 3 1 4
6 4 3 1 4
void rotateBy90(); // You have to implement the logic to rotate your matrix by 90 degrees anti-
clock wise.

Example:

9 6 2 2 5 4

3 9 5 6 9 4

6 4 4 9 3 6

int LeftDiagonalTotal() - Calculates and returns the total sum of the values in the left Diagonal ofarray.
int RightDiagonalTotal() - Calculates and returns the total sum of the values in the right Diagonal ofarray.

Matrix Merge(Matrix m); // a function to merge two matrices. E.g.

2,3,5,8 8,9,3 2,3,5,8,8,9,3


m= 5,9,8,7 n = 7,0,9 5,9,8,7,7,0,9

void input(); // takes input in every element of matrix


void display(); // prints every element
˜Matrix(); // destructor to de-allocate memory.
};

Total Marks = 50

Q3: Implement your own string class using only primitive data types. Your string class should containsome of the
same functionality as the string class in C++.

Your class should have the following member variables:


1. char *data: holds the character array that constitutes the string
2. int length: the current length of the string that the object contains (number of characters in data)

Your object should have the following constructors and destructor:


1. String(): default constructor
2. String(int size): alternate constructor that initializes length to size and initializes data to a new char array of
size.
3. String(char* str): alternate constructor that initializes length to size of str, initializesdata to a new char array of
size length and fills the contents of data with the contents of str.
Note: You can assume that any character array that is sent as str will be terminated
by a null character (‘\0’)
4. String(const String &str): copy constructor
5. ~String(): destructor in which you should delete your data pointer.

Your class should have the following functions:


1. int strLength():returns the length of the string in the object
2. void clear(): should clear the data in your string class (data and length)
3. bool empty(): this method should check to see if data within the String class isempty or not.
4. int charAt(char c): this method returns the first index at which this character occursin the string
5. void CompressString(): a function to find if string has more than one consecutive
occurrences of a character in the string, it removes the extra occurrences.
Example:
String: “a”
String after compression: “a”
String: “aaaaaaa”
String after compression: “a”
String: “bbabbbbbcccddddddddddeffffg”
String after Compression: “babcdefg”.

6. void findMinStr(char* secretStr); // decalare variable char* magicStr = data in this function only;
There was a programmer named Alex in the kingdom of Codeville who held a magical string called "magicStr."
Lily, a fellow developer, asked Alex for assistance in locating the smallest substring within "magicStr" that
included all of the characters from her string "secretStr." To accomplish this goal, Alex wrote a powerful
function called findMinStr, which returned the "Not Found" if it did not exist. Because of its exceptional skills,
its magical function became famous among coders, facilitating the search for hidden gems inside strings.

Example:

magicStr = “iamastudentoffastnuces.”
secretStr = “fasces”

returned value = “fastnuces”.

7. bool equalsIgnoreCase(char* str): this method compares the calling string objectdata with the data in str
without considering the case.
8. char* substring(char* substr, int startIndex) : this method should search for substrin data within the calling
String object starting from the startIndex. The methodshould search for the substring from the startIndex and
return the substring from the position found till the end. For example, if you had the string “awesome” andyou
wanted to find the substring ‘es’ starting from the beginning of the string(startIndex = 0). Your function
should return “esome”. Returns NULL if substring isnot found.

9. char* substring(char* substr, int startIndex, int endIndex) : this method should search for substr in data
within the calling String object between the startIndex and endIndex. For example, if you had the string
“awesome” and you wanted to find the substring ‘es’ starting from startIndex=2 and endIndex=5. Your function
shouldreturn “esom”. Returns NULL if substring is not found.

10. Once upon a time, in a magical land called Chromaland, there was a special version of a game called "Color
Challenge." In this exciting game, there was a line of colorful balls on a board. Each ball had its own color: bright
red ('R'), sunny yellow ('Y'), deep blue ('B'), fresh green ('G'), and pure white ('W'). The goal was simple: you
needed to get rid of all the balls from the board.

You had some colorful balls in your hand, just like the ones on the board. These balls were your tools to win the
game. Here were the rules:

1. On your turn, you could pick a ball from your hand and place it between two balls on the board or at either
end of the line.

2. If you put a ball in a way that made three or more balls of the same color next to each other, those balls
vanished from the board.

3. If this made more groups of three or more balls of the same color, you had to keep removing them until there
were none left.

4. If you managed to remove all the balls from the board, you won the game and became a hero!

Your job was to figure out how to use the balls in your hand wisely to clear the board with as few moves as
possible. In this scenario your function should return the number of balls used to win the game by your hand
string. But there was a challenge: if you couldn't clear the board with the balls in your hand, you'd lose, and your
function should return -1.

int ColorChallange(char* hand); // declare variable char* board = data; for this function

TestCase 1:

Input: board = "WRRBBW", hand = "RB"


Output: -1
Explanation: It is impossible to clear all the balls. The best you can do is:
- Insert 'R' so the board becomes WRRRBBW. WRRRBBW -> WBBW.
- Insert 'B' so the board becomes WBBBW. WBBBW -> WW.
There are still balls remaining on the board, and you are out of balls to insert so return -1.
TestCase 2:

Input: board = "WWRRBBWW", hand = "WRBRW"


Output: 2
Explanation: To make the board empty:
- Insert 'R' so the board becomes WWRRRBBWW. WWRRRBBWW -> WWBBWW.
- Insert 'B' so the board becomes WWBBBWW. WWBBBWW -> WWWW -> empty.
2 balls from your hand were needed to clear the board.

TestCase 3:

Input: board = "G", hand = "GGGGG"


Output: 2
Explanation: To make the board empty:
- Insert 'G' so the board becomes GG.
- Insert 'G' so the board becomes GGG. GGG -> empty.
2 balls from your hand were needed to clear the board.

Question 4 30 marks
Implement a structure Address with the following data members:

1. char* address – a sting to store the address


2. char* city – a sting to store the city name

3. char* state – a sting to store the state name


4. int zip_code – an integer to store the ZIP code

Implement a structure CustomerAccount with the following data members:

1. char* name – a string to store the name of the customer


2. Address address – a nested structure variable to hold the address
3. long long phoneNum – a long long to store the phone number of the customer

4. float balance – a float to store the customer’s balance


5. char* accountNum – a string to store the customer’s account number, account numbers are in the for “PK001”
to “PK100”.

The following two will be passed as arguments to all the global functions mentioned below.

1. CustomerAccount *customers[100] – an array of 100 CustomerAccount pointers.


2. int accountsOpen – an integer to store the current accounts open and can also be used
as an index for the customer’s array.

Implement the following functions in CustomerAccount:

1. void OpenCustomerAccount (CustomerAccount* customers[], int& accountsOpen, char* NameVal,


char*addVal, char*cityVal, char*stateVal, int zipcodeVal, long long phoneVal, float balanceVal) – a function to
create a new customer account and assign it an account number that has not already been taken between PK001
and PK100. To dynamically create a new customer account, use an element in the customers array (a pointer) with the
new keyword.

2. int SearchCustomer (CustomerAccount* customers[], int accountsOpen, char* accountNum) – a function


that searches for a customer using an account number. If the customer is found it returns the array index
otherwise return -1

3. bool UpdateCustomerAccount (CustomerAccount* customers[], int accountsOpen, char


*accountNumVal, Address addressVal) – an overloaded function that updates a
customer’s address

4. bool UpdateCustomerAccount (CustomerAccount* customers[], int accountsOpen, char *accountNumVal,


long long phoneVal) – a function that updates a customer’s phonenumber

5. bool UpdateCustomerAccount (CustomerAccount* customers[], int accountsOpen, char


*accountNumVal, float balanceVal) – a function that updates a customer’s balance

6. void DisplayAllCustomers(CustomerAccount* customers[], int accountsOpen) – a function that displays all


customer accounts in the bank

15 Marks

Q5. A restaurant serves five different kinds of sandwiches.

Implement a class Sandwich that includes the following private data members:

1. char *name – a sting to store the sandwich name


2. char *filling – a string to store the sandwich filling
3. char *size – a string for the size of the sandwich (can only be small, medium or large)
4. bool is_ready – a boolean to store the status of the sandwich, is it ready or not
5. double price - a double to store the price of the sandwich.
The class shall provide the following public methods:

1. Sandwich() – a default constructor


2. Sandwich(char *fillingVal, double priceVal) – a parametrized constructor
3. Sandwich(char *fillingVal, double priceVal, char* nameVal, char* sizeVal, boolready_status) – a
parametrized constructor
4. Sandwich(const Sandwich &sandwich) – a copy constructor
5. void setFilling(char *fillingVal) – setter for filling
6. void setPrice(double priceVal) – setter for price
7. void setName(char *nameVal) – setter for name
8. void setSize(char *sizeVal) – setter for size
9. char* getFilling() – getter for filling
10. double getPrice() - getter for price
11. char* getName() – getter for name
12. char* getSize() – getter for size
13. void makeSandwich() – function to make sandwich (check if filling is not NULL then setvalue of
is_ready to true)
14. bool check_status() - function to check if sandwich is ready or not

Happy Coding 🙂

You might also like