当前位置:首页 » 编程语言 » java设计一个长方形类

java设计一个长方形类

发布时间: 2024-08-02 17:11:05

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

⑵ 镐庢牱鐢╦ava璁捐′竴涓鎴愬憳鍙橀噺鍖呮嫭闀垮拰瀹界殑闀挎柟褰㈢被骞惰$畻闱㈢Н鍜屽懆闀...

/**
* 闀挎柟褰㈢被
*/
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创建一个长方形的类 急!!!在线等!!!学号是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;
}

}

热点内容
油猴安装脚本 发布:2025-07-10 21:01:30 浏览:583
json跨域访问 发布:2025-07-10 20:51:37 浏览:870
架设测试服务器怎么做 发布:2025-07-10 20:47:32 浏览:412
lol服务器满载怎么办 发布:2025-07-10 20:31:08 浏览:328
sql2005脚本导出数据 发布:2025-07-10 20:31:05 浏览:112
三星手机服务器停止运行怎么办 发布:2025-07-10 20:21:07 浏览:868
华为手机原厂设置密码多少 发布:2025-07-10 20:19:54 浏览:242
如何重设服务器地址 发布:2025-07-10 20:19:53 浏览:568
bp神经网络算法c 发布:2025-07-10 20:00:00 浏览:700
sqlserver导出mdf 发布:2025-07-10 19:52:55 浏览:673