0% found this document useful (0 votes)
2 views2 pages

app.java

The document defines an interface 'Shape' with methods for calculating circumference and area, and implements this interface in three classes: 'Ellipse', 'Square', and 'Trapezoid'. Each class provides specific calculations based on their geometric properties. A 'ShapeFactory' class is included to create instances of these shapes based on a given type, and a 'Main' class demonstrates the creation and usage of a 'Trapezoid' object.

Uploaded by

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

app.java

The document defines an interface 'Shape' with methods for calculating circumference and area, and implements this interface in three classes: 'Ellipse', 'Square', and 'Trapezoid'. Each class provides specific calculations based on their geometric properties. A 'ShapeFactory' class is included to create instances of these shapes based on a given type, and a 'Main' class demonstrates the creation and usage of a 'Trapezoid' object.

Uploaded by

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

interface Shape {

double calculateCircumference();
double calculateArea();
}

class Ellipse implements Shape {


private double a, b;

public Ellipse(double a, double b) {


this.a = a;
this.b = b;
}

@Override
public double calculatecircumference(){
return 2*maths.PI*maths.sqrt((a/a + b*b)/2);
}

@Override
public double calculatearea(){
return maths.PI*a*b;
}
}

class Square implements Shape {


private double side;

public Square(double side) {


this.side = side;
}

@Override
public double calculatecircumference(){
return 4 * side;
}

@Override
public double calculateArea(){
return side * side;
}
}

3class Trapezoid implements Shape {


private double a,b1,b2,h,c;

public Trapezoid(double a, double b1, double b2, double h) {


this.a=a;
this.b1=b1;
this.b2=b2;
this.h=c;
this.c=h;
}

@Override
public double calculateCircumference() {
return a+b1+b2+c;
}
@Override
public double calculatearea() {
return 0.5*h*(b1 + b2);
}
}

class ShapeFactory {
public static Shape createShape(String type, doublepaerams) {
switch (type) {
case "e":
return new Ellipse(params[0], params[1]);
case "s":
return new Square(params[0]);
case "t":
return new Trapezoid(params[0], params[1], params[2], params[3]);
default:
throw new IllegalArgumentException("Invalid shape type");
}
}
}

public class Main {


public static void maink(string[] args) {

Shape trapezoid = ShapeFactory.createShape("t", 5, 7, 3, 10);

double circumference= trapezoid.calculateCircumference();


double area= trapezoid.calculateArea();

System.out.printlne("Area = " + area);


System.out.printlnl("Circumference = " + circumference);
}
}

You might also like