Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
King Saud University College of Computer and Information Sciences Department of Computer Sciences CSC113: Computer Programming 2 Tutorial & Lab # 5: Abstract Classes, Polymorphism and Interfaces Exercise 1: Table-1 presented mathematical operations classes. Hence the data and methods for each class in the mathOp hierarchy UML class diagram (Figure-1) are illustrated in this table. Instanceof Table-1: Mathematical operations classes Class mathOp: opName: String Op1 Op2 + toString() + result(): double Addition: + Addition (double, double) Subtraction: + Subtraction (double, double) Multiplication: + Multiplication (double, double) Division: + Division (double, double) result() toString() Abstract opName Op1 + Op2 Op1 - Op2 Op1 * Op2 If Op2 != 0 result = Op1 / Op2 else result = -99999.99999 opName “: “ Op1 “ + ” Op2 “ = “ result() opName “: “ Op1 “ - ” Op2 “ = “ result() opName “: “ Op1 “ * ” Op2 “ = “ result() If Op2 == 0 “Error: Divide by zero” Else opName “: “ Op1 “ / ” Op2 “ = “ result() {Abstract} mathOp Addition Subtraction Multiplication Division Figure-1: mathOp hierarchy UML class diagram Implement the mathOp classes and then write the test class that has main() method as follow: 1. 2. 3. 4. Define four objects (one for each non-abstract class). Define an array of mathOp consists of 4 objects. Store the objects defined in step 1 into the array defined in step 2. Print the array data except the objects of type Subtraction. Exercise 2: Implement the Shape hierarchy shown below. Each TwoDimensionalShape should contain method getArea() to calculate the area of the two dimensional shape. Each ThreeDimensionalShape should have methods getArea() and getVolume() to calculate the surface area and volume, respectively. 1 Create a program that uses an array of Shape references to object of each concrete class in the hierarchy. The program should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or ThreeDimensionalShape. If the shape is TwoDimensionalShape, display its area. If a shape is a ThreeDimensionalShape, display its area and volume. Output part of the main… shapes[0]= shapes[1]= shapes[2]= shapes[3]= shapes[4]= shapes[5]= new new new new new new Circle(4); Cube(2); Sphere(3); Square(7); Circle(1); Sphere(4); Output should be like this: This is a 2D shape. Its area = 50.26548245743669 ------------------This is a 3D shape. Its area = 24.0 Its volume = 8.0 ------------------This is a 3D shape. Its area = 113.09733552923255 Its volume = 84.82300164692441 ------------------This is a 2D shape. Its area = 49.0 ------------------This is a 2D shape. Its area = 3.141592653589793 ------------------This is a 3D shape. Its area = 201.06192982974676 Its volume = 201.06192982974676 Exercise 3: An airport charges any aeroplane lands in it by the following rules: 1. Every adult is charged 2 S.R. for every kilogram they weigh. 2. Every child is charged 0.5 S.R for every kilogram they weigh. 2 3. Every piece of luggage is charged 1.5 S.R. for every kilogram it weighs. Every passenger has a name and a weight and every piece of luggage has an ID and weight. Given the UML diagram of this problem, write a program that process one aeroplane and display the information (name/ID, weight and charge) of the luggage or the passengers that cost a charge of more than 50 S.R. Also, print the number of all adults, all children and all pieces of luggage in that aeroplane. While you are implementing your program, consider the following: - Protect any method that is not intended to be overridden, from being overridden by subclasses. - Protect any concrete class from being inherited. Hint: Represent an aeroplane as a collection (array) of Chargeable Objects. Output Part of the main… chargeables[0]= chargeables[1]= chargeables[2]= chargeables[3]= chargeables[4]= new new new new new Child("Ahmed Ali",45.5); Adult("Khalid Nasser",86.4); Child("Noha Ameen",62); Luggage(324,35.5); Adult("Salem Saeed",94.25); Output should be like this: Name: Khalid Nasser Weight: 86.4 Charge: 172.8 -------------ID: 324 Weight: 35.5 Charge: 53.25 -------------Name: Salem Saeed Weight: 94.25 Charge: 188.5 -------------Number of adults = 2 Number of children = 2 Number of pieces of luggage = 1 -------------- 3