Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Setting constraints on a field

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Setting constraints on a field

    i am coding a project that has "Shape" as the parent with various shapes (ie rectangle, triangle, etc.) as sub-classes using inheritance. i am supposed "Modify the constructor and set methods of the Shape class to verify the values of X and Y stay between zero and the MAX size. If X or Y are out of range, issue an error message on the console and set the value to zero." for some reason i cannot get it to work. when i run the program it will display the x-value even if it is out of range instead of zero. Here is the code for the parent class "Shape":

    public abstract class Shape {

    int x;
    int y;
    protected final static int X_MAX_SIZE=800;
    protected final static int Y_MAX_SIZE=600;

    //Constructor w/out parameters
    public Shape(){
    }
    //Constructor w/ parameters
    public void Shape(int x, int y){
    x=getX();
    if(x<0 || x>X_MAX_SIZE){
    System.out.println("ERROR!");
    this.x=0;
    } else this.x=x;
    y=getY();
    }
    public int getX(){
    return x;
    }
    public void setX(int x){
    if(x<0 || x>X_MAX_SIZE){
    System.out.println("ERROR!");
    this.x=0;
    } else this.x=x;
    }
    public int getY(){
    return y;
    }
    public void setY(int y){
    this.y=y;
    }

    public abstract double area();
    public abstract void display();
    public abstract void display(Graphics g);
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Setting constraints on a field

    public void Shape(int x, int y){
    That is not a constructor!
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Setting constraints on a field

    can you please clarify?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Setting constraints on a field

    Why don't you find an example of a constructor (in a book or on a website) and compare it with what you have written.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Setting constraints on a field

    so i have a book for java and a professor that say that the following are constructors:

    public Shape(){
    }

    &

    public void Shape(int x, int y){
    }

    which i have both in my code.

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Setting constraints on a field

    Yes the first one is constructor of class Shape but not the second one.
    Constructor has no return type, neither void.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Setting constraints on a field

    so i got rid of the void. not sure why i had it there in the first place cuz i dont have it it any of my other constructors within my project. but i still cant seem to get the program to verify that the variable "x" is in between 0 & 800. when i enter in a negative # (like -20) and run the program it still displays -20 instead of giving me the "ERROR!" message and assigning 0 to x.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Setting constraints on a field

    x=getX();
    What's this line doing? Say, you are passing -20 to x in the parametrized(yours second) constructor and getX() will return you x's value that will be 0 as you didn't yet set.

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Setting constraints on a field

    quite honestly i have no idea why the "x=getX();" line is there, it was just my last effort in trying to get this to work. from my understanding, setX(); is supposed to set the new value for x. when i tried to set the new value for x using my constraints/verification

    if(x<0 || x>X_MAX_SIZE){
    System.out.println("ERROR!")
    this.x=0;
    }
    else this.x=x;

    it just sets the x value as -20

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Setting constraints on a field

    Yeah, so think logically, set before getting. Or simply remove x.getX() from the parametrized constructor.

  11. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Setting constraints on a field

    so i took out the "getX();" line from the shape constructor:

    public Shape(int x, int y){
    this.x=x;
    this.y=y;
    }

    public int getX(){
    return x;
    }

    public void setX(int x){
    if(x<0){
    System.out.println("ERROR!");
    this.x=0;
    } else this.x=x;
    }

    I ran the program:
    "The Circle is located at the x-coordinate of -20 and the y-coordinate of 20 with a raiuds 40."

    It should show something like this:
    "The Circle is located at the x-coordinate of ERROR! 0 and the y-coordinate of 20 with a raiuds 40."

  12. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Setting constraints on a field

    Can't you just debug it on your own now? Try to find out the behaviour of your program by placing PRINT statements after each statement and find it's behaviour.

Similar Threads

  1. What are the constraints on the argument values?
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: November 2nd, 2011, 07:15 AM
  2. How to add date field ?
    By Rajiv in forum Web Frameworks
    Replies: 1
    Last Post: August 10th, 2011, 09:27 PM
  3. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  4. Odd Instance Field Issue
    By destructobob in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 01:46 AM
  5. Using a password field
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 11:54 AM