0% found this document useful (0 votes)
22 views5 pages

Experiment 11 24-25

The document outlines a Java laboratory experiment focused on implementing abstract classes and packages. It includes code for calculating the area of different shapes (circle, triangle, rectangle) using an abstract class and also demonstrates how to create a package for calculating the volume of a cylinder. The provided code snippets illustrate the implementation of these concepts in Java.

Uploaded by

Mohammad Shaikh
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)
22 views5 pages

Experiment 11 24-25

The document outlines a Java laboratory experiment focused on implementing abstract classes and packages. It includes code for calculating the area of different shapes (circle, triangle, rectangle) using an abstract class and also demonstrates how to create a package for calculating the volume of a cylinder. The provided code snippets illustrate the implementation of these concepts in Java.

Uploaded by

Mohammad Shaikh
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/ 5

Object Oriented Programming using Java Laboratory (DJS23FLES201)

Academic Year 2024-25

Name: mohammad shaikh

SAPID: 60003240302

RollNo: I169

Java Experiment 11

11. To implement Abstract classes and packages (CO1)


a. Write an abstract class program to calculate area of circle, rectangle and triangle.

Code:

import java.util.*;

abstract class Shapes


{ static final float pi =
3.14f; abstract void area();

class Circle extends Shapes {


int r; float
area;
Circle(int a) {
r = a;

void area()
{ area = pi * r *
r;

System.out.println("\nArea of Circle = " + area);


}
}

class Triangle extends Shapes {


Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

int b, h;
double area;

Triangle(int p, int q) {
b = p;
h = q;
}

void area()
{ area = 0.5 * b *
h;

System.out.println("Area of Triangle = " + area);


}
}

class Rectangle extends Shapes {


int l, b; float area;
Rectangle(int p, int q) {
l = p; b = q;

void area() {
area = l * b;

System.out.println("Area of Rectangle = " + area);


}
}

public class All {


public static void main(String[] args)
{ Scanner sc = new Scanner(System.in);

int r, l, b, h, bh;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

System.out.println("Enter Radius of Circle :- ");


r = sc.nextInt();

System.out.println("Enter Base of Triangle :- ");


bh = sc.nextInt();

System.out.println("Enter Height of Triangle :- ");


h = sc.nextInt();
System.out.println("Enter Length of Rectangle :- ");
l = sc.nextInt();

System.out.println("Enter Breadth of Rectangle :- ");


b = sc.nextInt();

Circle cr = new Circle(r);


cr.area();

Triangle tr = new Triangle(bh, h);


tr.area();

Rectangle rt = new Rectangle(l, b);


rt.area();

}}

Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

b. WAP to create a package called vol having Cylinder class and volume (). WAP that
imports this package to calculate volume of a Cylinder.
Code: package
vol;

public class Cylinder { public double


volume(double rad, double height) { double
vol_cylinder; double PI = 3.14;
vol_cylinder = PI * rad * rad * height;

return vol_cylinder;
}}

// Volume Calculation :

import vol.Cylinder; import


java.util.*;
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

public class Vol { public static void


main(String args[]) {

double radius, volumeOfCylinder;


double height;

Scanner sc = new Scanner(System.in);

System.out.println("Enter radius of cylinder:");


radius = sc.nextDouble();

System.out.println("Enter height of cylinder:");


height = sc.nextDouble();

Cylinder c = new Cylinder();


volumeOfCylinder = c.volume(radius, height);

System.out.println("Volume of cylinder=" + volumeOfCylinder);


}
}
Output:

You might also like