Results 1 to 4 of 4
Thread: Hello all
- 11-02-2012, 01:31 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 14
- Rep Power
- 0
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?
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.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; } }
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.
- 11-02-2012, 01:55 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 11-04-2012, 03:15 AM #3
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Hello all
Should point out your problem.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());
- 11-05-2012, 11:58 AM #4
Member
- Join Date
- Nov 2012
- Posts
- 14
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks