Results 1 to 4 of 4

Thread: Hello all

  1. #1
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Hello all

    Just registered here on the forums and wanted to say hello.

    I am studying computer science on my first year here in Copenhagen and were using Java as the main programming language in our classes.

    Im currently doing excercises from our book called "Building Java Programs - A back to basics approrach" and I'm kind of a sticklre with finishing all the excercises but I'm stuck on this one.

    We just started on learning the OOP concepts and I'm doing an excercise that goes like this:

    ( I have built a rectangle class in the previous excercises and filled it with methods, some of my own creation )

    16. Add the following method to your Rectangle class:
    public Rectangle union(Rectangle rect)
    Returns a new Rectangle that represents the area occupied by the tightest bounding box that contains both this
    Rectangle and the given other Rectangle.



    This is the code from my entire class, the method I'm trying to get to work is the one called "union" which should solve the problem in the above excercise, my problem is it doesn't assign any values to the return object when I use it in the main program for some reason and I can't seem to figure out why?
    Java Code:
    import java.util.*;			// Opg13-17Kap8
    import java.awt.Point;
    
    public class Rectangle {
    	
    	// Field values.
    	private int x;
    	private int y;
    	private int width;
    	private int height;
    	private int rightX = x+width;
    	private int rightY = y-height;
    	
    	// Opg13Kap8
    	public Rectangle( int x, int y, int width, int height ) {
    		this.x = x;
    		this.y = y;
    		this.width = width;
    		this.height = height;
    	}
    	
    		
    	public int getHeight() {
    		return this.height;
    	}
    	
    	public void setHeight(int height) {
    		this.height = height;
    	}
    	
    	public int getWidth() {
    		return this.width;
    	}
    	
    	public void setWidth( int width ) {
    		this.width = width;
    	}
    	
    	public int getX() {
    		return this.x;
    	}
    	public int getRightX() {
    		return this.rightX;
    	}
    	
    	public int getY() {
    		return this.y;
    	}
    	public int getRightY() {
    		return this.rightY;
    	}
    	
    	public void setX( int x ) {
    		this.x = x;
    	}
    	
    	public void setY( int y ) {
    		this.y = y;
    	}
    	
    	public String toString() {
    		return "( x="+x+", y="+y+", Width="+width+", Height="+height+" )";
    	}
    	
    	
    	// Opg14Kap8
    	public Rectangle( Point p, int width, int height ) {
    		this.x = (int)p.getX();
    		this.y = (int)p.getY();
    		this.width = width;
    		this.height = height;
    	}
    
    	
    	//	Opg15Kap8
    	public boolean contains( int x, int y ) {
    		boolean answer = false;
    		if ( x>=this.x && x<=(this.x+this.width) && y<=this.y && y>=(this.y-height) ) {
    			answer = true;
    		}
    		return answer;	
    	}
    	
    	// Opg15Kap8
    	public boolean contains( Point other ) {
    		boolean answer = false;
    		if ( other.x>=this.x && other.x<=(this.x+this.width) && other.y<=this.y && other.y>=(this.y-height) ) {
    			answer = true;
    		}
    		return answer;
    	}
    	
    	// Opg16Kap8 IKKE FÆRDIG
    	public Rectangle union( Rectangle other ) {
    		Rectangle temp = new Rectangle( 0, 0, 0, 0 );
    		temp.setX	  ( Math.min( this.getX(), other.getX() ) );
    		temp.setY     ( Math.max( this.getY(), other.getY() ) );
    		temp.setWidth ( Math.abs( temp.getX() + Math.max( this.getRightX(), other.getRightX() )  )  );
    		temp.setHeight( Math.abs( temp.getY() + Math.min( this.getRightY(), other.getRightY() )  )  );
    	 	return temp;
    	}
    
    }
    I should probably mention that the x and y field values, represents the point in a normal 2d coordinate system, more specifically the point where the top left corner of the rectangle is located.

    Also the rightX and rightY field values represents the point in the coordinate system where the lower right corner of the rectangle is located.
    Last edited by ObedMarsh; 11-02-2012 at 01:49 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Hello all

    Stick some debugging in there to see what values are being calculated.
    That is, create some temporary values so you can see what the min(x) and max(y) and the two abs values are and print them out.
    If they are all correct, and temp has them all set correctly then the problem is with the calling code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Hello all

    Java Code:
    		Rectangle rect = new Rectangle(0,0,0,0);
    		rect.setHeight(5);
    		rect.setWidth(4);
    		System.out.println(rect);
    		System.out.println(rect.getRightX());
    		System.out.println(rect.getRightY());
    Should point out your problem.

  4. #4
    ObedMarsh is offline Member
    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Default Re: Hello all

    Thanks for the advice , I localized the rightX and rightY values instead and now it seems to be working. Thanks

    So I guess I should learn that you can't make field values that are made out of other combined field values? Or is it just the way I did it that seems to be buggy?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •