Home > Back-end >  A programming problem below:
A programming problem below:

Time:10-06

Defining Point, Circle and Cylinder three classes, class has three x, y attributes, and Circle increased attribute radius and the method of quadrature area (), Cylinder type and way to increase the height and volume volume (), write application respectively create three classes of objects, and each kind of corresponding test methods,

CodePudding user response:

 
/* *
* @ author: heiye
* @ date: 2020-03-20 what
* */
Public abstract class Point {
Double x, y;

Public abstract double volume ();

Public abstract double area ();

/* *
* test
*/
Public static void main (String [] args) {
Circle Circle=new Circle ();
Circle. SetRadius (2);
Cylinder Cylinder=new Cylinder (circle);
Cylinder. SetHeight (3);
System. The out. Println (" round volume: "+ circle. Volume ());
System. The out. Println (" is the area of a circle: "+ circle. Area ());
System. The out. Println (" is the volume of a cylinder: "+ cylinder. Volume ());
}
}

/* *
* for the area of a circle, the volume
*/
The class Circle extends Point {
Private double radius;

/* *
* size
* multiplied by 100 divided by 100 in order to keep a few decimal places, is not recommended, can cause loss of accuracy
* @ return
*/
@ Override
Public double volume () {
Return (double) ((int) (Math. PI * Math. Pow four thirds (radius, 3) * * 100))/100;
}

/* *

* area*
* @ return
*/
@ Override
Public double area () {
Return (double) ((int) (Math. PI * Math. Pow (radius, 2) * 100))/100;
}

Public void setRadius (double radius) {
Enclosing the radius=radius;
}
}

/* *
* for the area of the cylinder, the volume
*/
The class Cylinder extends Point {

Private double height;

Private Circle Circle;

/* *

* into the circle* @ param circle
*/
Public Cylinder (Circle Circle) {
Enclosing circle=circle;
}

/* *
* size
* @ return
*/
@ Override
Public double volume () {
The return circle area () * height;
}

@ Override
Public double area () {
return 0;
}

Public void setHeight (double height) {
This. Height=height;
}
}

  • Related