小弟只懂得C++和少量汇编,对Java不了解.受人之托 请教高人定义一个矩形类(Rectangle),类包括width,height和color三种属性,其中color的缺省颜色为白色。建立一个无参数构造器建立缺省的矩形,一个有参数构造器(传入width和height参数),编写与三个属性相关的get和set方法,和getArea和getPerimeter方法分别求面积和周长。
最后编写main方法建立矩形类对象,调用对象的方法计算面积和周长,并打印出来.

解决方案 »

  1.   

    ... ...class Rectangle {
        private int width = 0;
        private int height = 0;
        private int color = WRITE;    Rectangle() {
        }
        Rectangle(int width, int height) {
             this.width = width;
             this.height = height;
        }
        
        int getWidth() { return width; }
        void setWidth(int width) { this.width = width; }    int getHeight() { return height; }
        void setHeight(int height) { this.height = height; }    int getColor() { return color; }
        void setColor(int color) { this.color = color; }    
    }
      

  2.   

    private int color = WRITE; 
    应该修改为:
    private Color color = Color.WHITE;
      

  3.   

    行啊,都一样,反正Javaer不太了解颜色的构造,那就Color吧。
      

  4.   

    import java.awt.*;class Rectangle {
        private int width = 0;
        private int height = 0;
        private Color color = Color.WHITE;    Rectangle() {    }    Rectangle(int width, int height) {
             this.width = width;
             this.height = height;
        }    int getWidth() { return width; }
        void setWidth(int width) { this.width = width; }    int getHeight() { return height; }
        void setHeight(int height) { this.height = height; }    Color getColor() { return color; }
        void setColor(Color color) { this.color = color; }}
    这是全的,呵呵。根据上边的两个大哥说的该的。希望你能用上。
      

  5.   

    用double不用int 似乎更全面些
      

  6.   

    再补充一点,请指教
    double getArea()
    {
        return this.width*this.height;
    }
    double getPerimeter()
    {
        return (this.width+this.height)*2;
    }
    void print()
    {
        System.out.println("面积是:"+this.getArea()+",周长是:"+this.getPerimeter())
    }
    public static void main(String args[])
    {
        Rectangle rec1=new Rectangle();
        Rectangle rec2=new Rectangle(2.5,3);
        rec1.print();
        rec2.print();}
      

  7.   


    class Rectangle
    {
         private int height,width;
         private Color color = Color.WHITE;
         Rectangle(int width, int height) 
         { 
             this.width = width; 
             this.height = height; 
         } 
         int getPerimeter()
         {
                return 2*(height+width);
         }
         int getArea()
         {
                return height*width;
         }
         //照搬楼上的(不好意思)
          int getWidth() { return width; } 
         void setWidth(int width) { this.width = width; }      int getHeight() { return height; } 
         void setHeight(int height) { this.height = height; }      int getColor() { return color; } 
         void setColor(int color) { this.color = color; }     public static void main(String[] args)
         {
                Rectangle rect1=new Rectangle(30,40); //以宽30,长40为例              System.out.println("perimeter of Rectangle="+rect1.getPerimeter());
                System.out.println("area of Rectangle="+rect1.getArea());
         } 
    }
      

  8.   


    import java.util.Scanner;
    public class  Rectangle
    {
    private String color;
    private Double width  ;
    private Double height ; //无参缺省的矩形
        public Rectangle(){
    System.out.println("无参缺省的矩形");
     color = "WHITE";
     width = 0.00;
     height= 0.00;
    } //有参的矩形
        public Rectangle(Double w, Double h){
     color = "WHITE";
     width = w;
     height= h;
    }

    public String getColor(){
    return color;
    } public Double getWidth(){
    return width;
    } public Double getHeight(){
    return height;
    } public void setColor(String str){
    color = str ;
    } public void setWidth(Double str){
    width = str ;
    } public void setHeight(Double str){
    height = str ;
    } public Double getArea(){
    Double area ;
    area = width * height;
    return area;
    } public Double getPerimeter(){
    Double perimeter ;
    perimeter = (width + height)*2;
    return perimeter;
    }
    public static void main(String[] args) 
    {
    Rectangle rectange = new Rectangle();
    System.out.println("新建一个无参Rectangle");
    System.out.println("the Area is :"+rectange.getArea());
    System.out.println("the Perimeter is :"+rectange.getPerimeter()); Double a, b;
    System.out.println("请输入矩形的宽和高:");
    Scanner sc = new Scanner(System.in);
    a = sc.nextDouble();
    b = sc.nextDouble();
    System.out.println("a:"+a+" b:"+b);
    Rectangle rectange2 = new Rectangle(a,b);
    System.out.println("新建一个有参Rectangle");
    System.out.println("the Area is :"+rectange2.getArea());
    System.out.println("the Perimeter is :"+rectange2.getPerimeter());
    }
    }