當前位置:首頁 » 編程語言 » 矩形java

矩形java

發布時間: 2023-06-07 16:07:24

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程序,描寫一個矩形類,並輸出某個矩形的長,寬,面積。具體描述如下

// 矩形
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()重新設置矩形寬

❸ Java 編寫一個矩形類Rect

public class Rect {
private double length;//矩形的長
private double width;//矩形的寬

public Rect() {}//默認構造器

public Rect(double length,double width) {
this.length = length;
this.width = width;
}

/**
* 取得矩形的面積
* */
public double getArea(){
return this.getLength() * this.getWidth();
}

/**
* 取得矩形的周長
* */
public double getPerimeter(){
return (this.getLength() + this.getWidth()) * 2;
}

/**
* 取得矩形的面積,需傳入矩形長與寬
* */
public double getArea(double length,double width){
return length * width;
}

/**
* 取得矩形的周長,需傳入矩形長與寬
* */
public double getPerimeter(double length,double width){
return (length + width) * 2;
}

public double getLength() {
return length;
}

public void setLength(double length) {
this.length = length;
}

public double getWidth() {
return width;
}

public void setWidth(double width) {
this.width = width;
}
}

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

熱點內容
QRM演算法 發布:2025-02-14 02:45:19 瀏覽:265
c語言列印結構體 發布:2025-02-14 02:42:28 瀏覽:140
編譯技術實驗一 發布:2025-02-14 02:28:24 瀏覽:647
編程手機入門 發布:2025-02-14 02:27:40 瀏覽:733
區域網視頻android 發布:2025-02-14 02:23:56 瀏覽:423
麒麟系統如何安裝安卓程序 發布:2025-02-14 02:07:21 瀏覽:399
ipad訪問電腦硬碟嗎 發布:2025-02-14 02:02:53 瀏覽:901
蘋果筆記本電腦不能連接伺服器 發布:2025-02-14 01:43:02 瀏覽:394
查看linux的shell 發布:2025-02-14 01:38:42 瀏覽:989
用於打開ftp連接的應用程序 發布:2025-02-14 01:23:39 瀏覽:707