[Solved] CSCI204 Assignment 2

30 $

File Name: CSCI204_Assignment_2.zip
File Size: 188.4 KB

SKU: [Solved] CSCI204 Assignment 2 Category: Tag:

Or Upload Your Assignment Here:


This assignment aims to establish a basic familiarity with C++ classes. The assignment introduces increasingly object-based, C++ style of solution to a problem.General Requirements• Observe the common principles of OO programming when you design yourclasses.• Put your name, student number at the beginning of each source file.• Make proper documentation and implementation comments in your codes where they are necessary.• Logical structures and statements are properly used for specific purposes.ObjectivesOn completion of these tasks you should be able to:• Code and run C++ programs using the development environment.• Make effective use of the on-line documentation system that supports the development environment.• Code programs using C++ in a hybrid style (procedural code using instances of simple classes) and in a more object-based style.• Manipulate string data.Tasks:Task 1: (6.5 marks)A binary code is a type of code that every entry is either 0 or 1. It is very useful in computer science, cryptography, mathematic and engineering. In the computer system, negative binary code represented by its complement value. For example:A decimal number (9)10 is (01001)2 of binary number. A decimal number (-9)10 is (- 01001)2. In the computer system, the negative value usually represented by its complement value, (10111)2, the most significant (highest) bit is 1, indicates the sign of the value is negative. When the leftmost bit is 0, the sign of the value is positive.To get the complement value of a negative binary number, we can invert every bit, add 1 on the inverted value of the binary code. For example: (-9)10 = (-01001)2, to invert binary code, we have (10110)2, to add with 1, we have10110+ 1________10111The complement code of (-01001)2 is (10111)2.In this task, you will define a class BinaryCode in a file BinaryCode.h and implement the C++ program code in a file BinaryCode.cpp.In the class BinaryCode, declare a dynamic array (such as a char pointer or an short integer pointer) to store a binary code, an integer length to store the length of the binary code (includes the sign bit-the most significant bit). Define the following functions in the class BinaryCode.• In the class BinaryCode, you will define default constructor, copy constructor and other initialization constructors to set initial value for a BinaryCode object;• In the class BinaryCode, define following overloading operators:o Extraction operator (>>) to get input binary code from keyboard and save it in a BinaryCode object;o Insertion operator (<<) to print out the a BinaryCode stream;o Assignment operator(=) to make a deep copy of BinaryCode object.o Invert operator(!) to invert a BinaryCode object.o Complement operator(~) to make a complement BinaryCode object. A complement of a binary code is its invert plus one.o Addition operator (+) to compute two BinaryCode objects’ addition. You should extend two binary streams to the maximum length of both streams plus one so that the significant bits addition won’t be missed. For example:Two binary streams (011011)2 + (01111)2 will be extended (0011011)2 +(0001111)20011011+ 0001111_________0101010o Addition assignment operator (+=) to compute two BinaryCode objects’addition.o Subtraction operation (-) to compute two BinaryCode objects’ subtraction.The subtraction of two BinaryCode objects is defined as:binaryCode1 – binaryCode2 = binaryCode1 + ~(binaryCode2),where ~ is complement operator.o Subtraction assignment operator (-=) to compute two BinaryCode objects’subtraction.o Multiplication operator (*) to compute two BinaryCode objects’multiplication. To compute the multiplication of two BinaryCode objects,each BinaryCode object should be extended as a signed integer. That is toappend number of bits to the left side of a binary stream. Each appendedbit’s value should be the value of the most significant bit’s value of theoriginal binary stream. The new length of binary stream is addition of thetwo binary streams’ length (excludes sign bits).For example, to compute (01001)2 * (10111)2, we will extend two binarystreams to 10 bits (5+5 bits), as (0000001001)2 * (1111110111)2. Thencompute1111110111* 0000001001___________________1111110111+ 1111110111___________________10001110101111.Keep the last (rightmost) 10 bits as the results, which is (1110101111)2,the complement binary code of (-81)10.o Multiplication assignment operator (*=) to compute two BinaryCodes’multiplication.o Shift left operator (<<) to shift a BinaryCodes to left a number of bits. Forexample:(01011)2 << 3 will be (01011000)2. It is like (01011)2 * 23.o Shift right operator (>>) to use arithmetic shift method shift a BinaryCodeto right a number of bits. For example:(01011)2 >> 2 will be (010)2. It is like (01011)2 / 22.(1010)2 >> 2 will be (110)2.o Other necessary member functions.Implement main() in a file task1Main.cpp to test the functions and operators thatdefined above (See the Testing examples of the task for more details).Testing:Use CC to compile the source files on banshee by$ CC –o task1 task1Main.cpp BinaryCode.cppYou can test the task by run the program$ task1and input data that required. Your program will print out results like following (Red datameans input from keyboard):Testing example 1:Input a decimal number for bc1: 1027bc1 = 010000000011!bc1 = 101111111100~bc1 = 101111111101bc1 << 4 = 0100000000110000bc1 >> 2 = 0100000000Input a binary string for bc2: 100110111bc2 = 0100110111!bc2 = 1011001000~bc2 = 1011001001bc2 << 3 = 0100110111000bc2 >> 4 = 010011bc1 + bc2 = 010100111010bc1 – bc2 = 01011001100bc1 * bc2 = 01001101111110100101bc3 = bc1 = 010000000011bc3 += bc2 = 010100111010bc3 = bc1 = 010000000011bc3 -= bc2 = 01011001100bc3 = bc1 = 010000000011bc3 *= bc2 = 01001101111110100101Testing example 2:Input a decimal number for bc1: 852bc1 = 01101010100!bc1 = 10010101011~bc1 = 10010101100bc1 << 4 = 011010101000000bc1 >> 2 = 011010101Input a binary string for bc2: -10100110101001bc2 = 101011001010111!bc2 = 010100110101000~bc2 = 010100110101001bc2 << 3 = 101011001010111000bc2 >> 4 = 10101100101bc1 + bc2 = 101100110101011bc1 – bc2 = 010110011111101bc1 * bc2 = 1011101010101100110001100bc3 = bc1 = 01101010100bc3 += bc2 = 101100110101011bc3 = bc1 = 01101010100bc3 -= bc2 = 010110011111101bc3 = bc1 = 01101010100bc3 *= bc2 = 1011101010101100110001100Testing example 3:Input a decimal number for bc1: -2068bc1 = 1011111101100!bc1 = 0100000010011~bc1 = 0100000010100bc1 << 4 = 10111111011000000bc1 >> 2 = 10111111011Input a binary string for bc2: -1101110010111001bc2 = 10010001101000111!bc2 = 01101110010111000~bc2 = 01101110010111001bc2 << 3 = 10010001101000111000bc2 >> 4 = 1001000110100bc1 + bc2 = 10001101100110011bc1 – bc2 = 01101010010100101bc1 * bc2 = 0110111101110000011001110100bc3 = bc1 = 1011111101100bc3 += bc2 = 10001101100110011bc3 = bc1 = 1011111101100bc3 -= bc2 = 01101010010100101bc3 = bc1 = 1011111101100bc3 *= bc2 = 0110111101110000011001110100Note: Your program should work on different testing data.Task2: (3 marks)In this task, you will define a class Date in a name space MyLib in a file Date.h. Definedata members include day, month and year in the class Date. Define member functions inthe class Date. Implement member functions in a file Date.cpp. The member functionsinclude• A function setDate(int, int, int) takes three parameters of day, month and year andset date for the Date object.• A function setDate(const std::string &) takes a string of date with format“DD/MM/YYYY” and convert the string date to the Date object.“DD/MM/YYYY” represents 2 digits value of day, 2 digits value of month and 4digits value of year with “/” between them.• A function validateDate() validates the date of the Date object. If day is invalid,throw an error message “Invalid day. Day should between 1 and xx.” (where xxis the maximum day for that month). If month is invalid, throw an error message“Invalid month”. If year is invalid, such as less than zero, throw an error message“Invalid year”.• A function toString() converts day, month and year to a string with format“DD/MM/YYYY”.• Other necessary function to return date information.Implement C++ main() function in a file task2Main.cpp that test the member functionsspecified above. It will get date information and set the date to Date objects, validate thedate, catch the exception if the date is invalid. See the Testing of this task for details.Testing:Use CC to compile the source files on banshee by$ CC –o task2 task2Main.cpp Date.cppYou can test the task by run the program$ task2and input data that required. Your program will print out results like following (Red datameans input from keyboard):Input date (DD/MM/YYYY): 29/02/2014Invalid day. Day should be between 1 and 28.Testing example 2:Input date (DD/MM/YYYY): 30/04/200130/04/2001Input day: 32Input month: 5Input year: 2010Invalid day. Day should be between 1 and 31.Testing example 3:Input date (DD/MM/YYYY): 03/11/201103/11/2011Input day:2 9Input month: 2Input year: 200029/02/2000Task3: (5.5 marks)In this task, you will define and implement inheritance classes.Define the diagrams of the classes below.Define a base class Employee in a file Employee.h that described in the diagrams above.Define an extraction operator (>>) to get input values for an Employee from the keyboardor a file; insertion operator (<<) to print out the Employee information. The data memberDOB is the Date type which has been defined in Task 2. Define necessaryconstructor(s) and other member functions for the class. Implement insertion operator,extraction operator, constructor and other member functions in a file Employee.cpp.Define a derived class Administrator in a file Administrator.h that described in thediagrams above. Define an extraction operator (>>) to get input values for anAdministrator from the keyboard or a file; insertion operator (<<) to print out theAdministrator’s information. You should call the extraction operator and insertionoperator defined in the base class for those base class’s data members’ input / output.In the extraction operator, you will catch the exception if the input DOB is invalid. Whenthe exception has been caught, write the administrator information with the error messageinto a log file log.txt.Note: Do not terminate the program.Define necessary constructor(s) and other member functions for the class. Implementinsertion operator, extraction operator, constructor and other member functions in the fileAdministrator.cpp.Define a derived class Technician in a file Technician.h that described in the diagramsabove. Define Extraction operator (>>) to get input values for a Technician from thekeyboard or a file; insertion operator (<<) to print out the Technician’s information. Youshould call the extraction operator and insertion operator defined in the base class forthose data members’ input / output of the base class.In the extraction operator, you will catch the exception if the input DOB is invalid. Whenthe exception has been caught, write the Technician information with the error messageinto a log file log.txt.Note: Do not terminate the program.Define necessary constructor(s) and other member functions for the class. Implementinsertion operator, extraction operator, constructor and other member functions in the fileTechnician.cpp.Define a derived class Driver in a file Driver.h that described in the diagrams above.Define Extraction operator (>>) to get input values for a Driver from the keyboard or afile; insertion operator (<<) to print out the Driver’s information. You should call theextraction operator and insertion operator defined in the base class for those datamembers’ input / output of the base class.In the extraction operator, you will catch the exception if the input DOB is invalid. Whenthe exception has been caught, write the Technician information with the error messageinto a log file log.txt.Note: Do not terminate the program.Define necessary constructor(s) and other member functions for the class. Implementinsertion operator, extraction operator, constructor and other member functions in the fileTechnician.cpp.Define a class EmployeeManagemet in a file EmployeeManagement.h. Define twodata members: a dynamic array of Employee pointers. Each element is a base classEmployee pointer that points to a derived class object. Define an integer length thatstores the total number of Employee objects.Define constructor(s), destructor and following member functions:• loadEmployees(const char *): Load all employee records from a given text fileand store them in the dynamic array by using extraction operators (>>) for thederived classes Administrator, Technician, Driver and base class Employee.Note: The data for employee contain errors, such as wrong date (catch theerror) and wrong type of employee. Add the error messages in the file log.txt.Do not stop until the rest of data have been loaded into the dynamic memory.• manageEmployees():displays a menu and gets input choices, then call thecorresponding member functions to perform the actions.• displayEmployees(): Use insertion operators (defined for the derived classesAdministrator, Technician, Driver and base class Employee) to display requiredEmployees’ information that stored in the array.• updateEmployee(): Update an Employee’s information.• saveEmployees(): Save employee data to a given text file.• Other necessary member functions.Implement all the member functions defined for the class EmployeeManagement in afile EmployeeManagement.cpp.The data format of an employee in a text file are looked likeA,1234567,John Wood,12/02/1965,21 Victoria street Depto NSW 2530,311T,1122334,Taylor Smith,08/04/1978,32 Smith street Wollongong NSW 2500,C++,7D,1234123,Bob Bright,28/09/1983,121 Princess highway Wollongong NSW 2500,1320671Where ‘A’ represents Administrator type, ‘T’ represents Technician type, and ‘D’represents Driver type. You can download a text file employees.txt for your testing.Implement C++ main() functions in a file task3Main.cpp to get text file name from thecommand line, declare an instance of EmployeeManagement; then call the instancefunction loadEmployees() to load employee records, call the instance functionmanageEmployees() to perform the activities. Finally save the data to a text file. See theTesting of this task for more details.Note: Your program should work on different testing data.Testing:Use CC to compile the source files by$ CC –o task3 task3Main.cpp EmployeeManagement.cpp Employee.cppAdministrator.cpp Technician.cpp Driver.cpp Date.cppand run the program by$ ./task3 employees.txtWhen the program starts, it loads data from employees.txt (11 records for this testing file)and displays records (9 correct records) that loaded, then displays menu and get inputdata (red data denote input data from the keyboard. <Enter> means the Enter key).A,1234567,John,12/02/1965,21 Victoria street Depto NSW 2530,311T,1122334,Taylor,08/04/1978,32 Smith street Wollongong NSW 2500,C++,7D,1234123,Bob,28/09/1983,121 Princess highway Wollongong NSW 2500,1320671A,1234568,Marry,20/04/1985,34 Beach Road Fairy Meadow NSW 2500,311D,1122346,Michael,10/10/1970,24 Mall street Wollongong NSW 2500,1230781A,1234590,Rose,20/11/1990,232 Princess highway Unanderra NSW 2526,312D,1234128,Adam,10/12/1987,12 Princess highway Wollongong NSW 2500,1320671T,1122348,Alvin,15/07/1988,48 Spring road Wollongong NSW 2500,Java,8T,1122350,Alice,17/10/1975,33 Autumn street Wollongong NSW 2500,Database,71. Display employees2. Update an employee0. ExitYour choice: 11. Find an employee by a number2. Find all employees by a type0. ExitYour choice: 1Input an employee number: 1234567A,1234567,John,12/02/1965,21 Victoria street Depto NSW 2530,3111. Find an employee by a number2. Find all employees by a type0. ExitYour choice: 1Input an employee number: 1122348T,1122348,Alvin,15/07/1988,48 Spring road Wollongong NSW 2500,Java,81. Find an employee by a number2. Find all employees by a type0. ExitYour choice: 1Input an employee number: 1122350T,1122350,Alice,17/10/1975,33 Autumn street Wollongong NSW 2500,Database,71. Find an employee by a number2. Find all employees by a type0. ExitYour choice: 2Input an employee type: DD,1234123,Bob,28/09/1983,121 Princess highway Wollongong NSW 2500,1320671D,1122346,Michael,10/10/1970,24 Mall street Wollongong NSW 2500,1230781D,1234128,Adam,10/12/1987,12 Princess highway Wollongong NSW 2500,13206711. Find an employee by a number2. Find all employees by a type0. ExitYour choice: 2Input an employee type: TT,1122334,Taylor,08/04/1978,32 Smith street Wollongong NSW 2500,C++,7T,1122348,Alvin,15/07/1988,48 Spring road Wollongong NSW 2500,Java,8T,1122350,Alice,17/10/1975,33 Autumn street Wollongong NSW 2500,Database,71. Find an employee by a number2. Find all employees by a type0. ExitYour choice: 2Input an employee type: AA,1234567,John,12/02/1965,21 Victoria street Depto NSW 2530,311A,1234568,Marry,20/04/1985,34 Beach Road Fairy Meadow NSW 2500,311A,1234590,Rose,20/11/1990,232 Princess highway Unanderra NSW 2526,3121. Find an employee by a number2. Find all employees by a type0. ExitYour choice: 01. Display employees2. Update an employee0. ExitYour choice: 2Employee number: 1234567A,1234567,John,12/02/1965,21 Victoria street Depto NSW 2530,311Input new address (Directly Enter for no change): 107 Gipps road Wollongong NSW2500New office (0 for no change): 3121. Display employees2. Update an employee0. ExitYour choice: 11. Find an employee by a number2. Find all employees by a type0. ExitYour choice: 1Input an employee number: 1234567A,1234567,John,12/02/1965,107 Gipps road Wollongong NSW 2500,3121. Find an employee by a number2. Find all employees by a type0. ExitYour choice: 01. Display employees2. Update an employee0. ExitYour choice: 2Employee number: 1122350T,1122350,Alice,17/10/1975,33 Autumn street Wollongong NSW 2500,Database,7Input new address (Directly Enter for no change): <Enter>New skill level(0 for no change): 91. Display employees2. Update an employee0. ExitYour choice: 2Employee number: 1234128D,1234128,Adam,10/12/1987,12 Princess highway Wollongong NSW 2500,1320671Input new address (Directly Enter for no change): 321 Winter road Sydney NSW 20011. Display employees2. Update an employee0. ExitYour choice: 0Input a file name to save the data: newemployees.txtByeThe log file log.txt contains the following messages:In the text file line 3 has error: Invalid day. Day should between 1 and 28.In the text file line 9 has an error: ‘S’ is a wrong employee typeThe text file newemployees.txt contains following data:A,1234567,John,12/02/1965,107 Gipps road Wollongong NSW 2500,312T,1122334,Taylor,08/04/1978,32 Smith street Wollongong NSW 2500,C++,7D,1234123,Bob,28/09/1983,121 Princess highway Wollongong NSW 2500,1320671A,1234568,Marry,20/04/1985,34 Beach Road Fairy Meadow NSW 2500,311D,1122346,Michael,10/10/1970,24 Mall street Wollongong NSW 2500,1230781A,1234590,Rose,20/11/1990,232 Princess highway Unanderra NSW 2526,312D,1234128,Adam,10/12/1987,321 Winter road Sydney NSW 2001,1320671T,1122348,Alvin,15/07/1988,48 Spring road Wollongong NSW 2500,Java,8T,1122350,Alice,17/10/1975,33 Autumn street Wollongong NSW 2500,Database,9SubmissionThis assignment is due by 11.59 pm (sharp) on Sunday 11 May, 2014.Assignments are submitted electronically via the submit system.For this assignment you must submit the files via the command:$ submit -u your_user_name -c CSCI204 -a 2 task1Main.cpp BinaryCode.hBinaryCode.cpp task2Main.cpp Date.h Date.cpp task3Main.cpp Employee.hEmployee.cpp Administrator.h Administrator.cpp Technician.h Technician.cpp Driver.hDriver.cpp EmployeeManagement.h EmployeeManagement.cppand input your password.Make sure that you use the correct file names. The Unix system is case sensitive. Youmust submit all files in one submit command line.Remember the submit command scripts in the file should be in one line.Your program code must be in a good programming style, such as good names forvariables, methods, classes, and keep indentation.Submission via e-mail is NOT acceptable.After submit your assignment successfully, please check your email of confirmation. Youwould loss 50% of the marks if your program codes could not be compiled correctly.Late submissions do not have to be requested. Late submissions will be allowed for a fewdays after close of scheduled submission (up to 3 days). Late submissions attract a markpenalty (25% off for each day late); this penalty may be waived if an appropriate requestfor special consideration (for medical or similar problem) is made via the universitySOLS system before the close of the late submission time. No work can be submittedafter the late submission time.A policy regarding late submissions is included in the course outline.The assignment is an individual assignment and it is expected that all its tasks will besolved individually without any cooperation with the other students. If you have anydoubts, questions, etc. please consult your lecturer or tutor during tutorial classes oroffice hours. Plagiarism will result in a FAIL grade being recorded for that assessmenttask.End of specification

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSCI204 Assignment 2
30 $