Java --- Please help me understand further the code below by putting comments if possible in every line. Thank you! --- Circle.java public class Circle implements Shape { final float PI = 3.14f; int radius; public Circle(int radius) { this.radius = radius; } @Override public double getPerimeter() { return(2 * PI * radius); } @Override public double getArea() { return(PI * radius*radius); } @Override public void getDetails() { System.out.println("Type: Circle"); System.out.println("Radius: " + radius); System.out.printf("Perimeter: %.1f\n", getPerimeter()); System.out.printf("Area: %.1f\n", getArea()); } } Rectangle.java public class Rectangle implements Shape { int length, width; public Rectangle(int a, int b) { this.length = b; this.width = a; } @Override public double getPerimeter() { return(2 * (length + width)); } @Override public double getArea() { return(length * width); } @Override public void getDetails() { System.out.println("Type: Rectangle"); System.out.println("Lengh: "+ length); System.out.println("Width: "+ width); System.out.printf("Perimeter: %.1f\n", getPerimeter()); System.out.printf("Area: %.1f\n", getArea()); } } Square.java public class Square implements Shape { int side; public Square(int a) { this.side = a; } @Override public double getPerimeter() { return(4 * side); } @Override public double getArea() { return(side * side); } @Override public void getDetails() { System.out.println("Type: Square"); System.out.println("Side: " + side); System.out.printf("Perimeter: %.1f\n", getPerimeter()); System.out.printf("Area: %.1f\n", getArea()); } } Triangle.java public class Triangle implements Shape { int sideA, sideB, sideC; public Triangle(int a, int b, int c) { this.sideA = a; this.sideB = b; this.sideC = c; } @Override public double getPerimeter() { return(sideA + sideB + sideC); } @Override public double getArea() { float s = (sideA+sideB+sideC)/2; return(Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC))); } @Override public void getDetails() { System.out.println("Type: Triangle"); System.out.printf("Sides: %d, %d, %d \n", sideA, sideB, sideC); System.out.printf("Perimeter: %.1f\n", getPerimeter()); System.out.printf("Area: %.1f\n", getArea()); } } Shape.java public interface Shape { double getPerimeter(); double getArea(); void getDetails(); }
Java
---
Please help me understand further the code below by putting comments if possible in every line. Thank you!
---
Circle.java
public class Circle implements Shape
{
final float PI = 3.14f;
int radius;
public Circle(int radius)
{
this.radius = radius;
}
@Override
public double getPerimeter()
{
return(2 * PI * radius);
}
@Override
public double getArea()
{
return(PI * radius*radius);
}
@Override
public void getDetails()
{
System.out.println("Type: Circle");
System.out.println("Radius: " + radius);
System.out.printf("Perimeter: %.1f\n", getPerimeter());
System.out.printf("Area: %.1f\n", getArea());
}
}
Rectangle.java
public class Rectangle implements Shape
{
int length, width;
public Rectangle(int a, int b)
{
this.length = b;
this.width = a;
}
@Override
public double getPerimeter()
{
return(2 * (length + width));
}
@Override
public double getArea()
{
return(length * width);
}
@Override
public void getDetails()
{
System.out.println("Type: Rectangle");
System.out.println("Lengh: "+ length);
System.out.println("Width: "+ width);
System.out.printf("Perimeter: %.1f\n", getPerimeter());
System.out.printf("Area: %.1f\n", getArea());
}
}
Square.java
public class Square implements Shape
{
int side;
public Square(int a)
{
this.side = a;
}
@Override
public double getPerimeter()
{
return(4 * side);
}
@Override
public double getArea()
{
return(side * side);
}
@Override
public void getDetails()
{
System.out.println("Type: Square");
System.out.println("Side: " + side);
System.out.printf("Perimeter: %.1f\n", getPerimeter());
System.out.printf("Area: %.1f\n", getArea());
}
}
Triangle.java
public class Triangle implements Shape
{
int sideA, sideB, sideC;
public Triangle(int a, int b, int c)
{
this.sideA = a;
this.sideB = b;
this.sideC = c;
}
@Override
public double getPerimeter()
{
return(sideA + sideB + sideC); }
@Override
public double getArea()
{
float s = (sideA+sideB+sideC)/2;
return(Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC)));
}
@Override
public void getDetails()
{
System.out.println("Type: Triangle");
System.out.printf("Sides: %d, %d, %d \n", sideA, sideB, sideC);
System.out.printf("Perimeter: %.1f\n", getPerimeter());
System.out.printf("Area: %.1f\n", getArea());
}
}
Shape.java
public interface Shape
{
double getPerimeter();
double getArea();
void getDetails();
}
Step by step
Solved in 2 steps