Lab 5 OOPS
Lab 5 OOPS
Rahman
2021ugcs077
q1.
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid,
Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of
the hierarchy. Make the hierarchy as deep (i.e., as many levels) as possible.
Specify the instance variables and methods for each class. The private
instance variables of Quadrilateral should be the
x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a
program that instantiates objects of your classes and outputs each
object’s area.
Code
import java.util.Scanner;
class Quadilateral {
private double x1, y1, x2, y2, x3, y3, x4, y4;
Quadilateral(double x1, double x2, double y1, double y2, double x3,
double y3, double x4, double y4) {
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.x4 = x4;
this.y1 = y1;
this.y2 = y2;
this.y3 = y3;
this.y4 = y4;
System.out.println(x1 + " " + y1);
System.out.println(x2 + " " + y2);
System.out.println(x3 + " " + y3);
System.out.println(x4 + " " + y4);
double area() {
return (x2 - x1) * (y3 - y1);
}
Output
Q2
Create a general class ThreeDObject and derive the classes Box, Cube,
Cylinder and Cone from it. The class ThreeDObject has methods
wholeSurfaceArea ( ) and volume(). Override these two methods in each of
the derived classes to calculate the volume and whole surface area of each
type of three-dimensional objects. The dimensions ofthe objects are to be
taken from the users and passed through the respective constructors of
each derived class. Write a main method to test these classes.
import java.util.Scanner;
double wholeSurfaceArea() {
return 2 * (l * b + b * h + h * l);
}
double volume() {
return l * b * h;
}
}
Cylinder(double r, double h) {
this.r = r;
this.h = h;
}
double wholeSurfaceArea() {
return 2 * 3.14 * r * (r + h);
}
double volume() {
return 3.14 * r * r * h;
}
}
Cone(double r, double h)
{ this.r = r;
this.h = h;
}
double wholeSurfaceArea() {
return 3.14 * r * (r + Math.sqrt(Math.pow(r, 2) + Math.pow(h,
2)));
}
double volume() {
return .33 * 3.14 * r * r * h;
}
}
Output
Q3
Write a program to create a class named Vehicle having protected
instance variables regnNumber, speed, color, ownerName and a method
showData ( ) to show “This is a vehicle class”. Inherit the Vehicle class into
subclasses named Bus and Car having individual private instance variables
routeNumber in Bus and manufacturerName in Car and both of them
having showData ( ) method showing all details of Bus and Car
respectively with content of the super class’s showData ( ) method.
import java.util.Scanner;
Output
q4.Create an abstract class MotorVehicle with the following details:
a. Data Members:
b. Methods:
Code
import java.util.Scanner;
void display() {
System.out.println("MD Faisal
Rahman");
System.out.println("Car name is " + modelName);
System.out.println("Car number is " + modelNumber);
System.out.println("Car price is " + modelPrice);
System.out.println("Car discountRate is " + discountRate);
}
int discount() {
return modelPrice * (100 - discountRate);
}
}
Output
Q5
Implement the below Diagram. Here AbstractProduct is only abstract class.
import java.util.Scanner;
Q6
Demonstrates following two scenarios for Audi Class:
a. BRAND is a final variable and initialized to “AUDI” during
declaration itself and can never be changed. They remain constant
for all the objects of class type Audi.
b. EngineNumber is only declared as final but not initialized. These kind
of variables could be initialized in Constructor. They remain constant
only for the object of
Audi, i.e., each Audi object can have different.
Sol
Scenaíio A: In this scenaíio, we have a final vaíiable BRAND initialized to "AUDI"
duíing declaíation itself, and it cannot be changed. ľhis means that all
objects of the class Audi will shaíe the same constant value foí BRAND.
Code
Output
In this code, the BRAND vaíiable is a class-level constant, and it íemains the
same foí all objects of the Audi class.
Scenaíio B: In this scenaíio, EngineNumber is declaíed as final but not
initialized in the class. It can be initialized in the constíuctoí, and each Audi
object can have a diffeíent value foí it.
Code
public class Audi {
audi1.printCarDetails();
audi2.printCarDetails();
}
}
Output
Q7
Demonstrates final method and final Class with the help of example.
Final
method
Code
class Parent {
final void displayMessage() {
System.out.println("chintu kumar");
System.out.println("This is a final method in the Parent
class.");
}
}
Final class
final class FinalClass {
Output