[go: up one dir, main page]

0% found this document useful (0 votes)
13 views16 pages

Lab 5 OOPS

oops docx

Uploaded by

Akash Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views16 pages

Lab 5 OOPS

oops docx

Uploaded by

Akash Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

MD Faisal

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);
}

class Trapezoid extends Quadilateral {


Trapezoid(double x1, double x2, double y1, double y2, double x3,
double y3, double x4, double y4) {
super(x1, x2, y1, y2, x3, y3, x4, y4);
}
}

class Parallelogram extends Trapezoid {


Parallelogram(double x1, double x2, double y1, double y2, double x3, double y3, do
super(x1, x2, y1, y2, x3, y3, x4, y4);
}
}

class Rectangle extends Parallelogram {


Rectangle(double x1, double y1, double l, double b) { super(x1, x1 + l, y1, y1, x1
}

class Square extends Rectangle { Square(double x1, double y1, double a)


{
super(x1, y1, a, a);
}
}

public class lab5q1 {


public static void main(String args[]) { System.out.println("MD Faisal Rahman"); S
System.out.println(s.area());

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;

abstract class ThreeDObject {


abstract double wholeSurfaceArea();

abstract double volume();


};

class Box extends ThreeDObject {


private double l, b, h;

Box(double l, double w, double h) {


this.l = l;
this.b =
w; this.h
= h;
}

double wholeSurfaceArea() {
return 2 * (l * b + b * h + h * l);
}

double volume() {
return l * b * h;
}
}

class Cube extends Box {


Cube(double a) {
super(a, a, a);
}
}

class Cylinder extends ThreeDObject {


private double r, 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;
}
}

class Cone extends ThreeDObject


{ private double 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;
}
}

public class lab5q2 {


public static void main(String args[])
{ Box b1 = new Box(2, 3, 5);
System.out.println("MD Faisal
Rahman");
System.out.println(b1.wholeSurfaceArea())
; System.out.println(b1.volume());
System.out.println();
Cube c1 = new Cube(3);
System.out.println(c1.wholeSurfaceArea())
; System.out.println(c1.volume());
System.out.println();
Cylinder cy1 = new Cylinder(2, 3);
System.out.println(cy1.wholeSurfaceArea())
;
System.out.println(cy1.volume());
System.out.println();
Cone co1 = new Cone(2, 3);
System.out.println(co1.wholeSurfaceArea());
System.out.println(co1.volume());
System.out.println();
}
}

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;

abstract class Vehicle {


protected int regno, speed, color;
protected String ownerName;

Vehicle(int regno, int speed, int color, String ownerName) {


this.regno = regno;
this.speed =
speed; this.color
= color;
this.ownerName = ownerName;
}

public void showData() {


System.out.println("This is a vehicle class");
}
}

class Bus extends Vehicle {


private int routeNumber;

Bus(int regno, int speed, int color, String ownerName,


int routeNumber) {
super(regno, speed, color,
ownerName); this.routeNumber =
routeNumber;
}

public void showData()


{ super.showData();
System.out.println("This is bus class");
System.out.println("regno is " + regno);
System.out.println("speed is " + speed);
System.out.println("color is " + color);
System.out.println("ownerName is " + ownerName);
System.out.println("routeNumber is " + routeNumber);
}

class Truck extends Vehicle {


private String
manufacturerName;

Truck(int regno, int speed, int color, String ownerName,


String manufacturerName) {
super(regno, speed, color, ownerName);
this.manufacturerName = manufacturerName;
}

public void showData() {


super.showData();
System.out.println("MD
Faisal Rahman");
System.out.println("This is truck
class"); System.out.println("regno is " +
regno);
System.out.println("speed is " + speed);
System.out.println("color is " + color);
System.out.println("ownerName is " + ownerName);
System.out.println("manufacturer name is " + manufacturerName);
}
}

public class lab5q3 {


public static void main(String args[]) {
Bus b1 = new Bus(1, 50, 2, "chintu", 10);
Bus b2 = new Bus(3, 100, 5, "ankit", 11);
Truck t1 = new Truck(2, 30, 3, "arvindh", "mahindra ");
b1.showData();
t1.showData();
b2.showData();
}
}

Output
q4.Create an abstract class MotorVehicle with the following details:
a. Data Members:

modelName (b)modelNumber (c) modelPrice

b. Methods:

display() to show all the details

Create a subclass of this class Carthat inherits the class MotorVehicle


and add the following details:
Data Members:
discountRa
te
Methods:

(a) display() method to display the Car name, model number,


price and the discount rate.
(b) discount() method to compute the discount.

Code
import java.util.Scanner;

abstract class MotorVehicle {


protected int modelNumber,
modelPrice; protected String
modelName;

MotorVehicle(int modelNumber, int modelPrice, String modelName)


{ this.modelNumber = modelNumber;
this.modelPrice =
modelPrice; this.modelName =
modelName;
}

abstract void display();


}

class CartHat extends MotorVehicle


{ private int discountRate;

CartHat(int modelNumber, int modelPrice, String modelName,


int discountRate) {
super(modelNumber, modelPrice,
modelName); this.discountRate =
discountRate;
}

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);
}
}

public class lab5q4 {


public static void main(String args[]) {
CartHat c = new CartHat(1, 50000, "thar",
50); System.out.println(c.discount());
c.display();
}
}

Output

Q5
Implement the below Diagram. Here AbstractProduct is only abstract class.
import java.util.Scanner;

abstract class AbstractProduct {


protected int productId;
protected String name, description;

AbstractProduct(int productId, String name, String description) {


this.productId = productId;
this.name = name;
this.description = description;
}
}

class Product extends AbstractProduct {


private int price;

Product(int productId, String name, String description, int price)


{
super(productId, name, description);
this.price = price;
}
}

class Book extends Product {


private String isbn, author, title;
Book(int productId, String name, String description, int
price, String isbn, String author, String title) {
super(productId, name, description,
price); this.isbn = isbn;
this.author =
author; this.title =
title;
}
}

class CompactDisk extends Product


{ private String artist,
title;

CompactDisk(int productId, String name, String description,


int price, String artist, String title) {
super(productId, name, description,
price); this.artist = artist;
this.title = title;
}
}

class TravelGuide extends Book


{ private String country;

TravelGuide(int productId, String name, String description,


int price, String isbn, String author, String title,
String country) {
super(productId, name, description, price, isbn, author,
title);
this.country = country;
}
}

public class lab5q5 {


public static void main(String args[]) {
TravelGuide t = new TravelGuide(1, "mmt11", "shimla",
100, "A003", "ruskin bond", "hello", "india");
CompactDisk c = new CompactDisk(2, "cd2", "harry potter",
1000, "jk rowling", "goblet of fire");
System.out.println("MD Faisal Rahman");
System.out.println("all classes successfully completed");
}
}
Output

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

public class Audi {

public static final String BRAND = "AUDI";

private String model;


private int year;

public Audi(String model, int year) {


this.model = model;
this.year = year;
}

public void printCarDetails() {


System.out.println("chintu kumar");
System.out.println("Brand: " + BRAND);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}

public static void main(String[] args) {


Audi audi1 = new Audi("A3", 2022);
Audi audi2 = new Audi("Q5", 2023);
audi1.printCarDetails();
audi2.printCarDetails();
}
}

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 {

public final String engineNumber;

private String model;


private int year;

public Audi(String model, int year, String engineNumber) {


this.model = model;
this.year = year;
this.engineNumber = engineNumber;
}

public void printCarDetails() {


System.out.println("MD Faisal Rahman");
System.out.println("Model: " + model);
System.out.println("Year: " + year);
System.out.println("Engine Number: " + engineNumber);
}

public static void main(String[] args) {


Audi audi1 = new Audi("A3", 2022, "ABC123");
Audi audi2 = new Audi("Q5", 2023, "XYZ789");

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.");
}
}

class Child extends Parent {


void someOtherMethod() {
System.out.println("This is a regular method in the Child
class.");
}
}

public class lab5q7 {


public static void main(String[] args) {
Parent parent = new Parent();
parent.displayMessage();

Child child = new


Child();
child.displayMessage();
child.someOtherMethod();
}
}
Output

Final class
final class FinalClass {

public class lab5q7 {


public static void main(String[] args) {
System.out.println("chintu kumar");
FinalClass finalObj = new FinalClass();
}
}

Output

You might also like