“Java”编程中如何求矩形的面积?

“Java”编程中如何求矩形的面积?,第1张

代码如下:

import java.util.*

public class Rectangle {

private float length//定义长变量

private float width// 宽变量

public Rectangle(float length,float width){

this.length=length

this.width=width

}

public float getGirth(){

return (length+width)*2

} //求周长方法

public float getArea(){

return length*width

} //求面积方法

public static void main (String[] args) {

Scanner in=new Scanner(System.in)//调用输入方法

System.out.println ("请输入矩形的长:")

float a=in.nextFloat()

System.out.println ("请输入矩形的宽:")

float b=in.nextFloat()

System.out.println ("矩形周长为:"衡做+new Rectangle(a,b).getGirth())

System.out.println ("矩形面积兄拦升为:"+new Rectangle(a,b).getArea())

}

}

Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言。

Java 技术具有卓越的通用性、高效性、平台移植性和安全羡老性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

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())

}

}

// 矩形

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()重新设置矩形宽衡丛


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存