DUBLIN CITY UNIVERSITY REPEAT EXAMINATIONS 2008 MODULE: Object-oriented Programming I - EE219 COURSE: B.Eng. in Electronic Engineering (Year 2 & 3) B.Eng. in Information Telecomms Engineering (Year 2 & 3) B.Eng. in Digital Media Engineering (Year 2) YEAR: 2 and 3 EXAMINERS: Dr. Derek Molloy (DCU Extension 5355) Dr. F. Owens TIME ALLOWED: 2 Hours INSTRUCTIONS: Please answer FOUR questions. All questions carry equal marks Please note that where a candidate answers more than the required number of questions, the examiner will mark all questions attempted and then select the highest scoring ones. Requirements for this paper Please tick (X) as appropriate Log Table Graph Paper Attached Answer Sheet Statistical Tables Floppy Disk Actuarial Tables THE USE OF PROGRAMMABLE OR TEXT STORING CALCULATORS IS EXPRESSLY FORBIDDEN PLEASE DO NOT TURN OVER THIS PAGE UNTIL YOU ARE INSTRUCTED TO DO SO EE219 Object-oriented Programming I Repeat Examination Page 1 of 5
Question 1 1(a) Examine the following segment of code that has several errors: 00 #include <iostream> 01 using namespace std; 02 03 class Shape 04 05 private: 06 float posx=0.0f, posy=0.0f; 07 public: 08 virtual Shape(float, float); 09 virtual void display(); 10 }; 11 12 13 class Circle : public Shape 14 15 float radius; 16 public: 17 Circle(float, float, float); 18 virtual void setradius(float); 19 virtual void display(); 20 }; 21 22 Shape::Shape(float x, float y): x(posx), y(posy) } 23 24 void Shape::display() 25 26 cout << "The Shape is at position (" << x << "," << y << ")\n"; 27 } 28 29 Circle::Circle(float rad, float x, float y): Shape(x,y), radius(rad) } 30 31 void Circle::display() 32 33 Circle::display(); 34 cout << " and a radius of " << rad << endl; 35 } 36 37 int main() 38 39 Circle c(20, 30); 40 c.display(); 41 return 0; 42 } Locate the 9 (approximate) errors and describe why you believe there is an error at that location. Use the line numbers to help you to explain your answers. [18 marks] 1(b) Organise the following concepts into suitable class hierarchies: television, channel, volume, DVD player, video player, LCD television, Plasma television, dimensions, aerial. Concisely explain the rationale for your groupings and create appropriate additional classes if required. [7 marks] EE219 Object-oriented Programming I Repeat Examination Page 2 of 5
Question 2 2(a) Examine the following class definitions: #include <iostream> #include <string> using namespace std; class Vehicle string color; string brandname; public: Vehicle(string, string); virtual void display(); }; class Car: public Vehicle int numberseats, numberdoors, numberwheels; }; public: Car(string, string, int, int, int); Car(Vehicle, int, int, int); virtual void display(); Write an implementation for each of the methods listed in the class definition. Write a main() function that would test the methods and all constructors. Write a class for a Motorbike that suits the example above. [22 marks] 2(b) Where s and t are strings, describe the working and output of this line of code: cout << (s.length() < t.length()? s : t); [3 marks] Question 3 3(a) Discuss the output of the following C++ control statements: - while (x<5) cout << The value of x is << x++ << endl; } - do cout << The value of x is << x++ << endl; } while (x<5); - for (x=0; x<5; x++) cout << The value of x is << x << endl; } - for (x=0; x<5; ++x) cout << The value of x is << x << endl; } Assuming that before each control statement x is an int with value 0. [5 marks] 3(b) Explain pass-by-value and pass-by-reference. Write a short segment of code to demonstrate the difference? 3(c) Describe C++ namespaces using a code example. Describe Java packages, again using a code example. How do C++ namespaces compare to Java packages? EE219 Object-oriented Programming I Repeat Examination Page 3 of 5
Question 4 4(a) Java applets generally do not have a main() method. What is the lifecycle of a Java applet and what standard methods do we have to implement? If we do not implement one of these methods, will the applet compile? 4(b) What is the output of this segment of C++ code? int z[5] = 1,2,3,4,5}; int *p = &z[0]; for (int i=0; i<5; i++) (*p)++; (*p++)++; } for (int i=0; i<5; i++) cout << "The value of x[" << i << "] is " << z[i] << endl; } [5 marks] 4(c) Describe the Java pre-defined variables super and this. Write a short piece of code to demonstrate their possible uses. Question 5 5(a) Write the following Java applet and associated HTML file: where the user inputs u and v as 2-D vectors and theta is the angle between these vectors in degrees, which is calculated automatically when the go button is pressed, according to the formula: θ = cos 1 ( u. v / u v ) where u.v = u 1 v 1 + u 2 v 2 and u = u + (similarly for v) 2 2 1 u2 Please see table Q5.1 to see the Math class methods available. [25 marks] EE219 Object-oriented Programming I Repeat Examination Page 4 of 5
Table Q5.1 The Math class methods Method Summary static double abs(double a) Returns the absolute value of a double value. static float abs(float a) Returns the absolute value of a float value. static int abs(int a) Returns the absolute value of an int value. static long abs(long a) Returns the absolute value of a long value. static double acos(double a) Returns the arc cosine of an angle, in the range of 0.0 through pi. static double asin(double a) Returns the arc sine of an angle, in the range of -pi/2 through pi/2. static double atan(double a) Returns the arc tangent of an angle, in the range of -pi/2 through pi/2. static double atan2(double y, double x) Converts rectangular coordinates (x, y) to polar (r, theta). static double ceil(double a) Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer. static double cos(double a) Returns the trigonometric cosine of an angle. static double exp(double a) Returns Euler's number e raised to the power of a double value. static double floor(double a) Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer. static double IEEEremainder(double f1, double f2) Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. static double log(double a) Returns the natural logarithm (base e) of a double value. static double max(double a, double b) Returns the greater of two double values. static float max(float a, float b) Returns the greater of two float values. static int max(int a, int b) Returns the greater of two int values. static long max(long a, long b) Returns the greater of two long values. static double min(double a, double b) Returns the smaller of two double values. static float min(float a, float b) Returns the smaller of two float values. static int min(int a, int b) Returns the smaller of two int values. static long min(long a, long b) Returns the smaller of two long values. static double pow(double a, double b) Returns the value of the first argument raised to the power of the second argument. static double random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. static double rint(double a) Returns the double value that is closest in value to the argument and is equal to a mathematical integer. static long round(double a) Returns the closest long to the argument. static int round(float a) Returns the closest int to the argument. static double sin(double a) Returns the trigonometric sine of an angle. static double sqrt(double a) Returns the correctly rounded positive square root of a double value. static double tan(double a) Returns the trigonometric tangent of an angle. static double todegrees(double angrad) Converts an angle measured in radians to an approximately equivalent angle measured in degrees. static double toradians(double angdeg) Converts an angle measured in degrees to an approximately equivalent angle measured in radians. EE219 Object-oriented Programming I Repeat Examination Page 5 of 5