编写一个JAVA程序,描写一个矩形类,并输出某个矩形的长,宽,面积。具体描述如下?

编写一个JAVA程序,描写一个矩形类,并输出某个矩形的长,宽,面积。具体描述如下?,第1张

// 矩形

public class RectangleDemo {

public static void main(String[] args) {

RectangleDemo demo = new RectangleDemo(12, 32)

System.out.println(demo.getPerimeter())

System.out.println(demo.getArea())

demo = new RectangleDemo()

System.out.println(demo.getArea())

System.out.println(demo.getPerimeter())

demo.setHeight(50)

demo.setWidth(30)

System.out.println(demo.getArea())

System.out.println(demo.getPerimeter())

}

// 求周

public double getPerimeter() {

return (height + width) * 2

}

// 求面积

public double getArea() {

return height * width

}

public RectangleDemo(double height, double width) {

this.height = height

this.width = width

}

public RectangleDemo() {

this.height = 10

this.width = 10

}

private double height// 高度

private double width// 宽度

public double getHeight() {

return height

}

public void setHeight(double height) {

this.height = height

}

public double getWidth() {

return width

}

public void setWidth(double width) {

this.width = width

}

}

编写矩形类RectangleJava程序矩形类两数据员别rLength宽rWidth通getLength()、getWidth()、getArea()别查看矩形、宽面积通setLength()setWidth()重新设置矩形宽

class Rectangle{

private int width = 2

private int length = 1

public int getWidth(){

return this.width

}

public void setWidth(int w){

this.width = w

}

public int getLength(){

return this.length

}

public void setLength(int l){

this.length = l

}

public int getArea(){

return this.length * this.width

}

public int getCircumference(){

return (this.length + this.width) * 2

}

public Rectangle(){}

public Rectangle(int l, int w){

this.length = l

this.width = w

}

}

public class demo{

public static void main(String[] args) {

Rectangle rect = new Rectangle(30, 8)

System.out.print("长方形的面积是:")

System.out.println(rect.getArea())

System.out.printf("长方形的周长是:%d\n", rect.getCircumference())

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8009458.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存