Home > Back-end >  Which big save the children, can't figure out how to write
Which big save the children, can't figure out how to write

Time:09-17

(1) design a Shape class hierarchy, contains a getArea () method, this method does not contain the actual statement, (2) based on the Shape design of circle, rectangle, triangle and trapezoid four subclasses, request according to the actual Shape of rewriting getArea () method, (3) design a TestShape class that contains the variable area (storage area), the static method countArea (Shapes), the method for the parameters of Shape area in the area, in the main square method (2) new shape1, four types of object in shape2, shape3, shape4, by calling the countArea method put four object area of accumulation in the area, the output area,

How do you write this TestShape classes and the main function?

CodePudding user response:

 public class TestShape {
Public static void main (String [] args) {
Shape shape1=new Circle (10);
Shape shape2=new Rectangle (5, 10);
Shape shape3=new Triangle (1, 2, 3);//a, b, c
Shape shape4=new Trapezoid (3, 5, 4);//a, b, h

Double area=countArea (shape1 shape2, shape3, shape4);
System. The out. Println (area);
}

Private static double countArea (Shape... Shapes) {
Double area=0.0;
For (Shape Shape: shapes) area +=Shape. The getArea ();
The return area;
}
}

The abstract class Shape {
Public abstract double getArea ();
}

The class Circle extends Shape {
private double radius;

Public Circle radius, (double) {
Enclosing the radius=radius;
}

@ Override
Public double getArea () {
Return Math. Radius, PI * * radius;
}
}

Class a Rectangle extends Shape {
Private double length;
Private double width;

Public Rectangle (double length, double width) {
This. Length=length;
This. Width=width;
}

@ Override
Public double getArea () {
Return the length * width;
}
}

The class Triangle extends Shape {
Private double a;
Private double b;
Private double c;

Public Triangle (double a, b double, double c) {
Enclosing a=a;
This. B=b;
This. C=c;
}

@ Override
Public double getArea () {
Double p=(a + b + c)/2;
Return math.h SQRT (p * (p - a) * (p - b) * (p - c));
}
}

The class Trapezoid extends Shape {
Private double a;
Private double b;
Private double h;

Public Trapezoid (double a, b double, double h) {
Enclosing a=a;
This. B=b;
This. H=h;
}

@ Override
Public double getArea () {
Return (a + b) * h/2;
}
}

CodePudding user response:

reference 1st floor winner245 response:
 public class TestShape {
Public static void main (String [] args) {
Shape shape1=new Circle (10);
Shape shape2=new Rectangle (5, 10);
Shape shape3=new Triangle (1, 2, 3);//a, b, c
Shape shape4=new Trapezoid (3, 5, 4);//a, b, h

Double area=countArea (shape1 shape2, shape3, shape4);
System. The out. Println (area);
}

Private static double countArea (Shape... Shapes) {
Double area=0.0;
For (Shape Shape: shapes) area +=Shape. The getArea ();
The return area;
}
}

The abstract class Shape {
Public abstract double getArea ();
}

The class Circle extends Shape {
private double radius;

Public Circle radius, (double) {
Enclosing the radius=radius;
}

@ Override
Public double getArea () {
Return Math. Radius, PI * * radius;
}
}

Class a Rectangle extends Shape {
Private double length;
Private double width;

Public Rectangle (double length, double width) {
This. Length=length;
This. Width=width;
}

@ Override
Public double getArea () {
Return the length * width;
}
}

The class Triangle extends Shape {
Private double a;
Private double b;
Private double c;

Public Triangle (double a, b double, double c) {
Enclosing a=a;
This. B=b;
This. C=c;
}

@ Override
Public double getArea () {
Double p=(a + b + c)/2;
Return math.h SQRT (p * (p - a) * (p - b) * (p - c));
}
}

The class Trapezoid extends Shape {
Private double a;
Private double b;
Private double h;

Public Trapezoid (double a, b double, double h) {
Enclosing a=a;
This. B=b;
This. H=h;
}

@ Override
Public double getArea () {
Return (a + b) * h/2;
}
}

Crab crab, can ask for role under the code?

CodePudding user response:

reference 1st floor winner245 response:
 public class TestShape {
Public static void main (String [] args) {
Shape shape1=new Circle (10);
Shape shape2=new Rectangle (5, 10);
Shape shape3=new Triangle (1, 2, 3);//a, b, c
Shape shape4=new Trapezoid (3, 5, 4);//a, b, h

Double area=countArea (shape1 shape2, shape3, shape4);
System. The out. Println (area);
}

Private static double countArea (Shape... Shapes) {
Double area=0.0;
For (Shape Shape: shapes) area +=Shape. The getArea ();
The return area;
}
}

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related