?
Solved

how to construct a square

Posted on 2005-03-02
48
Medium Priority
?
373 Views
Last Modified: 2010-03-31
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
Comment
Question by:tedschnieders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 22
  • 15
  • 11
48 Comments
 

Author Comment

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


thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 13437306
what exactly is it you are having problems with?
0
 
LVL 92

Expert Comment

by:objects
ID: 13437310
>      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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 37

Accepted Solution

by:
zzynx earned 1000 total points
ID: 13437316
>> also when i print it out the way it is >>>>  System.out.println(s.enclose); it returns "null" why is that;
Add:

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

Expert Comment

by:objects
ID: 13437323
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13437337
>> 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 Comment

by:tedschnieders
ID: 13437364
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
 
LVL 92

Expert Comment

by:objects
ID: 13437376
> 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
 
LVL 92

Expert Comment

by:objects
ID: 13437379
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
 
LVL 92

Expert Comment

by:objects
ID: 13437387
>    public Square enclose(){
>          enclose = new Square(topLeft, topRight, bottomLeft, bottomRight);
>          return enclose;
>     }


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

Assisted Solution

by:objects
objects earned 1000 total points
ID: 13437395
you need to set the value of the enclose member variable if you do not want it to be null.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437399
Replace
>> System.out.println(s.enclose);
by
     System.out.println(s.enclose());

0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437406
But
   System.out.println(s);
will do the same
0
 

Author Comment

by:tedschnieders
ID: 13437407
I replaced it still get null
see im kinda confused on how you take TopLeft, topright, bottomleft, bottomright and set them
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437414
What is

>>  Point enclose;

supposed to contain?
0
 

Author Comment

by:tedschnieders
ID: 13437415
got it


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

0
 
LVL 92

Expert Comment

by:objects
ID: 13437419
> 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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437420
>> 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
 
LVL 92

Expert Comment

by:objects
ID: 13437426
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13437432
>>  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
 
LVL 92

Expert Comment

by:objects
ID: 13437436
> System.out.println(s);

That displays your square

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

That displays the square returned by enclose()
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437441
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13437450
>>>> 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
 
LVL 92

Expert Comment

by:objects
ID: 13437452
> Condition: you should have this constructor

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

Expert Comment

by:objects
ID: 13437457
> 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 Comment

by:tedschnieders
ID: 13437471
System.out.println(s.enclose());
System.out.println(s);


so you are sayen that i have 2  squares
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437478
Yes, since in the function enclose you create a new Square:

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

Expert Comment

by:objects
ID: 13437482
> so you are sayen that i have 2  squares

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

Expert Comment

by:zzynx
ID: 13437485
>>>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 13437490
>> those two statement do *not* display the same object
But since those two squares have the same coordinates, they DO print the same
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437495
>> Yes, since in the function enclose you create a new Square:
But with the same coordinates
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437499
... and since toString prints out the coordinates...
0
 

Author Comment

by:tedschnieders
ID: 13437508
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 Comment

by:tedschnieders
ID: 13437531
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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437532
>> 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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437540
>> just wonderen if you guys know someway that i can get help besides here
Here's the best ;°)
0
 

Author Comment

by:tedschnieders
ID: 13437541
must be some pretty damn nice people in this world
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437544
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 13437548
>> must be some pretty damn nice people in this world
:°)
0
 

Author Comment

by:tedschnieders
ID: 13437551
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13437558
>> 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 Comment

by:tedschnieders
ID: 13437562
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
 
LVL 92

Expert Comment

by:objects
ID: 13437567
> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 13437592
>> 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 Comment

by:tedschnieders
ID: 13437607
thanks to you both

really appreciate it
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13437647
Thanks for accepting
0
 
LVL 92

Expert Comment

by:objects
ID: 13444271
thanks mate, let us know if you interested in the tutoring.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question