联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> javajava

日期:2020-11-07 11:28

SCHOOL OF COMPUTING

UNIVERSITI TEKNOLOGI MALAYSIA

SESSION 2020/2021 SEMESTER 1

Code & Subject : SECJ 1023 – Programming Technique II

Week 3 Lab Tutorial

Introduction to Classes and Objects

Duration: 8.30am – 12.00pm (including submission)

Exercise Material

? The tasks to be completed are stated on this document

? You will be using your preferred IDE to write your program

Pair Programming

? You will be adopting the “Pair Programming” strategy do this tutorial

? Members for each pair are assigned automatically using the break-out session

? Each pair must ensure to switch role (navigator and driver) periodically

Plagiarism Warning

? You are allowed to refer to any resources

? You are allowed to have discussions with others during the exercise session

? However, you and your partner must do the tutorial on your own

? Any kind of plagiarism (e.g. copying and pasting code by any means) would lead your

submission dismissed. This applies to both parties, students that copy other’s code and

student that give their code to others.

Deliverable Item

? Write your name and your pair’s name at the top of every program

? Submit only the three sets of source code you have created

? Zip the folder and upload via e-learning

? Only one member from each pair needs to perform the submission.

Questions

1. Consider the declaration given in Program 3.1:

//Program 2.4

class Pet {

private:

string name;

int age;

public:

void setName (string n);

void setAge (int a);

string getName ()

{ return name; }

int getAge()

{ return age; }

void print() const;

};

a. In your main program, write a C++ statement that instantiates an object rabbit of type

Pet.

b. Set the pet’s name to Buttons and the pet’s age to 3, respectively.

c. The setName and setAge functions are not inline. Write the code to define these two

functions.

d. Write the definition of the member function print that prints the contents of name and

age.

e. Write a C++ statement that prints the values of the member variables of the object

rabbit.

f. Complete Program 3.1 then compile and execute it.

g. Next separate your program into application, specification and implementation files

respectively.

2. Write a class named Cone that finds the baseArea and volume of a conical structure.

The class’ private data are radius and height.

a. Create four class functions that performs the following actions:

i. Read the two private data

ii. Calculate the value for baseArea, according to the following equation:

baseArea = PI * radius * radius

iii. Calculate the value for volume, based on the following equation:

volume = (1/3) * baseArea * height

iv. Display the two private data, baseArea, as well as volume.

b. Write the main () function that performs the following tasks:

i. Declare an object from Cone class, called cn1

ii. Call the four functions using the sequence in a(i) to a(iv) above.

3. A counter variable is normally used in a program to keep track of the current count value.

Create your own counter data type which is able to perform common counter operations

such as increment (by one) the counter value. It is also able to set the counter to a specific value

and to retrieve the counter value. The minimum value the counter can attain is zero, while the

maximum value attained is stored in attribute maxValue. The class should have the following

private members (attributes):

Attribute Description

int count The counter value

int maxValue The maximum counter value

The class should also have the following member functions (methods):

Attribute Description

Increment Increments the counter

Decrement Decrement the counter

setCount Sets the counter value

setMaxVal Sets the maximum value

getCount Returns the count

getMaxValue Returns the maximum value

In the main program, test the workability of your member functions by first setting an initial

value to the counter. Display this value before proceeding. Then test other functions by

randomly incrementing and decrementing the object’s value. Be sure to keep track of your

actions. At the same time, you should also make sure that your minimum value should not be

less than zero. Once this is reached, you will not be allowed to do further decrements.

Consequently, the maximum value should also be maintained. The user should always ensure

that the current value does not exceed the maximum value. If this happens no more increments

will be allowed. Display appropriate comments for these two cases. Finally, when there are no

more increments or decrements to do, you should print the final value or set of the operations.

For purposes of reusability, separate the driver, specification and implementation file.

[[ends]]


版权所有:留学生程序网 2020 All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。