tedschnieders

asked on

# 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);

}

}

}

any help

Thanks

Ted

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

System.out.println("\nSqua

System.out.println("The Distance From Top Left To Top Right = " + s.topLeft.getDistance(s.to

System.out.println("The Distance From Top Left To Bottom Left = " + s.topLeft.getDistance(s.bo

System.out.println("The Distance From Bottom Left To Bottom Right = " + s.bottomLeft.getDistance(s

System.out.println("The Distance From Bottom Right To Top Right = " + s.bottomRight.getDistance(

System.out.println(s.enclo

}

}

--------------------------

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

bottomLeft = new Point(Math.min(x1,x2),Math

topRight = new Point(Math.max(x1,x2),Math

bottomRight = new Point(Math.max(x1,x2),Math

}

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

}

public Square enclose(){

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

}

}

}

what exactly is it you are having problems with?

> 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());

it returns the encode member var

sounds like you want to call the member function:

System.out.println(s.enclo

ASKER CERTIFIED SOLUTION

membership

Create a free account to see this answer

Signing up is free and takes 30 seconds.

**No credit card required.**
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+"]";

}

public String toString()

{

return "["+topLeft+","+topRight+"

}

>> 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?

Well, you have

public Square(double x1, double y1, double x2, double y2) {

topLeft = new Point(Math.min(x1,x2),Math

bottomLeft = new Point(Math.min(x1,x2),Math

topRight = new Point(Math.max(x1,x2),Math

bottomRight = new Point(Math.max(x1,x2),Math

}

and that is right.

What do you want more?

ASKER

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 + ")";

}

}

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

bottomLeft = new Point(Math.min(x1,x2),Math

topRight = new Point(Math.max(x1,x2),Math

bottomRight = new Point(Math.max(x1,x2),Math

}

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 + ")";

}

}

> 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)

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

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());

public Square enclose(){

enclose = new Square(topLeft, topRight, bottomLeft, bottomRight);

return enclose;

}

But your println should still be:

System.out.println(s.enclo

> public Square enclose(){

> enclose = new Square(topLeft, topRight, bottomLeft, bottomRight);

> return enclose;

> }

ignore that, just noticed they deal in different types.

> enclose = new Square(topLeft, topRight, bottomLeft, bottomRight);

> return enclose;

> }

ignore that, just noticed they deal in different types.

SOLUTION

membership

Create a free account to see this answer

Signing up is free and takes 30 seconds.

**No credit card required.**
Replace

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

by

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

>> System.out.println(s.enclo

by

System.out.println(s.enclo

But

System.out.println(s);

will do the same

System.out.println(s);

will do the same

ASKER

I replaced it still get null

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

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

What is

>> Point enclose;

supposed to contain?

>> Point enclose;

supposed to contain?

ASKER

got it

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

how come >>>>>System.out.println(s)

> 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

you already set them in your constructor

you shouldn't be, check you recompiled

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

you already set them in your constructor

>> 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?

Replaced what by what?

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

What do you mean?

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

but >>> System.out.println(s.enclo

They don't actually do the same thing as zzynx claims.

The 2nd cannot display null

>> 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

1) System.out.println(s);

prints out the result of the toString() function

2) System.out.println(s.enclo

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.enclo

prints out the member variable enclose

Which is null if you don't set it anywhere

> System.out.println(s);

That displays your square

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

That displays the square returned by enclose()

That displays your square

> System.out.println(s.enclo

That displays the square returned by enclose()

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;

}

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;

}

>>>> 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

>> 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

> Condition: you should have this constructor

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

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

> 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.

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

ASKER

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

System.out.println(s);

so you are sayen that i have 2 squares

System.out.println(s);

so you are sayen that i have 2 squares

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

public Square enclose(){

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

}

public Square enclose(){

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

}

> so you are sayen that i have 2 squares

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

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

>>>> 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.

>>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.

>> those two statement do *not* display the same object

But since those two squares have the same coordinates, they DO print the same

But since those two squares have the same coordinates, they DO print the same

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

But with the same coordinates

But with the same coordinates

... and since toString prints out the coordinates...

ASKER

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

not to be rude or anything

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

ASKER

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.

any advise

thanks ted

any advise

thanks ted

>> what do you guys get for helping people

Nothing but "Thank you" and "Your comments really helped me out"

plus the honour of being in some point ranking

Nothing but "Thank you" and "Your comments really helped me out"

plus the honour of being in some point ranking

>> just wonderen if you guys know someway that i can get help besides here

Here's the best ;°)

Here's the best ;°)

ASKER

must be some pretty damn nice people in this world

>> 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

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

>> must be some pretty damn nice people in this world

:°)

:°)

ASKER

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

>> 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?

Then there's no problem at all. That's exactly what this site is for.

What questions left?

ASKER

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

i get really confused with the multiple pages of code

> 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.

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

>> 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

I would suggest a good Google. There's a lot of stuff out there.

E.g.

http://leepoint.net/notes-java/index.html

ASKER

thanks to you both

really appreciate it

really appreciate it

http://java.sun.com/docs/books/tutorial/

http://www.freewarejava.com/tutorials/index.shtml

http://www.devdaily.com/Dir/Java/Articles_and_Tutorials/

Lots of inspiring small examples can be found at:

- http://javaalmanac.com/egs/index.html

- http://www.rgagnon.com/bigindex.html

- http://home-1.worldonline.nl/~bmc88/java/sbook/index.html

http://www.freewarejava.com/tutorials/index.shtml

http://www.devdaily.com/Dir/Java/Articles_and_Tutorials/

Lots of inspiring small examples can be found at:

- http://javaalmanac.com/egs/index.html

- http://www.rgagnon.com/bigindex.html

- http://home-1.worldonline.nl/~bmc88/java/sbook/index.html

Thanks for accepting

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

ASKER

thanks