「java设计一个长方形类」编写长方形类及其子类长方体java

博主:adminadmin 2022-12-22 20:03:07 48

本篇文章给大家谈谈java设计一个长方形类,以及编写长方形类及其子类长方体java对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

JAVA创建一个长方形的类 急!!!在线等!!!学号是37!

import java.util.Scanner;

public class Demo_137 {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

System.out.println("请输入长和宽:");

int length=input.nextInt();

int wide=input.nextInt();

if(length==137) {

Rectangle rec=new Rectangle(length, wide);

System.out.println("周长:"+rec.perimeter());

System.out.println("面积:"+rec.area());

System.out.println("构造长方形个数:"+rec.getCount());

}

}

}

class Rectangle{

private int length,wide,count=0;

public Rectangle(int length,int wide) {

this.length=length;

this.wide=wide;

count++;

}

public int perimeter() {

int per=(length+wide)*2;

return per;

}

public int area() {

int are=length*wide;

return are;

}

public int getCount() {

return count;

}

}

用java设计一个长方形类,成员变量包括长和宽.方法:计算面积和周长,有相应的set和get方法

/**

* 长方形类

*/

class Rectangle{

/**

* 宽

*/

private double width;

/**

* 高

*/

private double height;

/**

* 构造方法

* @param width 宽

* @param height 高

*/

public Rectangle(double width, double height){

this.width = width;

this.height = height;

}

/**

* 计算长方形的面积

* @return 返回长方形的面积

*/

public double getArea(){

return this.width * this.height;

}

/**

* 计算长方形的周长

* @return 返回长方形的周长

*/

public double getGirth(){

return 2 * (this.width + this.height);

}

public double getWidth(){

return this.width;

}

public void setWidth(double width){

this.width = width;

}

public double getHeight(){

return this.height;

}

public void setHeight(double height){

this.height = height;

}

}

public class Test {

public static void main(String[] args){

Rectangle rect = new Rectangle(2,3);

System.out.println("长方形的宽 :" + rect.getWidth());

System.out.println("长方形的高 :" + rect.getHeight());

System.out.println("长方形的面积:" + rect.getArea());

System.out.println("长方形的周长:" + rect.getGirth());

}

}

Java定义长方形类Rect ,包含三个属性长len ,宽width,高height和一个计算体积?

public class Rect {

private double len;

private double width;

private double height;

//体积

public double volume () {

return this.len * this.width * this.height;

}

public double getLen() {

return len;

}

public void setLen(double len) {

this.len = len;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

}

class RectTest {

public static void main(String[] args) {

Rect rect = new Rect();

rect.setLen(5);

rect.setWidth(7);

rect.setHeight(9);

double volume = rect.volume();

System.out.println("体积为 = " + volume);

}

}

java编写一个长方体类,通过类中的一个成员方法来初始化类中成员变量并通过另外一个方法来计算长方体体积

public class BoxTest

{

    public static void main(String[] args)

    {

        Box b = new Box();

        

        b.setBox(5, 2, 8);

        

        System.out.println(b.volume());

    }

}

class Box

{

    private int length;

    private int width;

    private int heigth;

    

    //初始化长方体的长宽高

    public void setBox(int l, int w, int h)

    {

        this.length = l;

        this.width = w;

        this.heigth = h;

    }

    

    //用于计算长方体的体积,并返回

    public int volume()

    {

        return this.length * this.width * this.heigth;

    }

    

    

}

望采纳!

关于java设计一个长方形类和编写长方形类及其子类长方体java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。