Advertisement
ONGER

CSCI204 Assignment 2 Solved

Apr 25th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.71 KB | None | 0 0
  1. SOLUTION LINK: https://www.ankitcodinghub.com/product/csci204-assignment-2-solved/?v=518f4a738816
  2.  
  3. This assignment aims to establish a basic familiarity with C++ classes. The assignment introduces increasingly object-based, C++ style of solution to a problem.
  4. General Requirements
  5. • Observe the common principles of OO programming when you design your
  6. classes.
  7. • Put your name, student number at the beginning of each source file.
  8. • Make proper documentation and implementation comments in your codes where they are necessary.
  9. • Logical structures and statements are properly used for specific purposes.
  10. Objectives
  11. On completion of these tasks you should be able to:
  12. • Code and run C++ programs using the development environment.
  13. • Make effective use of the on-line documentation system that supports the development environment.
  14. • Code programs using C++ in a hybrid style (procedural code using instances of simple classes) and in a more object-based style.
  15. • Manipulate string data.
  16. Tasks:
  17. Task 1: (6.5 marks)
  18. 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:
  19. 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.
  20. 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 have
  21. 10110
  22. + 1
  23. ________
  24. 10111
  25. The complement code of (-01001)2 is (10111)2.
  26. 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.
  27. 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.
  28. • In the class BinaryCode, you will define default constructor, copy constructor and other initialization constructors to set initial value for a BinaryCode object;
  29. • In the class BinaryCode, define following overloading operators:
  30. o Extraction operator (>>) to get input binary code from keyboard and save it in a BinaryCode object;
  31. o Insertion operator (<<) to print out the a BinaryCode stream;
  32. o Assignment operator(=) to make a deep copy of BinaryCode object.
  33. o Invert operator(!) to invert a BinaryCode object.
  34. o Complement operator(~) to make a complement BinaryCode object. A complement of a binary code is its invert plus one.
  35. 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:
  36. Two binary streams (011011)2 + (01111)2 will be extended (0011011)2 +
  37. (0001111)2
  38. 0011011
  39. + 0001111
  40. _________
  41. 0101010
  42. o Addition assignment operator (+=) to compute two BinaryCode objects’
  43. addition.
  44. o Subtraction operation (-) to compute two BinaryCode objects’ subtraction.
  45. The subtraction of two BinaryCode objects is defined as:
  46. binaryCode1 – binaryCode2 = binaryCode1 + ~(binaryCode2),
  47. where ~ is complement operator.
  48. o Subtraction assignment operator (-=) to compute two BinaryCode objects’
  49. subtraction.
  50. o Multiplication operator (*) to compute two BinaryCode objects’
  51. multiplication. To compute the multiplication of two BinaryCode objects,
  52. each BinaryCode object should be extended as a signed integer. That is to
  53. append number of bits to the left side of a binary stream. Each appended
  54. bit’s value should be the value of the most significant bit’s value of the
  55. original binary stream. The new length of binary stream is addition of the
  56. two binary streams’ length (excludes sign bits).
  57. For example, to compute (01001)2 * (10111)2, we will extend two binary
  58. streams to 10 bits (5+5 bits), as (0000001001)2 * (1111110111)2. Then
  59. compute
  60. 1111110111
  61. * 0000001001
  62. ___________________
  63. 1111110111
  64. + 1111110111
  65. ___________________
  66. 10001110101111.
  67. Keep the last (rightmost) 10 bits as the results, which is (1110101111)2,
  68. the complement binary code of (-81)10.
  69. o Multiplication assignment operator (*=) to compute two BinaryCodes’
  70. multiplication.
  71. o Shift left operator (<<) to shift a BinaryCodes to left a number of bits. For
  72. example:
  73. (01011)2 << 3 will be (01011000)2. It is like (01011)2 * 23.
  74. o Shift right operator (>>) to use arithmetic shift method shift a BinaryCode
  75. to right a number of bits. For example:
  76. (01011)2 >> 2 will be (010)2. It is like (01011)2 / 22.
  77. (1010)2 >> 2 will be (110)2.
  78. o Other necessary member functions.
  79. Implement main() in a file task1Main.cpp to test the functions and operators that
  80. defined above (See the Testing examples of the task for more details).
  81. Testing:
  82. Use CC to compile the source files on banshee by
  83. $ CC –o task1 task1Main.cpp BinaryCode.cpp
  84. You can test the task by run the program
  85. $ task1
  86. and input data that required. Your program will print out results like following (Red data
  87. means input from keyboard):
  88. Testing example 1:
  89. Input a decimal number for bc1: 1027
  90. bc1 = 010000000011
  91. !bc1 = 101111111100
  92. ~bc1 = 101111111101
  93. bc1 << 4 = 0100000000110000
  94. bc1 >> 2 = 0100000000
  95. Input a binary string for bc2: 100110111
  96. bc2 = 0100110111
  97. !bc2 = 1011001000
  98. ~bc2 = 1011001001
  99. bc2 << 3 = 0100110111000
  100. bc2 >> 4 = 010011
  101. bc1 + bc2 = 010100111010
  102. bc1 – bc2 = 01011001100
  103. bc1 * bc2 = 01001101111110100101
  104. bc3 = bc1 = 010000000011
  105. bc3 += bc2 = 010100111010
  106. bc3 = bc1 = 010000000011
  107. bc3 -= bc2 = 01011001100
  108. bc3 = bc1 = 010000000011
  109. bc3 *= bc2 = 01001101111110100101
  110. Testing example 2:
  111. Input a decimal number for bc1: 852
  112. bc1 = 01101010100
  113. !bc1 = 10010101011
  114. ~bc1 = 10010101100
  115. bc1 << 4 = 011010101000000
  116. bc1 >> 2 = 011010101
  117. Input a binary string for bc2: -10100110101001
  118. bc2 = 101011001010111
  119. !bc2 = 010100110101000
  120. ~bc2 = 010100110101001
  121. bc2 << 3 = 101011001010111000
  122. bc2 >> 4 = 10101100101
  123. bc1 + bc2 = 101100110101011
  124. bc1 – bc2 = 010110011111101
  125. bc1 * bc2 = 1011101010101100110001100
  126. bc3 = bc1 = 01101010100
  127. bc3 += bc2 = 101100110101011
  128. bc3 = bc1 = 01101010100
  129. bc3 -= bc2 = 010110011111101
  130. bc3 = bc1 = 01101010100
  131. bc3 *= bc2 = 1011101010101100110001100
  132. Testing example 3:
  133. Input a decimal number for bc1: -2068
  134. bc1 = 1011111101100
  135. !bc1 = 0100000010011
  136. ~bc1 = 0100000010100
  137. bc1 << 4 = 10111111011000000
  138. bc1 >> 2 = 10111111011
  139. Input a binary string for bc2: -1101110010111001
  140. bc2 = 10010001101000111
  141. !bc2 = 01101110010111000
  142. ~bc2 = 01101110010111001
  143. bc2 << 3 = 10010001101000111000
  144. bc2 >> 4 = 1001000110100
  145. bc1 + bc2 = 10001101100110011
  146. bc1 – bc2 = 01101010010100101
  147. bc1 * bc2 = 0110111101110000011001110100
  148. bc3 = bc1 = 1011111101100
  149. bc3 += bc2 = 10001101100110011
  150. bc3 = bc1 = 1011111101100
  151. bc3 -= bc2 = 01101010010100101
  152. bc3 = bc1 = 1011111101100
  153. bc3 *= bc2 = 0110111101110000011001110100
  154. Note: Your program should work on different testing data.
  155. Task2: (3 marks)
  156. In this task, you will define a class Date in a name space MyLib in a file Date.h. Define
  157. data members include day, month and year in the class Date. Define member functions in
  158. the class Date. Implement member functions in a file Date.cpp. The member functions
  159. include
  160. • A function setDate(int, int, int) takes three parameters of day, month and year and
  161. set date for the Date object.
  162. • A function setDate(const std::string &) takes a string of date with format
  163. “DD/MM/YYYY” and convert the string date to the Date object.
  164. “DD/MM/YYYY” represents 2 digits value of day, 2 digits value of month and 4
  165. digits value of year with “/” between them.
  166. • A function validateDate() validates the date of the Date object. If day is invalid,
  167. throw an error message “Invalid day. Day should between 1 and xx.” (where xx
  168. is the maximum day for that month). If month is invalid, throw an error message
  169. “Invalid month”. If year is invalid, such as less than zero, throw an error message
  170. “Invalid year”.
  171. • A function toString() converts day, month and year to a string with format
  172. “DD/MM/YYYY”.
  173. • Other necessary function to return date information.
  174. Implement C++ main() function in a file task2Main.cpp that test the member functions
  175. specified above. It will get date information and set the date to Date objects, validate the
  176. date, catch the exception if the date is invalid. See the Testing of this task for details.
  177. Testing:
  178. Use CC to compile the source files on banshee by
  179. $ CC –o task2 task2Main.cpp Date.cpp
  180. You can test the task by run the program
  181. $ task2
  182. and input data that required. Your program will print out results like following (Red data
  183. means input from keyboard):
  184. Input date (DD/MM/YYYY): 29/02/2014
  185. Invalid day. Day should be between 1 and 28.
  186. Testing example 2:
  187. Input date (DD/MM/YYYY): 30/04/2001
  188. 30/04/2001
  189. Input day: 32
  190. Input month: 5
  191. Input year: 2010
  192. Invalid day. Day should be between 1 and 31.
  193. Testing example 3:
  194. Input date (DD/MM/YYYY): 03/11/2011
  195. 03/11/2011
  196. Input day:2 9
  197. Input month: 2
  198. Input year: 2000
  199. 29/02/2000
  200. Task3: (5.5 marks)
  201. In this task, you will define and implement inheritance classes.
  202. Define the diagrams of the classes below.
  203. Define a base class Employee in a file Employee.h that described in the diagrams above.
  204. Define an extraction operator (>>) to get input values for an Employee from the keyboard
  205. or a file; insertion operator (<<) to print out the Employee information. The data member
  206. DOB is the Date type which has been defined in Task 2. Define necessary
  207. constructor(s) and other member functions for the class. Implement insertion operator,
  208. extraction operator, constructor and other member functions in a file Employee.cpp.
  209. Define a derived class Administrator in a file Administrator.h that described in the
  210. diagrams above. Define an extraction operator (>>) to get input values for an
  211. Administrator from the keyboard or a file; insertion operator (<<) to print out the
  212. Administrator’s information. You should call the extraction operator and insertion
  213. operator defined in the base class for those base class’s data members’ input / output.
  214. In the extraction operator, you will catch the exception if the input DOB is invalid. When
  215. the exception has been caught, write the administrator information with the error message
  216. into a log file log.txt.
  217. Note: Do not terminate the program.
  218. Define necessary constructor(s) and other member functions for the class. Implement
  219. insertion operator, extraction operator, constructor and other member functions in the file
  220. Administrator.cpp.
  221. Define a derived class Technician in a file Technician.h that described in the diagrams
  222. above. Define Extraction operator (>>) to get input values for a Technician from the
  223. keyboard or a file; insertion operator (<<) to print out the Technician’s information. You
  224. should call the extraction operator and insertion operator defined in the base class for
  225. those data members’ input / output of the base class.
  226. In the extraction operator, you will catch the exception if the input DOB is invalid. When
  227. the exception has been caught, write the Technician information with the error message
  228. into a log file log.txt.
  229. Note: Do not terminate the program.
  230. Define necessary constructor(s) and other member functions for the class. Implement
  231. insertion operator, extraction operator, constructor and other member functions in the file
  232. Technician.cpp.
  233. Define a derived class Driver in a file Driver.h that described in the diagrams above.
  234. Define Extraction operator (>>) to get input values for a Driver from the keyboard or a
  235. file; insertion operator (<<) to print out the Driver’s information. You should call the
  236. extraction operator and insertion operator defined in the base class for those data
  237. members’ input / output of the base class.
  238. In the extraction operator, you will catch the exception if the input DOB is invalid. When
  239. the exception has been caught, write the Technician information with the error message
  240. into a log file log.txt.
  241. Note: Do not terminate the program.
  242. Define necessary constructor(s) and other member functions for the class. Implement
  243. insertion operator, extraction operator, constructor and other member functions in the file
  244. Technician.cpp.
  245. Define a class EmployeeManagemet in a file EmployeeManagement.h. Define two
  246. data members: a dynamic array of Employee pointers. Each element is a base class
  247. Employee pointer that points to a derived class object. Define an integer length that
  248. stores the total number of Employee objects.
  249. Define constructor(s), destructor and following member functions:
  250. • loadEmployees(const char *): Load all employee records from a given text file
  251. and store them in the dynamic array by using extraction operators (>>) for the
  252. derived classes Administrator, Technician, Driver and base class Employee.
  253. Note: The data for employee contain errors, such as wrong date (catch the
  254. error) and wrong type of employee. Add the error messages in the file log.txt.
  255. Do not stop until the rest of data have been loaded into the dynamic memory.
  256. • manageEmployees():displays a menu and gets input choices, then call the
  257. corresponding member functions to perform the actions.
  258. • displayEmployees(): Use insertion operators (defined for the derived classes
  259. Administrator, Technician, Driver and base class Employee) to display required
  260. Employees’ information that stored in the array.
  261. • updateEmployee(): Update an Employee’s information.
  262. • saveEmployees(): Save employee data to a given text file.
  263. • Other necessary member functions.
  264. Implement all the member functions defined for the class EmployeeManagement in a
  265. file EmployeeManagement.cpp.
  266. The data format of an employee in a text file are looked like
  267. A,1234567,John Wood,12/02/1965,21 Victoria street Depto NSW 2530,311
  268. T,1122334,Taylor Smith,08/04/1978,32 Smith street Wollongong NSW 2500,C++,7
  269. D,1234123,Bob Bright,28/09/1983,121 Princess highway Wollongong NSW 2500,1320671
  270. Where ‘A’ represents Administrator type, ‘T’ represents Technician type, and ‘D’
  271. represents Driver type. You can download a text file employees.txt for your testing.
  272. Implement C++ main() functions in a file task3Main.cpp to get text file name from the
  273. command line, declare an instance of EmployeeManagement; then call the instance
  274. function loadEmployees() to load employee records, call the instance function
  275. manageEmployees() to perform the activities. Finally save the data to a text file. See the
  276. Testing of this task for more details.
  277. Note: Your program should work on different testing data.
  278. Testing:
  279. Use CC to compile the source files by
  280. $ CC –o task3 task3Main.cpp EmployeeManagement.cpp Employee.cpp
  281. Administrator.cpp Technician.cpp Driver.cpp Date.cpp
  282. and run the program by
  283. $ ./task3 employees.txt
  284. When the program starts, it loads data from employees.txt (11 records for this testing file)
  285. and displays records (9 correct records) that loaded, then displays menu and get input
  286. data (red data denote input data from the keyboard. <Enter> means the Enter key).
  287. A,1234567,John,12/02/1965,21 Victoria street Depto NSW 2530,311
  288. T,1122334,Taylor,08/04/1978,32 Smith street Wollongong NSW 2500,C++,7
  289. D,1234123,Bob,28/09/1983,121 Princess highway Wollongong NSW 2500,1320671
  290. A,1234568,Marry,20/04/1985,34 Beach Road Fairy Meadow NSW 2500,311
  291. D,1122346,Michael,10/10/1970,24 Mall street Wollongong NSW 2500,1230781
  292. A,1234590,Rose,20/11/1990,232 Princess highway Unanderra NSW 2526,312
  293. D,1234128,Adam,10/12/1987,12 Princess highway Wollongong NSW 2500,1320671
  294. T,1122348,Alvin,15/07/1988,48 Spring road Wollongong NSW 2500,Java,8
  295. T,1122350,Alice,17/10/1975,33 Autumn street Wollongong NSW 2500,Database,7
  296. 1. Display employees
  297. 2. Update an employee
  298. 0. Exit
  299. Your choice: 1
  300. 1. Find an employee by a number
  301. 2. Find all employees by a type
  302. 0. Exit
  303. Your choice: 1
  304. Input an employee number: 1234567
  305. A,1234567,John,12/02/1965,21 Victoria street Depto NSW 2530,311
  306. 1. Find an employee by a number
  307. 2. Find all employees by a type
  308. 0. Exit
  309. Your choice: 1
  310. Input an employee number: 1122348
  311. T,1122348,Alvin,15/07/1988,48 Spring road Wollongong NSW 2500,Java,8
  312. 1. Find an employee by a number
  313. 2. Find all employees by a type
  314. 0. Exit
  315. Your choice: 1
  316. Input an employee number: 1122350
  317. T,1122350,Alice,17/10/1975,33 Autumn street Wollongong NSW 2500,Database,7
  318. 1. Find an employee by a number
  319. 2. Find all employees by a type
  320. 0. Exit
  321. Your choice: 2
  322. Input an employee type: D
  323. D,1234123,Bob,28/09/1983,121 Princess highway Wollongong NSW 2500,1320671
  324. D,1122346,Michael,10/10/1970,24 Mall street Wollongong NSW 2500,1230781
  325. D,1234128,Adam,10/12/1987,12 Princess highway Wollongong NSW 2500,1320671
  326. 1. Find an employee by a number
  327. 2. Find all employees by a type
  328. 0. Exit
  329. Your choice: 2
  330. Input an employee type: T
  331. T,1122334,Taylor,08/04/1978,32 Smith street Wollongong NSW 2500,C++,7
  332. T,1122348,Alvin,15/07/1988,48 Spring road Wollongong NSW 2500,Java,8
  333. T,1122350,Alice,17/10/1975,33 Autumn street Wollongong NSW 2500,Database,7
  334. 1. Find an employee by a number
  335. 2. Find all employees by a type
  336. 0. Exit
  337. Your choice: 2
  338. Input an employee type: A
  339. A,1234567,John,12/02/1965,21 Victoria street Depto NSW 2530,311
  340. A,1234568,Marry,20/04/1985,34 Beach Road Fairy Meadow NSW 2500,311
  341. A,1234590,Rose,20/11/1990,232 Princess highway Unanderra NSW 2526,312
  342. 1. Find an employee by a number
  343. 2. Find all employees by a type
  344. 0. Exit
  345. Your choice: 0
  346. 1. Display employees
  347. 2. Update an employee
  348. 0. Exit
  349. Your choice: 2
  350. Employee number: 1234567
  351. A,1234567,John,12/02/1965,21 Victoria street Depto NSW 2530,311
  352. Input new address (Directly Enter for no change): 107 Gipps road Wollongong NSW
  353. 2500
  354. New office (0 for no change): 312
  355. 1. Display employees
  356. 2. Update an employee
  357. 0. Exit
  358. Your choice: 1
  359. 1. Find an employee by a number
  360. 2. Find all employees by a type
  361. 0. Exit
  362. Your choice: 1
  363. Input an employee number: 1234567
  364. A,1234567,John,12/02/1965,107 Gipps road Wollongong NSW 2500,312
  365. 1. Find an employee by a number
  366. 2. Find all employees by a type
  367. 0. Exit
  368. Your choice: 0
  369. 1. Display employees
  370. 2. Update an employee
  371. 0. Exit
  372. Your choice: 2
  373. Employee number: 1122350
  374. T,1122350,Alice,17/10/1975,33 Autumn street Wollongong NSW 2500,Database,7
  375. Input new address (Directly Enter for no change): <Enter>
  376. New skill level(0 for no change): 9
  377. 1. Display employees
  378. 2. Update an employee
  379. 0. Exit
  380. Your choice: 2
  381. Employee number: 1234128
  382. D,1234128,Adam,10/12/1987,12 Princess highway Wollongong NSW 2500,1320671
  383. Input new address (Directly Enter for no change): 321 Winter road Sydney NSW 2001
  384. 1. Display employees
  385. 2. Update an employee
  386. 0. Exit
  387. Your choice: 0
  388. Input a file name to save the data: newemployees.txt
  389. Bye
  390. The log file log.txt contains the following messages:
  391. In the text file line 3 has error: Invalid day. Day should between 1 and 28.
  392. In the text file line 9 has an error: ‘S’ is a wrong employee type
  393. The text file newemployees.txt contains following data:
  394. A,1234567,John,12/02/1965,107 Gipps road Wollongong NSW 2500,312
  395. T,1122334,Taylor,08/04/1978,32 Smith street Wollongong NSW 2500,C++,7
  396. D,1234123,Bob,28/09/1983,121 Princess highway Wollongong NSW 2500,1320671
  397. A,1234568,Marry,20/04/1985,34 Beach Road Fairy Meadow NSW 2500,311
  398. D,1122346,Michael,10/10/1970,24 Mall street Wollongong NSW 2500,1230781
  399. A,1234590,Rose,20/11/1990,232 Princess highway Unanderra NSW 2526,312
  400. D,1234128,Adam,10/12/1987,321 Winter road Sydney NSW 2001,1320671
  401. T,1122348,Alvin,15/07/1988,48 Spring road Wollongong NSW 2500,Java,8
  402. T,1122350,Alice,17/10/1975,33 Autumn street Wollongong NSW 2500,Database,9
  403. Submission
  404. This assignment is due by 11.59 pm (sharp) on Sunday 11 May, 2014.
  405. Assignments are submitted electronically via the submit system.
  406. For this assignment you must submit the files via the command:
  407. $ submit -u your_user_name -c CSCI204 -a 2 task1Main.cpp BinaryCode.h
  408. BinaryCode.cpp task2Main.cpp Date.h Date.cpp task3Main.cpp Employee.h
  409. Employee.cpp Administrator.h Administrator.cpp Technician.h Technician.cpp Driver.h
  410. Driver.cpp EmployeeManagement.h EmployeeManagement.cpp
  411. and input your password.
  412. Make sure that you use the correct file names. The Unix system is case sensitive. You
  413. must submit all files in one submit command line.
  414. Remember the submit command scripts in the file should be in one line.
  415. Your program code must be in a good programming style, such as good names for
  416. variables, methods, classes, and keep indentation.
  417. Submission via e-mail is NOT acceptable.
  418. After submit your assignment successfully, please check your email of confirmation. You
  419. would loss 50% of the marks if your program codes could not be compiled correctly.
  420. Late submissions do not have to be requested. Late submissions will be allowed for a few
  421. days after close of scheduled submission (up to 3 days). Late submissions attract a mark
  422. penalty (25% off for each day late); this penalty may be waived if an appropriate request
  423. for special consideration (for medical or similar problem) is made via the university
  424. SOLS system before the close of the late submission time. No work can be submitted
  425. after the late submission time.
  426. A policy regarding late submissions is included in the course outline.
  427. The assignment is an individual assignment and it is expected that all its tasks will be
  428. solved individually without any cooperation with the other students. If you have any
  429. doubts, questions, etc. please consult your lecturer or tutor during tutorial classes or
  430. office hours. Plagiarism will result in a FAIL grade being recorded for that assessment
  431. task.
  432. End of specification
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement