Design a cuboid class Cuboid (Java)

Design a cuboid class Cuboid (10 points)
Requirements: Design a class named Cuboid to represent a cuboid. This class includes three double data fields named length, width, and height, which represent the length, width, and height of the cuboid, respectively. A no-argument construction method, the default values ​​of length, width, and height are all 1. A construction method for specifying values ​​for length, width, and height. A method called getArea() returns the surface area of ​​this cuboid. A method called getVolume() returns the volume of this cuboid.

Function interface definition:
public double getArea();
public double getVolume();

Sample referee test procedure:

import java.util.Scanner;
/* 你的代码将被嵌入到这里 */

public class Main {
    
    
  public static void main(String[] args) {
    
    
    Scanner input = new Scanner(System.in);

    double l = input.nextDouble();
    double w = input.nextDouble();
    double h = input.nextDouble();
    Cuboid myCuboid = new Cuboid(l, w, h);
    System.out.println(myCuboid.getArea());
    System.out.println(myCuboid.getVolume());

    input.close();
  }
}

Input example:
3.5 2 5

Sample output:
69.0
35.0

class Cuboid {
    
    
    double length=1;
    double width=1;
    double height=1;

    public  double getArea(){
    
    
        return (length*width+length*height+width*height)*2 ;
    }

    public double getVolume(){
    
    
        return length*width*height;
    }

    public Cuboid(){
    
    

    }

    public Cuboid (double length,double width,double height){
    
    
        this.length=length;
        this.width=width;
        this.height=height;
    }
}

Guess you like

Origin blog.csdn.net/weixin_51430516/article/details/115119685