小弟作业有道题不会做,请前辈学长们写一下,最好能编译通过并运行的,感激不尽啦!声明一个矩形类Rectangle,其中有多个构造方法。用不同的构造方法创建对象,并输出矩形的周长和面积。

解决方案 »

  1.   

    public class Rectangle{
       public Rectangle(int width,int height){
          System.out.println(width*height);
       }
       public Rectangle(int double,int height){
          System.out.println(width*height);
       }
       public Rectangle(int double,int double){
          System.out.println(width*height);
       }
    }
      

  2.   

    class Rectangle{
       private double long;
       private double length;   Rectangle(){
         this(0.0, 0.0);
     }   Rectangle(double long, double length){
         this.long=long;
         this.length=length;
     }  public void showPerimeter(){
         double perimeter;
         perimeter = 2*(long+length);
         System.out.println("周长为:"+ perimeter);
     }  public void showArea(){
         double area;
         area = long*length;
         System.out.println("周长为:"+ area);
     }}
      

  3.   

    public class Rectangle{
       public Rectangle(int width,int height){
      System.out.println("int,int");
          System.out.println(width*height);
       }
       public Rectangle(double width,int height){
      System.out.println("double,int");
          System.out.println(width*height);
       }
       public Rectangle(double width,double height){
      System.out.println("double,double");
          System.out.println(width*height);
       }   public static void main(String[] s){
           new Rectangle(10,10);
       new Rectangle(5.0,10);
           new Rectangle(2.0,2.0);   }
    }输出结果:
    int,int
    100
    double,int
    50.0
    double,double
    4.0
      

  4.   

    zjun前辈,你的程序能否解释下呢? 还有应该还要周长的说
      

  5.   

    package myTest;public class Test2
    { /**
     * @param args
     */
    public static void main(String[] args)
    {
    Rectangle r = new Rectangle();
    r.showPerimeter();
    r.showArea();
    Rectangle r1 = new Rectangle(3,5);
    r1.showPerimeter();
    r1.showArea();
    }}class Rectangle
    {
    private double length; private double width; Rectangle()
    {
    this(0.0, 0.0);
    } Rectangle(double length, double width)
    {
    this.length = length;
    this.width = width;
    } public void showPerimeter()
    {
    double perimeter;
    perimeter = 2 * (length + width);
    System.out.println("周长为:" + perimeter);
    } public void showArea()
    {
    double area;
    area = length * width;
    System.out.println("面积为:" + area);
    }}
    运行结果:
    周长为:0.0
    面积为:0.0
    周长为:16.0
    面积为:15.0
      

  6.   

    晕!lz刚学java啊?这种问题都需要发帖啊?
    大学好好学吧。赶快结帖吧。
      

  7.   

    public class Rectangle
    {
       private int wid;
       private int len;
       public Rectangle()
       {
        this(0,0);
       }
       public Rectangle(int wid,int len)
       {
        this.wid = wid;
        this.len=len;
       }
       //求面积
       public long sumArea()
       {
        return wid*len;
       }
       //求周长
      public long sumRound()
      {
       return 2*(len+wid);
      }
      public static void main(String[]args)
      {
       Rectangle r=new Rectangle(10,20);
       int round=r.sumRound();
       System.out.println("周长为:"+round);
       int area=r.sumArea();
       System.out.println("面积为:"+area);
      }
    }
      

  8.   

    kaiming2008的main()方法中“int round=r.sumRound();”
    不妥吧,建议改为“double round=r.sumRound();”
      

  9.   

    这道题目我在JAVA 入门经典里见过。我把代码贴上来。有两个类。 Rectangle 和TestRectangle ```````````````````````````````````````````````````````````````````````````
    public class Rectangle {
    private Point topLeft; private Point bottomRight; // Constructors
    public Rectangle(double x1, double y1, double x2, double y2) {
    // Ensure topleft uses the smaller of x1,x2 and y1,y2
    // and topright uses the larger of x1,x2 and y1,y2
    topLeft = new Point(Math.min(x1, x2), Math.min(y1, y2));
    bottomRight = new Point(Math.max(x1, x2), Math.max(y1, y2));
    } public Rectangle(Point tl, Point br) {
    this(tl.getX(), tl.getY(), br.getX(), br.getY());
    } public Rectangle(Rectangle rect) {
    topLeft = new Point(rect.topLeft);
    bottomRight = new Point(rect.bottomRight);
    } // Methods to create a rectangle enclosing the current rectangle and the
    // argument
    public Rectangle encloses(Rectangle rect) {
    // Return a new rectangle defined by the minimum x,y for to left and the
    // and maximum x,y for bottom right
    return new Rectangle(Math.min(topLeft.getX(), rect.topLeft.getX()),
    Math.min(topLeft.getY(), rect.topLeft.getY()), Math.max(
    bottomRight.getX(), rect.bottomRight.getX()), Math.max(
    bottomRight.getY(), rect.bottomRight.getY())); } public String toString() {
    return "Rectangle: " + topLeft + " , " + bottomRight;
    }
    }··········································public class TestRectangle {
      public static void main(String args[]) {
    Rectangle[] rects = new Rectangle[4];
    Rectangle enclosing; // Initialize the rectangles as arbitrary sizes and at arbitrary
    // positions:
    for (int i = 0; i < rects.length; i++) {
    rects[i] = new Rectangle(Math.random() * 100, Math.random() * 100,
    Math.random() * 100, Math.random() * 100);
    System.out.print("Coords of rectangle " + i + " are: ");
    System.out.println(rects[i]);
    } // Initialize the enclosing rectangle to be first rectangle
    enclosing = rects[0]; // Combine it with each the other rectangles in turn.
    // This will result in the rectangle that encloses them all.
    for (int i = 1; i < rects.length; i++) {
    enclosing = enclosing.encloses(rects[i]);
    } System.out.println("\nCoords of Enclosing rectangle are ");
    System.out.println(enclosing);
    }
    }