JAVA設計問題2

題目如下:

class Rectangle

{

int lenght,width;

String unit;

?

?

?

public static void main(string args[])

{

Rectangle r1=new (Rectangle(12,10,\"公分\");

r1.Print();

r1.Area();

r1.Lenght();

Rectangle r2=new (Rectangle(5,6,\"公尺\");

r2.Print();

r2.Area();

r2.Lenght();

}

}

我要的結果:

長方形:12公分 x 10公分

長方形面積:120平方公分

長方形周長:44公分

長方形:5公尺 x6公尺

長方形面積:30平方公尺

長方形周長:22公尺

要怎做勒?

1 Answer

Rating
  • Samuel
    Lv 5
    1 decade ago
    Favorite Answer

    class Rectangle

    {

    int lenght,width;

    String unit;

    Rectangle(int lenght, int width, String unit) {

    this.lenght = lenght;

    this.width = width;

    this.unit = unit;

    }

    void Print() {

    System.out.println("長方形:" + lenght + unit + "X" + width + unit);

    }

    void Area() {

    System.out.println("長方形面積:" + lenght * width + "平方" + unit);

    }

    void Lenght() {

    System.out.println("長方形周長:" + (lenght + width) * 2 + unit);

    }

    public static void main(String args[])

    {

    Rectangle r1 = new Rectangle(12,10,"公分");

    r1.Print();

    r1.Area();

    r1.Lenght();

    Rectangle r2 = new Rectangle(5,6,"公尺");

    r2.Print();

    r2.Area();

    r2.Lenght();

    }

    }

    • Login to reply the answers
Still have questions? Get your answers by asking now.