hello can you please help correct this, it is not running. i cant figure out what i did wrong // Shape class public abstract class Shape { Stringname; public Shape(Stringname) { this.name = name; } publicabstractdouble getArea(); publicabstractdouble getPerimeter(); publicString getName() { returnthis.name; } //Circle.java publicclass Circle extends Shape { doublediameter; public Circle(doublediameter) { super("Circle"); this.diameter = diameter; } @Override publicdouble getArea() { // A = π r^2 return Math.PI * Math.pow(diameter/2, 2); } @Override publicdouble getPerimeter() { // P = πd return Math.PI * diameter; } //Square.java publicclass Square extends Shape { doublelength; public Square(doublelength) { super("Square"); this.length = length; } @Override publicdouble getArea() { // A = l*l returnlength * length; } @Override publicdouble getPerimeter() { // P = 4(l) return 4 * length; } //Triangle.java publicclass Triangle extends Shape { doublelength; // side public Triangle(doublelength) { super("Triangle"); this.length = length; } @Override publicdouble getArea() { // Heron's formula: // A = (SquareRoot(3)/4) * side * side return (Math.sqrt(3)/4)*length*length; } @Override publicdouble getPerimeter() { // P = 3 * side return 3*length; } } import java.text.DecimalFormat; public class TestShapes { publicstaticvoid main(String[] args) { Shape[] shapes = new Shape[3]; shapes[0] = newCircle(10); shapes[1] = newSquare(5); shapes[2] = newTriangle(8); DecimalFormat dec = new DecimalFormat("#0.00"); for (inti = 0; i < shapes.length; i++) { System.out.println("The Shape's Name: "+ shapes[i].getName()); System.out.printf("The Shape's Area: "+ dec.format(shapes[i].getArea())+"\n"); System.out.println("The Shape's Perimeter: "+dec.format(shapes[i].getPerimeter())+"\n\n"); System.out.println(); } } }
hello can you please help correct this, it is not running. i cant figure out what i did wrong
// Shape class
public abstract class Shape {
Stringname;
public Shape(Stringname) {
this.name = name;
}
publicabstractdouble getArea();
publicabstractdouble getPerimeter();
publicString getName()
{
returnthis.name;
}
//Circle.java
publicclass Circle extends Shape {
doublediameter;
public Circle(doublediameter) {
super("Circle");
this.diameter = diameter;
}
@Override
publicdouble getArea() {
// A = π r^2
return Math.PI * Math.pow(diameter/2, 2);
}
@Override
publicdouble getPerimeter() {
// P = πd
return Math.PI * diameter;
}
//Square.java
publicclass Square extends Shape {
doublelength;
public Square(doublelength) {
super("Square");
this.length = length;
}
@Override
publicdouble getArea() {
// A = l*l
returnlength * length;
}
@Override
publicdouble getPerimeter() {
// P = 4(l)
return 4 * length;
}
//Triangle.java
publicclass Triangle extends Shape {
doublelength; // side
public Triangle(doublelength) {
super("Triangle");
this.length = length;
}
@Override
publicdouble getArea() {
// Heron's formula:
// A = (SquareRoot(3)/4) * side * side
return (Math.sqrt(3)/4)*length*length;
}
@Override
publicdouble getPerimeter() {
// P = 3 * side
return 3*length;
}
}
import java.text.DecimalFormat;
public class TestShapes {
publicstaticvoid main(String[] args) {
Shape[] shapes = new Shape[3];
shapes[0] = newCircle(10);
shapes[1] = newSquare(5);
shapes[2] = newTriangle(8);
DecimalFormat dec = new DecimalFormat("#0.00");
for (inti = 0; i < shapes.length; i++)
{
System.out.println("The Shape's Name: "+ shapes[i].getName());
System.out.printf("The Shape's Area: "+ dec.format(shapes[i].getArea())+"\n");
System.out.println("The Shape's Perimeter: "+dec.format(shapes[i].getPerimeter())+"\n\n");
System.out.println();
}
}
}

Step by step
Solved in 5 steps with 6 images









