「java算矩形」java矩形类的设计

博主:adminadmin 2023-01-08 23:42:07 886

今天给各位分享java算矩形的知识,其中也会对java矩形类的设计进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java编程求矩形的面积

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计算一个矩形的面积?

//计算矩形的面积

public class RectArea {

public static double getArea(double width, double higth) {

double area = 0.0;// 矩形面积

// 判断输入是否合理

if (!(width = 0 || higth = 0)) {

area = width * higth;

return area;// 返回面积

} else {

System.out.println("请输入合理的长宽");

return -1;

}

}

public static void main(String[] args) {

//测试 宽:10.0 高:20.0

System.out.println("矩形面积" + RectArea.getArea(10.0, 20.0));

}

}

Java编写一个矩形类,并计算面积和周长?

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

}

}

java算矩形的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java矩形类的设计、java算矩形的信息别忘了在本站进行查找喔。