Results 1 to 5 of 5
  1. #1
    camper2 is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default Assigning values to an object

    Hi, I am supposed to have a constructor that creates a new Triangle object, and assigns coordinates and measurements for the triangle.

    Here is what I have:

    Java Code:
    public Triangle(int xPos, int yPos, int height, int width)
        {
            this();
            Triangle aTriangle = new Triangle();
        }
    but when I run the program, and create the object, it asks for the parameters, I specify them, but the parameters stay set to the same numbers. Im kind of confused, thanks.
    Last edited by camper2; 04-05-2009 at 03:55 AM.

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

    Default

    1) Don't try to construct aTriangle within Triangle's constructor. The constructor itself is already creating a new Triangle object. In other words, get rid of the line:
    Java Code:
    Triangle aTriangle = new Triangle();
    2) you need to set the class's fields to the parameter values within the constructor. For instance:
    Java Code:
    public class Fubar2
    {
      private int value;
      
      public Fubar2(int value)
      {
        this.value = value;
      }
    }

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Triangle is clearly not doing anything with the parameters it takes in. That's why it does nothing. You should have something more like

    Java Code:
    this.xPos = xPos;
    this.yPos = yPos;
    this.height = height;
    this.width = width;

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    you beat me, i'm too slow...

  5. #5
    camper2 is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    ah that worked, thanks again that really helped, and it let me see what I was doing wrong.

Similar Threads

  1. passing object as value for checkbox values??
    By Pooja Deshpande in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-17-2009, 12:55 PM
  2. declaring fields without assigning values to them
    By diggitydoggz in forum New To Java
    Replies: 12
    Last Post: 01-03-2009, 09:22 PM
  3. Replies: 0
    Last Post: 11-21-2008, 09:17 AM
  4. [SOLVED] Creating List of Values Using ClipBoard Object
    By Judoon_Platoon in forum Java Applets
    Replies: 2
    Last Post: 05-21-2008, 09:07 AM
  5. Replies: 1
    Last Post: 08-07-2007, 12:10 AM

Posting Permissions

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