當前位置:首頁 » 編程語言 » 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-01-16 07:41:14 瀏覽:193
寶駿解壓流程 發布:2025-01-16 07:35:35 瀏覽:2
兩匹壓縮機多少錢 發布:2025-01-16 07:29:19 瀏覽:635
個人pc搭建游戲伺服器 發布:2025-01-16 07:27:09 瀏覽:970
存儲剩餘照片 發布:2025-01-16 07:25:01 瀏覽:50
ftp解除限制上傳文件個數 發布:2025-01-16 07:16:26 瀏覽:347
梯度下降法python 發布:2025-01-16 07:10:43 瀏覽:520
載入並編譯著色器apex 發布:2025-01-16 07:00:08 瀏覽:59
方舟出售腳本 發布:2025-01-16 06:57:55 瀏覽:955
釘釘代理伺服器Ip地址和瑞口 發布:2025-01-16 06:57:05 瀏覽:699