【Comp 218/EC基础讲解】Concordia University
Comp 218/EC – Winter 2021
Fundamentals of Object Oriented Programming
Assignment 4 - Due by 11:59pm Friday April 16th, 2021
__
PURPOSE: This assignment will give you the opportunity to write the declaration for a structure
and use it, to define and use a class, and manipulate a one-dimensional array.
Specifications for programming:
- Refer to handout for Assignment #1
Question #1: Writing a ‘structure’ declaration (4 pts)
a) Declare a struct type named Pet which will describe a pet. The following information
should be included:
Name (string of characters)
Age (double number)
Breed (string of characters)
Gender (string of characters)
isAdopted (boolean)
b) Declare pet1 to be a variable of type Pet.
c) Write a function to read values into the member variables of type Pet. (The struct
variable should be passed as an argument). The order in which the data is read is the
same as that in the struct.
d) Display all the values in your program.
Following is a sample output screens to illustrate the expected behavior of your program.
User’s input is highlighted with a red circle.
Comp 218/Winter 2021 Page 2 of 7 Assignment #4
Figure 1: Output Example of Question 1
Question #2: Implementing a class (11 pts)
a) (2 pts) A Pet object represent the name, age, breed and gender for a pet. Write a
declaration for the class Pet including its data members and function members based on
the following UML and save it in a file called Pet.h.
Comp 218/Winter 2021 Page 3 of 7 Assignment #4
b) (5 pts) Implement the member functions based on the following descriptions, and save them
in a file called Pet.cpp.
? Pet(): default constructor, which will set all the attributes to “kitty”, 2.5,
“BritishShorthair”, “female”, true.
? Pet (string name): a name constructor which sets the attribute name to the passed
value and the other three attributes age, breed , gender, isAdopted will be the
default one, which are 2.5, “BritishShorthair”, “female”, true.
? Pet (string name, double date, string breed, string gender, bool
choice): a third constructor which sets all the attributes to the passed values.
? getName(): returns the name of the item.
? getAge(): returns the age of the pet.
? getBreed(): returns the breed of the pet.
? getGender(): returns the gender of the pet.
? getAdoption(): return the adoption status of the pet.
? setName(string n): sets the name attribute to n.
? setAge(double a): sets the age attribute to a.
? setBreed(string b): sets the breed attribute to b.
? setGender(string g): sets the gender attribute to g.
? setAdoption (boolean choice): sets the adoption status of the pet.
? inHumanYears(): which will return the age of a pet cat in human years. Here is how to
calculate it:
a. 18 human years equals the first year of a medium-sized cat’s life.
b. Year two for a cat equals about nine years for a human.
c. And after that, each human year would be approximately five years for a cat.
Here a few examples:
Pet - name: string
- age: double
- breed: string
- gender: string
- isAdopted: boolean
- Pet ()
- Pet (string n)
- Pet (string n, double a, string b, string g, bool choice)
- getName(): string
- getAge(): double
- getBreed(): string
- getGender(): string
- getAdoption(): boolean
- setName(string n):void
- setAge(double a): void
- setBreed(string b): void
- setGender (int g): void
- setAdoption (bool choice): void
- inHumanYears(): double
- equals(Pet p): boolean
- printInfo():void
Comp 218/Winter 2021 Page 4 of 7 Assignment #4
a. 4-month old cat = .25 (which is ? of a year) * 18 = 4.5 human years
b. a 5 years old = 18 + 9 + (5-2) * 5 = 42 human years
? equals(Pet p): returns true if the name, age, breed , gender, isAdopted of the
calling object are the same, otherwise return false.
? printInfo(): displays on the console the name, age, breed , gender, isAdopted.
(see the example below)
c) (3 pts) Write a program name A4Q2.cpp that uses the class Pet as follows:
? Create an object pet1 using the default constructor.
? Create an object pet2 using the second constructor. Prompt the user for the name.
Assume the user enters valid input.
? Create an object pet3 using the third constructor. Prompt the user for the name, age,
breed, gender and isAdopted. Assume the user enters valid inputs.
? Display the info of three objects following the format shown in the example shown
below.
? Calculate the 3 Objects’ age in human years and display the result.
? Change the age of the second object pet2 which equals to the sum of pet1’s age and
pet3’s age and display pet2’s new age in human years.
? Compare the 3 objects (pet1, pet2 & pet3) and indicate whether they are equal. See
format of message in the sample output at the end of this question.
? End with a closing message.
NOTE : Be sure to save the class declaration in a header file Pet.h, the class implementation in
a Pet.cpp file. Don’t forget to include these files in the project.
Following is a sample output screens to illustrate the expected behavior of your program.
User’s input is highlighted with a red circle.
Comp 218/Winter 2021 Page 5 of 7 Assignment #4
Figure 2: Output Example of Question 2
Question #3: Array implementation (5 pts)
In this question, assuming a building has 20 rooms and 20 security guards. All rooms are closed
on the weekends. Every Monday, the first security guard, denoted S1, opens every room. Then
the second security guard, S2, begins with the second room, denoted R2, and closes every other
room. Security guard S3 begins with the third room and changes every third room (closes it if it
was open, and opens it if it was closed). Security guard S4 begins with room R4 and changes every
fourth room. Security guard S5 starts with R5 and changes every fifth room, and so on, until
security guard S20 changes R20.
After all the security guards have passed through the building and changed the rooms, which
rooms are open? You are required to use one dimensional array to find the answer. The
Comp 218/Winter 2021 Page 6 of 7 Assignment #4
program should display as following:
Figure 3: Output of Question 3
What to Hand in for Assignment 4
Be sure to test your programs in Visual Studio. - Zip the word file from question 1, the .h and two .cpp files for question 2 and the .cpp file for
question 3. - Naming convention for zip file: Create one zip file , containing all source files for your
assignment using the following naming convention:
The zip file should be called a#_studentID, where # is the number of the assignment
studentID is your student ID number. So for the fourth assignment, student 123456
would submit a zip file named a4_123456.zip.
Evaluation Criteria for Assignment 4
Question 1/4 pts
Part a) 1 pt
Part b) 1 pt
Part c) 1 pt
Part d) 1 pt
Question 2/11 pts
Header file (pet.h) 2 pts
Implementation of class (pet.cpp) 5 pts
Writer main program using class Pet 3 pts - Comments/identifier names
- Choice of variable names
- description of variables
- description of functions and steps in main
program - pt
Comp 218/Winter 2021 Page 7 of 7 Assignment #4
Question 3/5 pts - Comments/identifier names
- Choice of variable names
- description of variables
- description of the steps in code
- pt
Store data into array 2 pts
Save and display data from the array 2 pts
TOTAL 20 pts
Programs were tested in Visual Studio and zipped as
suggested in the two “How-to documents” on the
assessments page. - pts (not included in 20 pts)
Note: If assignment format is not
as requested, a perfect
assignment could result in a final
grade of 18/20
推荐阅读
- 32516互联网程序
- INFO1113 飞机调度算法
- 算法经典例题|使用蛮力法解数字迷(枚举的范围尽可能地小)(C++)(蛮力法)
- R Apply系列教程
- 使用Scrapy Python制作网络爬虫
- Python中的模糊字符串匹配
- Python读取和编辑PDF和Word文档
- 25行Python代码实现人脸识别
- Python MySQL入门详细教程