DATE 7/6/21: Object Oriented Programming (Abstract Classes, Upcasting, Downcasting)
DATE 7/6/21: Object Oriented Programming (Abstract Classes, Upcasting, Downcasting)
DATE 7/6/21: Object Oriented Programming (Abstract Classes, Upcasting, Downcasting)
LAB # 10
Task # 01: You have to implement the UML diagram given below.Also Design
and implement a subclass “EquilateralTriangle” having a double variable side
denoting the three sides of the equilateral triangle [Note that since all the 3
sides are equal, the constructor will have only one parameter]. The area and
perimeter of the equilateral triangle are given as follows:
Area = ¼**(side)2
Perimeter = 3*side
Provide accessor methods for the sides. Test your class using the TestShapes
and DownCastingShapes classes.
Solution:
package lab10task2;
/**
*
* @author Sameen Arshad
*/
public class Lab10Task2 {
package lab10task2;
/**
*
* @author Sameen Arshad
*/
abstract class Shape {
/**
*
* @author Sameen Arshad
*/
class EquilateralTriangle extends Shape {
package lab10task2;
/**
*
* @author Sameen Arshad
*/
public class Circle extends Shape {
private double radius;
public Circle(double r) {
radius = r;
}
@Override
public double Area() {
return Math.PI * (radius * radius);
}
package lab10task2;
/**
*private double length;
private double width;
@Override
public double perimeter() {
return 2 * (length + width);
}
/**
*
* @author Sameen Arshad
*/
public class Square extends Shape {
private double length;
public Square(double length) {
this.length = length;
@Override
public double Area() {
return length *length;
}
public double perimeter() {
return 4 * (length);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab10task2;
import java.util.Random;
/**
*
* @author Sameen Arshad
*/
public class TestShapes {
switch (assigner) {
case 0:
randomShapes[i]
= new Rectangle(generator.nextDouble() * DIMENSION, generator.nextDouble() *
DIMENSION);
break;
case 1:
randomShapes[i] = new Circle(generator.nextDouble() * DIMENSION);
break;
case 2:
randomShapes[i] = new Square(generator.nextDouble() * DIMENSION);
break;
}
}
return randomShapes;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab10task2;
/**
*
* @author Sameen Arshad
*/
public class DownCastingShapes {
Output:
Task # 02: Write a program for exam department which provide abstract
class and method of Exam type which contains general methods related to
exams and can be used by different department for conducting exams.
Solution:
package lab10task1;
/**
*
* @author Sameen Arshad
*/
public class Lab10Task1 {
obj.Attendence();
obj.Start();
obj.End();
if (obj instanceof DepartmentX) {
DepartmentX obj2 = (DepartmentX) obj;
obj.Collect_Copies();
obj2.Calculate_Gpa();
}
}
package lab10task1;
/**
*
* @author Sameen Arshad
*/
abstract class Exam {
protected int std_present;
protected int std_absent;
Exam(int std_present, int std_absent) {
this.std_present = std_present;
this.std_absent = std_present;
}
}
package lab10task1;
/**
*
* @author Sameen Arshad
*/
class DepartmentX extends Exam {
Output: