Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 379

# how to construct a square

i have the cordinates of each corner but i dont know what to do with them.  I think that i need to take all those cordinates and put them together but im not really sure how.

any help

Thanks
Ted

Square s = new Square(30,20,50,10);

System.out.println("\nSquare:");
System.out.println("The Distance From Top Left To Top Right = " + s.topLeft.getDistance(s.topRight));
System.out.println("The Distance From Top Left To Bottom Left = " + s.topLeft.getDistance(s.bottomLeft));
System.out.println("The Distance From Bottom Left To Bottom Right = " + s.bottomLeft.getDistance(s.bottomRight));
System.out.println("The Distance From Bottom Right To Top Right = " + s.bottomRight.getDistance(s.topRight));

System.out.println(s.enclose);

}
}
--------------------------------------------------------------
public class Square{

Point topLeft;
Point topRight;
Point bottomRight;
Point bottomLeft;
Point enclose;

public Square(double x1, double y1, double x2, double y2) {
topLeft = new Point(Math.min(x1,x2),Math.max(y1,y2));
bottomLeft = new Point(Math.min(x1,x2),Math.min(y1,y2));
topRight = new Point(Math.max(x1,x2),Math.max(y1,y2));
bottomRight = new Point(Math.max(x1,x2),Math.min(y1,y2));
}

public Square(Point topLeft, Point topRight, Point bottomLeft, Point bottomRight){

}
public Square enclose(){

return new Square(topLeft, topRight, bottomLeft, bottomRight);

}

}
}
0
tedschnieders
• 22
• 15
• 11
2 Solutions

Author Commented:
also when i print it out the way it is >>>>  System.out.println(s.enclose); it returns "null" why is that;

thanks
0

Commented:
what exactly is it you are having problems with?
0

Commented:
>      System.out.println(s.enclose);

it returns the encode member var

sounds like you want to call the member function:

System.out.println(s.enclose());
0

Software engineerCommented:
>> also when i print it out the way it is >>>>  System.out.println(s.enclose); it returns "null" why is that;

public Square(Point topLeft, Point topRight, Point bottomLeft, Point bottomRight){
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
}
0

Commented:
If you want to use println() with your objects then you may want to give them a toString() method (to provide a string representation of the object). For example something like:

public String toString()
{
return "["+topLeft+","+topRight+"]";
}
0

Software engineerCommented:
>> i have the cordinates of each corner but i dont know what to do with them.
Well, you have

public Square(double x1, double y1, double x2, double y2) {
topLeft = new Point(Math.min(x1,x2),Math.max(y1,y2));
bottomLeft = new Point(Math.min(x1,x2),Math.min(y1,y2));
topRight = new Point(Math.max(x1,x2),Math.max(y1,y2));
bottomRight = new Point(Math.max(x1,x2),Math.min(y1,y2));
}

and that is right.
What do you want more?
0

Author Commented:
i changed the code to this and i still get "null" when i print out >>>>System.out.println(s.enclose); -----> What more do you want?  im not sure its just so abstract to me i know that is the cordinates of a square but it seems like they should be stored in one place  line my topLeft stores an x and y cordinate.

public class Square{

Point topLeft;
Point topRight;
Point bottomRight;
Point bottomLeft;
Point enclose;

public Square(double x1, double y1, double x2, double y2) {
topLeft = new Point(Math.min(x1,x2),Math.max(y1,y2));
bottomLeft = new Point(Math.min(x1,x2),Math.min(y1,y2));
topRight = new Point(Math.max(x1,x2),Math.max(y1,y2));
bottomRight = new Point(Math.max(x1,x2),Math.min(y1,y2));
}

public Square(Point topLeft, Point topRight, Point bottomLeft, Point bottomRight){
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
}
public Square enclose(){

return new Square(topLeft, topRight, bottomLeft, bottomRight);

}

public String toString(){

return "(" + topLeft + "," + topRight + ")  :  (" + bottomLeft + "," + topRight + ")";

}

}
0

Commented:
> i changed the code to this and i still get "null" when i print out >>>>System.out.println(s.enclose);

See my earlier comment, the ctor had nothing to with it (it isn't even being called)
0

Commented:
you could also do this:

public Square enclose(){
enclose = new Square(topLeft, topRight, bottomLeft, bottomRight);
return enclose;
}

But your println should still be:

System.out.println(s.enclose());
0

Commented:
>    public Square enclose(){
>          enclose = new Square(topLeft, topRight, bottomLeft, bottomRight);
>          return enclose;
>     }

ignore that, just noticed they deal in different types.
0

Commented:
you need to set the value of the enclose member variable if you do not want it to be null.
0

Software engineerCommented:
Replace
>> System.out.println(s.enclose);
by
System.out.println(s.enclose());

0

Software engineerCommented:
But
System.out.println(s);
will do the same
0

Author Commented:
I replaced it still get null
see im kinda confused on how you take TopLeft, topright, bottomleft, bottomright and set them
0

Software engineerCommented:
What is

>>  Point enclose;

supposed to contain?
0

Author Commented:
got it

how come >>>>>System.out.println(s); works  but  >>> System.out.println(s.enclose()); doesn't

0

Commented:
> I replaced it still get null

you shouldn't be, check you recompiled

> see im kinda confused on how you take TopLeft, topright, bottomleft, bottomright and set them

0

Software engineerCommented:
>> I replaced it still get null
Replaced what by what?

>> im kinda confused on how you take TopLeft, topright, bottomleft, bottomright and set them
What do you mean?

0

Commented:
how come >>>>>System.out.println(s); works
but  >>> System.out.println(s.enclose()); doesn't

They don't actually do the same thing as zzynx claims.
The 2nd cannot display null
0

Software engineerCommented:
>>  how come >>>>>System.out.println(s); works  but  >>> System.out.println(s.enclose()); doesn't

1) System.out.println(s);
prints out the result of the toString() function
2) System.out.println(s.enclose());
prints out the result of the enclose() function

If you have:

public Square enclose(){
return new Square(topLeft, topRight, bottomLeft, bottomRight);
}

it shouldn't print null

3) System.out.println(s.enclose);
prints out the member variable enclose
Which is null if you don't set it anywhere

0

Commented:
> System.out.println(s);

> System.out.println(s.enclose());

That displays the square returned by enclose()
0

Software engineerCommented:
If you have:

public Square enclose(){
return new Square(topLeft, topRight, bottomLeft, bottomRight);
}

it shouldn't print null

Condition: you should have this constructor

public Square(Point topLeft, Point topRight, Point bottomLeft, Point bottomRight){
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
}
0

Software engineerCommented:
>>>> System.out.println(s.enclose());
>> That displays the square returned by enclose()
But since you have:

public Square enclose(){
return new Square(topLeft, topRight, bottomLeft, bottomRight);
}

and

public Square(Point topLeft, Point topRight, Point bottomLeft, Point bottomRight){
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
this.bottomRight = bottomRight;
}

the result will be the same
0

Commented:
> Condition: you should have this constructor

Incorrect, you will not get null regardless of what the ctor looks like
0

Commented:
> the result will be the same

Not the same, each will display two different Square instances that just happen to have the same coordinates in this case.
0

Author Commented:
System.out.println(s.enclose());
System.out.println(s);

so you are sayen that i have 2  squares
0

Software engineerCommented:
Yes, since in the function enclose you create a new Square:

public Square enclose(){
return new Square(topLeft, topRight, bottomLeft, bottomRight);
}
0

Commented:
> so you are sayen that i have 2  squares

That is correct, those two statement do *not* display the same object
0

Software engineerCommented:
>>>> the result will be the same
>>Not the same, each will display two different Square instances that just happen to have the same coordinates in this case
So, the result of
return "(" + topLeft + "," + topRight + ")  :  (" + bottomLeft + "," + topRight + ")";
will be the same.
Q.E.D.

0

Software engineerCommented:
>> those two statement do *not* display the same object
But since those two squares have the same coordinates, they DO print the same
0

Software engineerCommented:
>> Yes, since in the function enclose you create a new Square:
But with the same coordinates
0

Software engineerCommented:
... and since toString prints out the coordinates...
0

Author Commented:
what do you guys get  for helping people

not to be rude or anything

but i just wonder whats in it for you if you want to answer
0

Author Commented:
basically what i am getting at is i am a student which i know i am not suppose to be on here getting help but i dont know what else to do.  I had a tutor at school but the kid never shows up so i just work my ass off tryen to figgure the stuff out.  i have been sitten here for 13 hours now on what should be a simple program. just wonderen if you guys know someway that i can get help besides here where i can get tutored if you will. and maybe you guys have some kind of price that would get you to do it.

thanks ted
0

Software engineerCommented:
>> what do you guys get  for helping people
plus the honour of being in some point ranking
0

Software engineerCommented:
>> just wonderen if you guys know someway that i can get help besides here
Here's the best ;Â°)
0

Author Commented:
must be some pretty damn nice people in this world
0

Software engineerCommented:
>> i am a student which i know i am not suppose to be on here getting help
Who said that?
We're not allowed to do work that you are supposed to do, but we are for sure here for helping you
0

Software engineerCommented:
>> must be some pretty damn nice people in this world
:Â°)
0

Author Commented:
yeah i dont want it done for me i want to learn it . i know that i am a little slow picking the **** up but it just amazes the hell out of me
0

Software engineerCommented:
>> yeah i dont want it done for me i want to learn it .
Then there's no problem at all. That's exactly what this site is for.

What questions left?
0

Author Commented:
well right now we are going over defing classes, overloading fuctions, constructors stuff like that any thing you can tell me that will help me out.

i get really confused with the multiple pages of code
0

Commented:
> ust wonderen if you guys know someway that i can get help besides here where i can get tutored if you will.

There is a tutoring service available http://www.objects.com.au if you are interested.
0

Software engineerCommented:
>> we are going over defing classes, overloading fuctions, constructors stuff like that any thing you can tell me that will help me out.
I would suggest a good Google. There's a lot of stuff out there.

E.g.
http://leepoint.net/notes-java/index.html
0

Author Commented:
thanks to you both

really appreciate it
0

Software engineerCommented:
0

Software engineerCommented:
Thanks for accepting
0

Commented:
thanks mate, let us know if you interested in the tutoring.
0

## Featured Post

• 22
• 15
• 11
Tackle projects and never again get stuck behind a technical roadblock.