Philadelphia University Lecturer : Dr. Ali Fouad Coordinator : Dr. Ali Fouad Internal Examiner : Dr. Samer Hanna Faculty of Information Technology Department of Software Engineering Examination Paper Advanced Object Oriented Programming (721324) Sec. 1 Second Exam. Date: 15-5-2015 Second Semester of 2014-2015 Time: 50 minutes Information for Candidates 1. This examination paper contains four questions, totaling 25 marks. 2. The marks for parts of questions are shown in round brackets. Advice to Candidates 1. You should attempt all questions. 2. You should write your answers clearly. I. Basic concepts Objective: The aim of the question in this part is to evaluate your knowledge and skills concerning with the basic concepts of object oriented. Question1 (5 marks) Look at the following class definitions: :class Shape: def __cmp__(s1, s2): return cmp(s1.area(), s2.area()) class Square(Shape): def __init__(self, h): self.side = float(h) def area(self): return self.side**2 def __str__(self): return 'Square with side ' + str(self.side) class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14159*(self.radius**2) def __str__(self): return 'Circle with radius ' + str(self.radius) Given these class definitions, what will the following statements display? s = Square(2) print s.area() c = Circle(4) shapes = [Circle, Square] L = [shapes[0](6), shapes[1](8), shapes[0](10)] print s print c print L[0] print L[1] print L[2] Answer 4 Square with side 2 Circle with radius 4 Circle with radius 6 Square with side 8 II. Familiar Problem Solving Objective: The aim of the question in this part is to evaluate your ability to solve problems in object oriented programming, focusing on constructors, assessors, and other methods. Question2 (10 marks) Consider the following class hierarchy where Class PC is the supper class and the classes DesktopPC and Labtop are two subclasses derived from PC. Class ComputerStore contains a filed of type List that stores objects of type PC. PC - price: double - processor type : int - memoey size : int - screen width : int + PC (double p , string pt, int ms, int sc) + string __str__( ) + double calculateSalePrice ( ) ComputerStore -name: string - L : List + ComputerStore (string name) + void addPC (Pc c) + int getTotalPrice( ) DesktopPC + DesktopPC (...) +double calculateSalePrice ( ) Labtop + Labtop (,,,) + double calculateSalePrice( ) 1) Write a python code for class PC assuming that the method calculateSalePrice ( ) is pass. 2) Write a python code for class DesktopPc assuming that the method calculateSalePrice ( ) calculates the sale price of the DesktopPc as follow: DesktopPc = 200 + processor type*30 + memoeysize*15+ (screenwidth-14)*20 1) class PC: def __init__(self, p, pt, ms, sw): self.price=p self.processor=pt self.memory=pt self.screen=sw 2 marks def calculateSalePrice (self): 2 marks PASS def __str__(self ): 2 marks return str(self.price) + “/”+str(self.processor) +”/””+str(self.memory)+”/”+str(self.screen) 2) class DesktopPC(PC): 2 marks def __init__(self, p, pt, ms, sw): PC.__init__(self, p, pt, ms, sw) def calculateSalePrice(self ): return 200 + self.processor*30 + self.memory*15+ (self.screenwidth-14)*20 2 marks Question3 (5 marks) Write a python code for class ComputerStore found in above UML assuming that the list L has cars of different types and getTotalPrice method returns the total prices of all computers in the store. Answer class ComputerStore def __init__(self, store): 3 self.name=store self.L = [] def addPC (self, pc) self.L.append(pc) def getTotalPrice(self ): total=0 for c in self.L: total = total+ c.calculateSalePrice() return total; 3 4 Total 10 /2=5 marks III. Unfamiliar Problem Solving Objective: The aim of the question in this part is to evaluate that student can solve familiar problems with ease and can make progress toward the solution of unfamiliar problems, and can set out reasoning and explanation in clear and coherent manner. Question4 (5 marks) Suppose we want to do arithmetic with rational numbers. We want to be able to add, subtract, multiply, and divide them and to test whether two rational numbers are equal. We can add, subtract, multiply, divide, and test equality by using the following relations: n1/d1 + n2/d2 = (n1*d2 + n2*d1)/(d1*d2) n1/d1 - n2/d2 = (n1*d2 - n2*d1)/(d1*d2) n1/d1 * n2/d2 = (n1*n2)/(d1*d2) (n1/d1) / (n2/d2) = (n1*d2)/(d1*n2) Lets the rational number class as follows: class RationalNumber: def __init__(self, numerator, denominator=1): self.n = numerator self.d = denominator def __str__(self): return "%s/%s" % (self.n, self.d) Add to the class two methods __add__ , and __mul__ to perform rational number addition and multipcation, and then use the plus operator to add two rational numberobjects. class RationalNumber: """ Rational Numbers with support for arthmetic operations. >>> a = RationalNumber(1, 2) >>> b = RationalNumber(1, 3) >>> a + b 5/6 >>> a - b 1/6 >>> a * b 1/6 >>> a/b 3/2 """ def __init__(self, numerator, denominator=1): self.n = numerator self.d = denominator def __add__(self, other): if not isinstance(other, RationalNumber): other = RationalNumber(other) n = self.n * other.d + self.d * other.n d = self.d * other.d return RationalNumber(n, d) def __sub__(self, other): if not isinstance(other, RationalNumber): other = RationalNumber(other) n1, d1 = self.n, self.d n2, d2 = other.n, other.d return RationalNumber(n1*d2 - n2*d1, d1*d2) def __mul__(self, other): if not isinstance(other, RationalNumber): other = RationalNumber(other) n1, d1 = self.n, self.d n2, d2 = other.n, other.d return RationalNumber(n1*n2, d1*d2) def __div__(self, other): if not isinstance(other, RationalNumber): other = RationalNumber(other) n1, d1 = self.n, self.d n2, d2 = other.n, other.d return RationalNumber(n1*d2, d1*n2) def __str__(self): return "%s/%s" % (self.n, self.d)