Link to home
Create AccountLog in
Avatar of tedschnieders
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);

      }


      
      }
}
Avatar of tedschnieders
tedschnieders

ASKER

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


thanks
Avatar of Mick Barry
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());
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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+"]";
}
>> 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?
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 + ")";

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


ignore that, just noticed they deal in different types.
SOLUTION
Link to home
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());

But
   System.out.println(s);
will do the same
I replaced it still get null
see im kinda confused on how you take TopLeft, topright, bottomleft, bottomright and set them
What is

>>  Point enclose;

supposed to contain?
got it


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

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

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



> System.out.println(s);

That displays your square

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

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;
     }
>>>> 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
> Condition: you should have this constructor

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.
System.out.println(s.enclose());
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);
     }
> so you are sayen that i have 2  squares

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.

>> those two statement do *not* display the same object
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
... and since toString prints out the coordinates...
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
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
>> 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
>> just wonderen if you guys know someway that i can get help besides here
Here's the best ;°)
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
>> must be some pretty damn nice people in this world
:°)
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?
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
> 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.
>> 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
thanks to you both

really appreciate it
Thanks for accepting
thanks mate, let us know if you interested in the tutoring.