java problem again


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: java problem again

  1. #1
    Join Date
    Oct 2004
    Posts
    11

    java problem again

    Code:
    Hi again!
    Now my program is working but I have problem whit
    Print out. How can I  solve this problem.
    
    class myOverLoad{
          int x1=0;
          int y1=0;
          int x2=0;
          int y2=0;	
    
      myOverLoad buildRect(int x1, int y1, int x2, int y2){
          this.x1=x1;
          this.y1=y1;
          this.x2=x2;
          this.y2=y2;
          return this;
      }
      
      
      myOverLoad buildRect(Point topleft, Point  bottomRight){
            x1=topleft.x;
            y1=topleft.y;
            x2=bottomRight.x;
            y2=bottomRight.y;
            return this;
      }
      
      
       myOverLoad buildRect(Point topleft, int w, int h){
           x1=topleft.x;
           y1=topleft.y;
           x2=(x1+w);
           y2=(y1+h);
           return this;
      }
    	
    }
    class Overload1{
    public static void main (String[]args){
       my.buildRect(1, 2, 3, 4);
          my.buildRect(new Point(1,2), new Point(3,4)); 
            my.buildRect(new Point(1,2),10,11);
              
            System.out.println(my.buildRect(1, 2, 3, 4));
          System.out.println(my.buildRect(new Point(5,6), new Point(7,8) ));
       System.out.println(my.buildRect(new Point(5,6), new Poin(7,8) ));
      }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I cannot imagine that this will compile as you are using an undeclared and uninitiated
    variable (my) in the main method. If you instatiate a new instance of myOverLoad
    like:

    myOverLoad my=new myOverLoad();

    and the call the buildRect method repeatedly you are changing the contents of the
    my instance each time. What you should do was make a toString method for the
    myOverLoad class like
    Code:
    public String toString() {
      return "myOverLoad: x1:"+x1+" y1:"+y1+" x2:"+x2+" y2:"+y2;
    }
    The you could, in the main method do this:

    System.out.println(my.buildRect(new Point(1,2), new Point(3,4)));

    and so on....

    But..... why ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center