Write an interface Shape with methods getShapeName and getPerimeter. Write classes called Square, Rectangle, Circle and Triangle that implement the interface. Supply the proper parameters to the Square, Rectangle, Triangle and Circle constructors such that the perimeter can be calculated. Run THRou JDK compiler please
Sample code:
//Save this in Shape.java public interface Shape{ public double getPerimeter(); public String getShapeName(); } //Save this in Triangle.java public class Triangle implements Shape{ private double side1; private double side2; Triangle(double length1, double length2, double length3 ){ side1 = length1; side2 = length2; side3 = length3; } public String getShapeName(){ return "Triangle"; } public double getPerimeter(){ return side1 + side2 + side3; } } // Save this in Main.java public class Main{ public static void main(String[] args){ Shape shape = new Triangle(3, 4, 5); System.out.println(String.valueOf(shape.getPerimeter())); System.out.println(shape.getShapeName()); // Now do the same thing for the Square, Rectangle and Circle classes. // Shape circle = new Circle(PARAMS); // System.out.println(String.valueOf(circle.getPerimeter()); // System.out.println(circle.getShapeName()); // Shape square = new Circle(PARAMS); // System.out.println(String.valueOf(square.getPerimeter()); // System.out.println(square.getShapeName()); // Shape rectangle = new Circle(PARAMS); // System.out.println(String.valueOf(rectangle.getPerimeter()); // System.out.println(rectangle.getShapeName()); } }
Below is your code: -
Main.java
public class Main {
public static void main(String[] args) {
Shape shape = new Triangle(3, 4,
5);
System.out.println(String.valueOf(shape.getPerimeter()));
System.out.println(shape.getShapeName());
Shape circle = new
Circle(3);
System.out.println(String.valueOf(circle.getPerimeter()));
System.out.println(circle.getShapeName());
Shape square = new
Square(4);
System.out.println(String.valueOf(square.getPerimeter()));
System.out.println(square.getShapeName());
Shape rectangle = new
Rectangle(4, 5);
System.out.println(String.valueOf(rectangle.getPerimeter()));
System.out.println(rectangle.getShapeName());
}
}
Triangle.java
public class Triangle implements Shape{
private double side1;
private double side2;
private double side3;
Triangle(double length1, double length2, double length3 ){
side1 = length1;
side2 = length2;
side3 = length3;
}
public String getShapeName(){
return "Triangle";
}
public double getPerimeter(){
return side1 + side2 + side3;
}
}
Shape.java
public interface Shape{
public double getPerimeter();
public String getShapeName();
}
Circle.java
public class Circle implements Shape {
private double radius;
Circle(double r) {
this.radius = r;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public String getShapeName() {
return "Circle";
}
}
Square.java
public class Square implements Shape {
private double side;
Square(double s) {
this.side = s;
}
public double getPerimeter() {
return 4 * side;
}
public String getShapeName() {
return "Square";
}
}
Rectangle.java
public class Rectangle implements Shape {
private double length;
private double breadth;
Rectangle(double l, double b) {
this.length = l;
this.breadth = b;
}
public double getPerimeter() {
return 2 * (this.length +
this.breadth);
}
public String getShapeName() {
return "Rectangle";
}
}
Output
12.0
Triangle
18.84955592153876
Circle
16.0
Square
18.0
Rectangle
Get Answers For Free
Most questions answered within 1 hours.