The second in-class test of the Java class experiment (all possible questions)

1

Static variables can be shared by all instances of a class, so static variables can be used as shared data between instances to increase interactivity between instances.
If all instances of a class contain the same constant property, you can define this property as a static constant type to save memory space. For example, define a static constant PI in the class.
[Problem Description] Define three classes Circle, Cylinder and main class, pi = 3.14
Circle class:
attributes:
double radius;
double area;
methods are:
Circle (double radius), construction method of Circle class.
double getArea(), calculates the circle area.
Cylinder class:
Attributes:
Circle bottom;
double height;
Method:
Cylinder (Circle bottom, double height), the construction method of the Cylinder class.
double getVolme(), calculates the volume of the cylinder.
Main class:
Input circle radius and cylinder height, and output cylinder volume.
[Input form] Please enter the radius of the base circle. If the radius is less than 0, set the radius to 0.

                  请输入圆柱体的高。如果高小于0,就将高设为0。

[Output form] Find the volume of the cylinder.
[Sample input 1]
please input the radius of the circle
-2
The radius is not less than 0.
please input the height of the cylinder
3
[Sample input 1]
The volume is: 0.0
[Sample input 2]
please input the radius of the circle
5
please input the height of the cylinder
-3
The height is not less than 0.
[Sample output 2]
The volume is: 0.0
[Sample input 3]
please input the radius of the circle
2
please input the height of the cylinder
3

import java.util.Scanner;


class Circle{
   
    
    
    double radius;
    double area;
    Circle(double radius) {
   
    
    
        if (radius<0)
        {
   
    
    
            System.out.println("The radius is not less than 0.");
            this.radius=0;
        }
        else{
   
    
    
            this.radius=radius;
        }
    }
    double getArea(){
   
    
    
        area=3.14*radius*radius;
        return area;
    }
}
class Cylinder{
   
    
    
    Circle bottom;
    double height;
    Cylinder(Circle bottom, double height)
    {
   
    
    
        if (height<0)
        {
   
    
    
            System.out.println("The height is not less than 0.");
            this.height=0;
            this.bottom=bottom;
        }
        else{
   
    
    
            this.bottom=bottom;
            this.height=height;
        }
    }
    double getVolme(){
   
    
    
        return bottom.getArea()*height;
    }
}
public class yuanzhu {
   
    
    
    public static void main(String[] args) {
   
    
    
        Scanner in=new Scanner(System.in);
        System.out.println("please input the radius of the circle");
        double radius=in.nextDouble();
        Circle x=new Circle(radius);
        System.out.println("please input the height of the cylinder");
        double height=in.nextDouble();
        Cylinder y=new  Cylinder

Guess you like

Origin blog.csdn.net/X131644/article/details/124973221