Вы находитесь на странице: 1из 7

ET0301 CPA 2011/2012 Semester 2

ASSIGNMENT 2 @ 5% Instructions: 1. There are a total of twelve(12) questions. 2. You will be assigned one(1) question according to your class list number, e.g., Question 1 is assigned to the first student in the class list, and so on. 3. Complete your assignment by the 16th week of the semester (week starting 30/01/2012). 4. Submit hardcopy of your program source codes to your Lecturer on week 16. 5. Use object orientated design (OOD) with proper data members (attributes) and function members (behaviours) for all programs. 6. Assignment is for individual students work.

Question 1:

In C++, an object is an instance of a programmer-specified class that can have data assigned to its members and that can be called by the program. Given a class definition Monkey with attributes age and level, creates an object name whoMonkey and complete the main() function by calling the class methods to assign and display values.
class Monkey { private: //Attributes. int age, level; public: //Methods. void assignData(int, int ); void displayData(void ); }; void Monkey::assignData(int a, int b) { age = a; level = b; } void Monkey::displayData() { cout << Monkeys age is << age; cout << with level << level << endl; } void main() { int a = 18; int b = 10;

//Monkeys age is 18. //Monkeys level is 10.

//Write your codes in the answer booklet.

Question 2:

Referring to the incomplete program as shown below. Write codes for the function members add() and show() that returns the total of the two private data members value1 and value2. The program output is shown below.
The sum is 120 __

#include <iostream> using namespace std; class Sum { private: int value1, value2; public: void set(void); int add(void ); void show(void ); }; void Sum::set() { value1 = 100; value2 = 20; } int Sum::add() { //Write code to add and return the total of the //two data members.

//Data members.

//Member functions.

} void Sum::show() { //Write code to display the sum.

} void main() { Sum mysum; mysum.set(); mysum.show();

Question 3:

Referring to the class Compute below. Add an overloaded member function to return the minimum of the two integer parameters, iw and ix.
class Compute { public: //Returns the minimum of two pass-in integer numbers. int minimum(short, short ); //Write overloaded function to return //the minimum of two pass-in integer numbers. }; int Compute::minimum(short sw, short sx) { if (sw < sx) return(sw); else return(sx); }

Question 4:

Referring to the class CBuffer as shown below, write a constructor to dynamically allocate an array pchildBuffer using the new operator, the arrays 640 elements are to be of data type unsigned char. Write codes for the function initbuffer(); to initialise the array with a preset value of 255 for all elements. Complete the program by coding the destructor to free all memories when exiting.
class CBuffer { private: unsigned char *pchildBuffer; public: CBuffer(); void initbuffer(void ); ~CBuffer(); };

Question 5:

Referring to the class Cer as shown below, write a constructor to dynamically allocate an array ptrCer using the new operator, the arrays 40 elements are to be of data type int. Write codes for the function initCer(); to initialise the array with a preset value of 0 for all elements. class Cer { private: unsigned char *ptrCer;

public: Cer(); void initCer(void ); };

Question 6:

The program listing shown below was written without the use of class and object. Re-write the entire program with object oriented design (OOD). Write a class bioData with public function members (as shown below) to access the two private data members. Complete your program with a driver routine and the class member function implementation. The object name is called data.
void void setValues(void ); showValues(void );

//Original program. #include <iostream> using namespace std; void main() { int age; float height; cout << "Enter age and height: "; cin >> age >> height; //Display persons height with 1 decimal point. cout << "Age is " << age << endl; cout << "Height is " << height << endl; }

Question 7:

You are given a programming task by your supervisor to write C++ function(s) to show the content of a single pass-in parameter on the monitor screen, the single argument could be one of the data type given as integer, character or double. Your supervisor has instructed you to use only one function name, he has also suggested using overloading method instead of using template approach. Your function is called display as this word accurately reflects the purpose of the function; there is no value return upon calling the function. Complete your task by writing the necessary functions and prototypes that accept a single argument and display this argument as integer, character or double.
Hint: //Function implementation. cout << The argument is << argument;

Question 8:

The following C++ codes are declaration of the class CAcc.


class CAcc { private: double balance; double saving_rate; public: CAcc(); void earning(); };

//Interest rate.

(a) Write the implementation function for the constructor to initialize the followings: (i) balance is $10000, (ii) saving interest rate is at 2.0%. (b) Implement the function member, earning(); to calculate and show the amount of interest earned per month for the saving account; accurate up to two(2) decimal points.

Saving account: $16.67 per month

Notes: To compute interest earn per month, Interest per month = (balance * interest rate) / 12. Question 9: Referring to Text Book Programming Exercise 10.21 of Chapter 10. An n-sided regular polygon has n sides of the same length having same angles. Design a class names RegularPolygon that contains: A private int data member n that defines number of sides. A private double data member named side that stores length of the side. A private double data member named x that defines x-coordinate of the centre of the polygon. A private double data member named y that defines y-coordinate of the centre of the polygon. A no argument constructor that creates a regular polygon with n =3, side =1, x = 0 and y = 0. A constructor that creates a regular polygon with user-defined side and length of side with x = 0 and y = 0. A constructor that creates a regular polygon with user-defined side and length of side with x-coordinate and y-coordinate. The accessor and mutator functions for all data. A function getPerimeter() that gets value of the perimeter. A function getArea() that gets the area of the polygon. Use the formula : Area = n x s2 / ( 4 x tan( /n )

Write a test program that creates RegularPolygon object using no-argument constructor, RegularPolygon object (6,4) object, RegularPolygon object (10, 4 5.6, 7.8) object. For each object, display it perimeter and area.

Question 10:

Design a class to represent complex numbers with suitable data and functions for the following test program. void main() { Complex a(1.1, 2.3); Complex b(0.7, -0.9); Complex c; cout << endl a.display(); cout << endl b.display(); << // << // "a: "; Show: 1.1 + j2.3 "b: "; Show: 0.7 - j0.9

c = a.add(b); cout << endl << "c=a+b : "; c.display(); // Show: 1.8 + j1.4 cout << endl; } Expected output: a: 1.1 + j2.3 b: 0.7 - j0.9 c=a+b : 1.8 + j1.4

Question 11:

Design a class to represent matrices with suitable data and functions for the following test program. void main() { cout << "Enter no. of matrix rows & columns (up to 10 10): "; int i, j; cin >> i >> j; Matrix a(i,j); // i-row x j-column Matrix b(i,j); // i-row x j-column

Matrix c(i,j); // i-row x j-column cout << "\nMatrix A" << endl; a.readData(); // Prompt user to key in elements cout << "\nMatrix B" << endl; b.readData(); // Prompt user to key in elements c = a.add(b); cout << "\nSum" << endl; c.display(); // Show the matrix sum } Sample running session (with user input in bold): Enter no. of matrix rows & columns (up to 10 10): 2 3 Matrix A Enter the 3 data for row 1: 1.2 -1.0 2.1 Enter the 3 data for row 2: 0.9 1.1 1.3 Matrix B Enter the 3 data for row 1: 2.2 1.0 -1.2 Enter the 3 data for row 2: 1.0 -2.0 1.3 Sum 3.4 0 0.9 1.9 -0.9 2.6

Question 12:

Design a class to represent a point on the graph with x and y coordinates. Your class should have the following: - variables for the x and y value - constructors to initialise the x, y value or set the x and y to the default 0 - functions to get and set the values of x and y Create two objects from the above class and use the followng cordinates: (0,0) and (4.5, 7.2) respectively. Create an additional function in the class above1. This function will accept an object of the same class to calculate the distance between itself and the other object.
<ET0301 Assignment 2 BK.doc>

Вам также может понравиться