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);
}
Re: Setting constraints on a field
Code java:
public void Shape(int x, int y){
That is not a constructor!
Re: Setting constraints on a field
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.
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.
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.
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.
Re: Setting constraints on a field
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.
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
Re: Setting constraints on a field
Yeah, so think logically, set before getting. Or simply remove x.getX() from the parametrized constructor.
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."
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.