mirror of
https://github.com/Manoj-HV30/OOPS-lab-codes.git
synced 2026-05-16 19:35:25 +00:00
Added Java lab programs
This commit is contained in:
+107
@@ -0,0 +1,107 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
// -------- Interface for Circle --------
|
||||
interface CircleOps {
|
||||
double circleArea();
|
||||
double circlePerimeter();
|
||||
}
|
||||
|
||||
// -------- Interface for Rectangle --------
|
||||
interface RectangleOps {
|
||||
double rectangleArea();
|
||||
double rectanglePerimeter();
|
||||
}
|
||||
|
||||
// -------- Interface inheriting both --------
|
||||
interface ShapeOps extends CircleOps, RectangleOps {
|
||||
void displaySummary();
|
||||
}
|
||||
|
||||
// -------- Base class --------
|
||||
class ShapeBase {
|
||||
|
||||
double radius;
|
||||
double length;
|
||||
double breadth;
|
||||
}
|
||||
|
||||
// -------- Implementing class --------
|
||||
class ShapeCalculator extends ShapeBase implements ShapeOps {
|
||||
|
||||
ShapeCalculator(double radius, double length, double breadth) {
|
||||
this.radius = radius;
|
||||
this.length = length;
|
||||
this.breadth = breadth;
|
||||
}
|
||||
|
||||
// Circle operations
|
||||
public double circleArea() {
|
||||
return Math.PI * radius * radius;
|
||||
}
|
||||
|
||||
public double circlePerimeter() {
|
||||
return 2 * Math.PI * radius;
|
||||
}
|
||||
|
||||
// Rectangle operations
|
||||
public double rectangleArea() {
|
||||
return length * breadth;
|
||||
}
|
||||
|
||||
public double rectanglePerimeter() {
|
||||
return 2 * (length + breadth);
|
||||
}
|
||||
|
||||
// Summary method
|
||||
public void displaySummary() {
|
||||
System.out.println("\n--- Shape Summary ---");
|
||||
System.out.println("Circle Area: " + circleArea());
|
||||
System.out.println("Circle Perimeter: " + circlePerimeter());
|
||||
System.out.println("Rectangle Area: " + rectangleArea());
|
||||
System.out.println("Rectangle Perimeter: " + rectanglePerimeter());
|
||||
}
|
||||
}
|
||||
|
||||
// -------- Demo class --------
|
||||
public class ShapesDemo {
|
||||
|
||||
// Method accepting CircleOps
|
||||
static void circleOperations(CircleOps c) {
|
||||
System.out.println("\nCircle Area: " + c.circleArea());
|
||||
System.out.println("Circle Perimeter: " + c.circlePerimeter());
|
||||
}
|
||||
|
||||
// Method accepting RectangleOps
|
||||
static void rectangleOperations(RectangleOps r) {
|
||||
System.out.println("\nRectangle Area: " + r.rectangleArea());
|
||||
System.out.println("Rectangle Perimeter: " + r.rectanglePerimeter());
|
||||
}
|
||||
|
||||
// Method accepting ShapeOps
|
||||
static void shapeOperations(ShapeOps s) {
|
||||
s.displaySummary();
|
||||
}
|
||||
|
||||
// -------- Main method --------
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
System.out.print("Enter radius of circle: ");
|
||||
double radius = sc.nextDouble();
|
||||
|
||||
System.out.print("Enter length of rectangle: ");
|
||||
double length = sc.nextDouble();
|
||||
|
||||
System.out.print("Enter breadth of rectangle: ");
|
||||
double breadth = sc.nextDouble();
|
||||
|
||||
ShapeCalculator shape = new ShapeCalculator(radius, length, breadth);
|
||||
|
||||
// Polymorphism using interface references
|
||||
circleOperations(shape);
|
||||
rectangleOperations(shape);
|
||||
shapeOperations(shape);
|
||||
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user