DOC PREVIEW
Purdue ECE 462 - Exam 2

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ECE 462 Exam 109:30-10:20AM, October 20, 2008I certify that I will not receive nor provide aid to any other student for this exam.Signature:You must sign here. Otherwise, the exam is not graded. Please put your photo ID on the desk.This exam is printed double sides.Write your answers next to the questions. If you need more space, you can use the firstblank page.This is an open-book, open-note exam. You can use any book or note or program printouts.No electronic device is allowed. Please turn off your cellular phone now .Two outcomes are tested in this exam. To pass each outcome, you must receive 50% of the points.• Outcome 5: an ability to overload operators in C++.• Outcome 6: an ability to incorporate exception handling in object-oriented programs.You need to obtain 50% of the points in each outcome to pass the outcome. There are 16 questions in thisexam. If a program has a syntax error, point out which line causes the error and provide a brief explanation.If there are multiple errors, you need to write only one of them.If a question has a numeric answer, you can write the procedure without the final result. For example, youcan write “1 + 2” instead of “3”.Name:Seat:Name: 1 Seat:This page is blank.Name: 2 Seat:Contents1 C++ Exception Objects (outcome 6, 10 points) 42 Catch C++ Exception (outcome 6, 5 points) 53 Overload*Operator (outcome 5, 5 points) 64 Overload - Operator (outcome 5, 5 points) 75 Overload = Operator (outcome 5, 10 points) 86 Java Exception Class (outcome 6, 5 points) 97 Overload << Operator (outcome 5, 5 points) 108 UML Sequence Diagram (10 points) 119 Overload Postfix ++ Operator (outcome 5, 5 points) 1110 friend in C++ (outcome 5, 5 points) 1311 Java Exception (outcome 6, 5 points) 1312 Exception Handling (outcome 6, 5 points) 1313 UML Class Diagram (10 points) 1414 Java Exception and Array (outcome 6, 5 points) 1515 Overload =, <, > Operators (outcome 5, 5 points) 1616 try - catch (outcome 6, 5 points) 16Pass Outcomes:Total Score:Name: 3 Seat:1 C++ Exception Objects (outcome 6, 10 points)What is the output of this program?Answer:caught Type 2 2#include <iostream>#include <string>using namespace std;class ExceptionType1{protected:static int counter;public:ExceptionType1() { counter ++; }virtual ˜ExceptionType1() { counter --; }int getCounter(){ return counter; }};int ExceptionType1::counter = 0;class ExceptionType2: public ExceptionType1{public:ExceptionType2() { counter ++; }};void f (int i) throw (ExceptionType1, ExceptionType2){switch (i){case 1:throw ExceptionType1();break;case 2:throw ExceptionType2();break;default:cout << "no exception" << endl;}}void g (int i) throw (ExceptionType1, ExceptionType2){try {f(i);} catch (ExceptionType1 et1) {throw ExceptionType2();}}Name: 4 Seat:int main(){try {g(2);g(1);g(0);} catch (ExceptionType2 et2 ) {cout << "caught Type 2 " << et2.getCounter() << endl;} catch (ExceptionType1 et1 ) {cout << "caught Type 1 " << et1.getCounter() << endl;}return 0;}2 Catch C++ Exception (outcome 6, 5 points)Please write the code so that the caller can catch the two types of exceptions.Answer:} catch (ExceptionType1 et1 ) {cout << et1.getMessage() << endl;} catch (ExceptionType2 et2 ) {cout << et2.getValue() << endl;}#include <iostream>#include <string>using namespace std;class ExceptionType1{private:string et1_message;public:ExceptionType1(string m): et1_message(m) { }string getMessage() const { return et1_message; }};class ExceptionType2{private:int et2_value;public:ExceptionType2(int v) { et2_value = v; }int getValue() const { return et2_value; }};void f (int i) throw (ExceptionType1, ExceptionType2){switch (i){case 1:Name: 5 Seat:throw ExceptionType1("type 1");break;case 2:throw ExceptionType2(2);break;default:cout << "no exception" << endl;}}int main(){srand(time(NULL)); // initialize the random numbertry {f (rand() % 3);}// >>>>>// catch exception// if it is type 1, print the message// if it is type 2, print the value// <<<<<return 0;}3 Overload*Operator (outcome 5, 5 points)Please overload the*operator between a Vector object and double.Answer:Vector operator*(const Vector & v, double d){return Vector (v.getX()*d, v.getY()*d, v.getZ()*d);}#include <iostream>#include <string>using namespace std;// do not change the Vector classclass Vector{private:double v_x;double v_y;double v_z;public:// do not change the public methodsVector(double x, double y, double z){ v_x = x; v_y = y; v_z = z; }void print(){ cout << "(" << v_x << "," << v_y << "," << v_z << ")" << endl; }double getX() const { return v_x; }Name: 6 Seat:double getY() const { return v_y; }double getZ() const { return v_z; }};// >>>>>// overload operator*for multiplication between a Vector object// and double. return a Vector object// <<<<<int main(int argc, char*argv[]){Vector v1(2, 3.1, 5.7);Vector v2 = v1*0.5;v1.print();v2.print();/*output:(2,3.1,5.7)(1,1.55,2.85)*/return 0;}4 Overload - Operator (outcome 5, 5 points)Please overload the - operator to change the direction of a vector object.Answer:Vector operator - (){return Vector (-v_x, -v_y, -v_z);}#include <iostream>#include <string>using namespace std;class Vector{private:double v_x;double v_y;double v_z;public:Vector(double x, double y, double z){ v_x = x; v_y = y; v_z = z; }// >>>>>// overload - (negation) operator// <<<<<void print(){ cout << "(" << v_x << "," << v_y << "," << v_z << ")" << endl; }};int main(int argc, char*argv[]){Name: 7 Seat:Vector v1(2, 3.1, 5.7);Vector v2 = -v1;Vector v3(-0.8, 0.4, 7.5);v1.print();v2.print();v3.print();v3 = -v1;v3.print();/*output:(2,3.1,5.7)(-2,-3.1,-5.7)(-0.8,0.4,7.5)(-2,-3.1,-5.7)*/return 0;}5 Overload = Operator (outcome 5, 10 points)Please overload the = (assignment) operator for Vector.Answer:Vector & operator = (const Vector & v){if (this != & v){delete [] v_element;v_size = v.v_size;v_element = new double[v_size];for (int ecnt = 0; ecnt < v_size; ecnt ++){ v_element[ecnt] = v.v_element[ecnt]; }}return*this;}#include <iostream>#include <string>using namespace std;class Vector{private:double*v_element;int v_size;public:Vector(int s, double*elem){v_size = s;v_element = new double[s];for (int ecnt = 0; ecnt < s; ecnt ++){ v_element[ecnt] = elem[ecnt]; }Name: 8 Seat:}// copy constructorVector(const Vector & v){v_size = v.v_size;v_element = new double[v_size];for (int ecnt = 0; ecnt < v_size; ecnt ++){ v_element[ecnt] = v.v_element[ecnt]; }}// >>>>>// overload = (assignment) operator// <<<<<void print(){for (int ecnt = 0; ecnt < v_size; ecnt ++){ cout <<


View Full Document

Purdue ECE 462 - Exam 2

Download Exam 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exam 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exam 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?