Results 1 to 3 of 3
  1. #1
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default Rectangle class again

    I have the no argument constructor written for a rectangle class. I had to set the height=10 and width=25. And now I need to make a constructor with 2 int arguments w and h and sets the width to w and the height to h only if h and w are greater than 0 and less than 30 otherwise error message. Looking at what I have so far, why can't I create r2 in the Rectangle r2 = new Rectangle() ?
    Code:
    public class Rectangle {


    private int height;
    private int width;


    public static void main(String[] args) {
    Rectangle r = new Rectangle();
    Rectangle r2 = new Rectangle(int w, int h);
    System.out.println("Default rectangle created:" );
    }

    public void setHeight(int height){
    height = 10;
    }
    public void setWidth (int width){
    width = 25;
    }


    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So you don't reply to the answer in your other thread and then start a new thread?

    This is how not to encourage people to help you.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't see your constructors. The reason why your first rectangle worked was because the compiler supplies you win a default constructor if you don't supply any constructors. Once you create a constructor(of any type) the commoners default is gone.

    The constructor is a special type of method. It's not really a method, but for the purpose of this explanation I'm relating it to one. It has no return type(void IS a return type, thus you can't have this in the declaration) and it has the same name as the class. It can also have a variable length of formal parameters as well as multiple overloaded headers.

    They are declared in the class, not in main as you have done. The body of the constructor does something with the arguments and the classes instance variables. Quick example shown below

    Java Code:
    public class Bear{
      int weight;
      String name;
      public Bear(){
        weight = 500;
        name = "yogi";
      }
      public String toString(){
        return name + " weighs " + weight + " pounds.";
      }
      public static void main(String[] args){
        Bear b = new Bear();
        System.out.println(b);
      }
    }
    I suggest you compile and run this. If you are still confused google the following phrase "oracle constructors java tutorial"

Similar Threads

  1. Rectangle class with no argument constructor
    By toppcon in forum New To Java
    Replies: 1
    Last Post: 05-05-2011, 01:37 AM
  2. Replies: 6
    Last Post: 11-30-2010, 11:29 PM
  3. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 03:39 PM
  4. Construct a rectangle
    By cstokes91 in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 06:14 AM
  5. help with rectangle class
    By darkgt in forum New To Java
    Replies: 7
    Last Post: 11-14-2007, 06:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •