Results 1 to 17 of 17
  1. #1
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Help With Rectangles

    Hello everybody, I am relatively new to java programming and am in need of assistance with a program.

    Before I begin writing the program, I am given three classes: a 2D point class (Point.java), a skeleton class (Rectangle.java), and a controller class (MainClass.java). These classes are listed below.

    public class Point {
    public int X;
    public int Y;

    public Point(int X, int Y) {
    this.X = X;
    this.Y = Y;
    }

    }

    ----------------------------------------

    public class Rectangle {
    private Point bottomLeft;
    private Point topRight;

    /*********** Write the methods below **********/
    }

    ----------------------------------------

    public class MainClass {

    public static void main(String[] args) {

    Rectangle r1 = new Rectangle();
    System.out.println("A default rectangle created...");
    System.out.println(r1.toString());

    Rectangle r2 = new Rectangle(50, 70, 100, 100);
    System.out.println("Another rectangle created...");
    System.out.println(r2.toString());

    if (Rectangle.overlap(r1, r2)) {
    System.out.println("r1 and r2 are overlapping");
    } else {
    System.out.println("r1 and r2 are disjoint");
    }

    System.out.println("r1 equals r2:" + r1.equals(r2));
    System.out.println("r1 equals a default rectangle:" + r1.equals( new Rectangle() ));


    Point p1 = new Point(30, 20);
    Point p2 = new Point(120, 80);
    Rectangle r3 = new Rectangle(p1 , p2);
    System.out.println("Another rectangle created...");
    System.out.println(r3.toString());

    if (Rectangle.overlap(r3, r1)) {
    System.out.println("r1 and r3 are overlapping");
    } else {
    System.out.println("r1 and r3 are disjoint");
    }

    Rectangle r4 = Rectangle.generateRandomRectangle();
    System.out.println("A random rectangle created...");
    System.out.println(r4.toString());
    }

    }

    ----------------------------------------

    These three classes are to be placed into the same project. Then, I am supposed to define the following methods in Rectangle.java:

    - Rectangle()
    A default constructor that returns a rectangle defined by (0,0) and (100, 50).

    - Rectangle( int x1, int y1, int x2, int y2 )
    A constructor that returns a rectangle defined by (x1,y1) and (x2, y2).

    - Rectangle( Point p1, Point p2 )
    A constructor that returns a rectangle defined by p1 and p2.

    - int getArea()
    return the area of the rectangle.

    - boolean equals( Rectangle r )
    tests whether an input rectangle is equal to the current rectangle

    - String toString()
    return a summary of the rectangle's information in a string

    - static Rectangle generateRandomRectangle()
    This method generates and returns one random rectangle. Note that this is a static method.

    - static boolean overlap(Rectangle r1, Rectangle r2)
    This method accepts two input rectangles and tests whether they overlap with each other. Note that this is a static method.

    Once again, I do not have much experience with programming and I am unclear where to even start. I know I should define the constructors first, but I struggle with this. Could anybody offer me some assistance? Thanks so much.

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

    Default Re: Help With Rectangles

    Start from the top.
    Write the first constructor.

    ALso, you need to wrap your code in [code] tags [/code] so that it is formatted correctly on the forum.
    Please do not ask for code as refusal often offends.

  3. #3
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help With Rectangles

    Okay, sorry about that.

    Java Code:
     public Rectangle() {
         Point a = new Point(0, 0);
         Point b = new Point(100, 50);
    }
    Is this correct? The hardest part for me regarding coding is getting started.

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

    Default Re: Help With Rectangles

    Java Code:
    public class Rectangle {
        private Point bottomLeft;
        private Point topRight;
    
    /*********** Write the methods below **********/ 
    }
    That's the Rectangle class at the moment.
    With your new constructor you need to store those Points, which is what those two member variable are for.
    At the moment you are creating 2 Points, and assigning them to two local variables 'a' and 'b'...but they disappear at the end of the constructor.
    You should be assigning them to your member variables 'bottomLeft' and 'topRight'.
    Please do not ask for code as refusal often offends.

  5. #5
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help With Rectangles

    Ah, I see what you are saying. Here is my revised code:

    Java Code:
     public class Rectangle {
    	private Point bottomLeft;
    	private Point topRight;
    	
    	public Rectangle() {
    		bottomLeft = new Point(0, 0);
    		topRight = new Point(100, 50);
    	}
    }
    Now I am confused as how to create a constructor with parameters. Do I need to create local variables?

  6. #6
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help With Rectangles

    I have worked on my program a little more and now have this:

    Java Code:
     public class Rectangle {
    	private Point bottomLeft;
    	private Point topRight;
    	
    	public Rectangle() {
    		bottomLeft = new Point(0, 0);
    		topRight = new Point(100, 50);
    	}
    	
    	public Rectangle( int x1, int y1, int x2, int y2 ) {
    		bottomLeft = new Point(x1, y1);
    		topRight = new Point(x2, y2);
    	}
    	
    	public Rectangle( Point p1, Point p2 ) {
    		bottomLeft = p1;
    		topRight = p2;
    	}
    	
    	int getArea() {
    		int length = 1; // I am just using 1 as a placeholder.
    		int width = 1; // I am just using 1 as a placeholder.
    		return length * width;
    	}
    }
    I am not sure if the constructors are correct, so let me know if I need to tweak it. Now, I am trying to figure out how to get the length and width from the rectangle. Any suggestions?

  7. #7
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help With Rectangles

    Update on the program. Sorry if this is considered excessive bumping or anything like that.

    Java Code:
     import java.util.Random;
    
    public class Rectangle {
    	private Point bottomLeft;
    	private Point topRight;
    	private int length;
    	private int width;
    	private int area;
    	
    	public Rectangle() {
    		bottomLeft = new Point(0, 0);
    		topRight = new Point(100, 50);
    		length = 100;
    		width = 50;
    	}
    	
    	public Rectangle( int x1, int y1, int x2, int y2 ) {
    		bottomLeft = new Point(x1, y1);
    		topRight = new Point(x2, y2);
    		length = x2 - x1;
    		width = y2 - y1;
    	}
    	
    	public Rectangle( Point p1, Point p2 ) {
    		bottomLeft = p1;
    		topRight = p2;
    		length = p2.X - p1.X;
    		width = p2.Y - p1.Y;
    	}
    	
    	int getArea() {
    		area = length * width;
    		return area;
    	}
    	
    	public boolean equals( Rectangle r ) {
    		if (this.getArea() == r.getArea())
    			return true;
    		else
    			return false;
    	}
    	
    	public String toString() {
    		String info = "length: " + length + "  width: " + width + "  area: " + area;
    		return info;
    	}
    	
    	public static Rectangle generateRandomRectangle() {
    		Random x = new Random();
    		int x1 = x.nextInt();
    		int x2 = x.nextInt();
    		int y1 = x.nextInt();
    		int y2 = x.nextInt();
    		Point a = new Point(x1, y1);
    		Point b = new Point(x2, y2);
    		Rectangle rectangle = new Rectangle(a, b);
    		return rectangle;
    	}
    	
    	public static boolean overlap(Rectangle r1, Rectangle r2) {
    		
    	}
    }
    I am completely lost as on how to begin the overlap method.

  8. #8
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: Help With Rectangles

    Java Code:
    System.out.println("---------------");
    System.out.println("|              |");
    System.out.println("|              |");
    System.out.println("|              |");
    System.out.println("|              |");
    System.out.println("|              |");
    System.out.println("|              |");
    System.out.println("|              |");
    System.out.println("|              |");
    System.out.println("|              |");
    System.out.println("|              |");
    System.out.println("|              |");
    System.out.println("---------------");
    There you go, rectangle. :3

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

    Default Re: Help With Rectangles

    Quote Originally Posted by CG9892 View Post
    Java Code:
    	public boolean equals( Rectangle r ) {
    		if (this.getArea() == r.getArea())
    			return true;
    		else
    			return false;
    	}
    I am completely lost as on how to begin the overlap method.
    Your equals method doesn't match (in my mind) the requirements:
    "tests whether an input rectangle is equal to the current rectangle"

    Matching areas does not (to me) mean matching rectangles. I would say that the values of both Points should be equal (the x and y coords).

    As for the overlap, draw some pictures on some scrap paper. Some with r1 and r2 not overlapping. Some with them overlapping.
    Then see if you can see what the relationship between the 2 pairs of Point values might be.
    Please do not ask for code as refusal often offends.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help With Rectangles

    The part that's bothering me is the 'topleft and bottomright' part. If I supply two points (100,100) (0,0) (in that order) those points need to be checked and corrected or rejected or whatever ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Help With Rectangles

    The requirements aren't terribly clear, it has to be said, but picking up the pieces (eg possible negative areas) afterwards is probably a polishing exercise.
    I was ignoring the actual names...:)
    Please do not ask for code as refusal often offends.

  12. #12
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help With Rectangles

    Thanks for the tips guys. Here is my updated Rectangle class.

    Java Code:
     import java.util.Random;
    
    public class Rectangle {
    	private Point bottomLeft;
    	private Point topRight;
    	public int length;
    	public int width;
    	public int area;
    	
    	public Rectangle() {
    		bottomLeft = new Point(0, 0);
    		topRight = new Point(100, 50);
    		length = 100;
    		width = 50;
    	}
    	
    	public Rectangle( int x1, int y1, int x2, int y2 ) {
    		bottomLeft = new Point(x1, y1);
    		topRight = new Point(x2, y2);
    		length = x2 - x1;
    		width = y2 - y1;
    	}
    	
    	public Rectangle( Point p1, Point p2 ) {
    		bottomLeft = p1;
    		topRight = p2;
    		length = p2.X - p1.X;
    		width = p2.Y - p1.Y;
    	}
    	
    	int getArea() {
    		area = length * width;
    		return area;
    	}
    	
    	public boolean equals( Rectangle r ) {
    		if (this.bottomLeft == r.bottomLeft && this.topRight == r.topRight)
    			return true;
    		else
    			return false;
    	}
    	
    	public String toString() {
    		String info = "length: " + length + "  width: " + width + "  area: " + area;
    		return info;
    	}
    	
    	public static Rectangle generateRandomRectangle() {
    		Random x = new Random();
    		int x1 = x.nextInt();
    		int x2 = x.nextInt();
    		int y1 = x.nextInt();
    		int y2 = x.nextInt();
    		Point a = new Point(x1, y1);
    		Point b = new Point(x2, y2);
    		Rectangle rectangle = new Rectangle(a, b);
    		return rectangle;
    	}
    	
    	public static boolean overlap(Rectangle r1, Rectangle r2) {
    		if (r1.topRight.X > r2.bottomLeft.X) 
    			return true;
    		else if (r1.bottomLeft.X < r2.topRight.X)
    			return true;
    		else if (r1.topRight.Y > r2.bottomLeft.Y)
    			return true;
    		else if (r1.bottomLeft.X < r2.topRight.Y)
    			return true;
    		else
    			return false;
    	}
    }
    For some reason, I am getting two error messages in my main class, one stating "the method overlap(Rectangle, Rectangle) is undefined for the type Rectangle" and "the method generateRandomRectangle() is undefined for the type Rectangle". Any reasons as to why this is happening?

  13. #13
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help With Rectangles

    Fixed that error, so scratch that last statement. Now, I am having trouble with the program's output. Last time I ran the main class, I received this output:

    A default rectangle created...
    Bottomleft point: Point@558ee9d6 Topright point: Point@199a0c7c area: 0
    Another rectangle created...
    Bottomleft point: Point@50a9ae05 Topright point: Point@33dff3a2 area: 0
    r1 and r2 are overlapping
    r1 equals r2:false
    r1 equals a default rectangle:false
    Another rectangle created...
    Bottomleft point: Point@33f42b49 Topright point: Point@6345e044 area: 0
    r1 and r3 are overlapping
    A random rectangle created...
    Bottomleft point: Point@35960f05 Topright point: Point@eb42cbf area: 0

    I changed the toString method to output the coordinates of bottomLeft and topRight. It now looks like this:

    Java Code:
     public String toString() {
    		String info = "Bottomleft point: " + this.bottomLeft + "  Topright point: " + this.topRight + "  area: " + this.area;
    		return info;
    	}

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help With Rectangles

    Your Point class doesn't implement a toString() method, that's why you get that funny output (it's from the Object.toString() method).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help With Rectangles

    Quote Originally Posted by JosAH View Post
    Your Point class doesn't implement a toString() method, that's why you get that funny output (it's from the Object.toString() method).

    kind regards,

    Jos
    That makes sense, thanks. I modified the toString method to

    Java Code:
     public String toString() {
    		String info = "length: " + this.length + "  width: " + this.width + "  area: " + this.getArea();
    		return info;
    	}
    and I have received much better results. However, I still have a problem with the generateRandomRectangle() method. My last output was:

    A default rectangle created...
    length: 100 width: 50 area: 5000
    Another rectangle created...
    length: 50 width: 30 area: 1500
    r1 and r2 are overlapping
    r1 equals r2:false
    r1 equals a default rectangle:false
    Another rectangle created...
    length: 90 width: 60 area: 5400
    r1 and r3 are overlapping
    A random rectangle created...
    length: -574816277 width: -565475835 area: 108986775

    Any ideas pertaining to how I should eliminate negative numbers from the random rectangle output?

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help With Rectangles

    Read the API documentation and see what the Random class actually can generate.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    CG9892 is offline Member
    Join Date
    Nov 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help With Rectangles

    Ah, I see. I think I have figured it out now; I used Math.abs() and just took the absolute value of the length, width and area. I appreciate all the help, guys.

Similar Threads

  1. Why only one of the two rectangles is painted?
    By JOHNINALBANY in forum Java 2D
    Replies: 5
    Last Post: 07-07-2012, 10:54 PM
  2. Intersection of two Rectangles
    By Dex in forum New To Java
    Replies: 4
    Last Post: 03-25-2012, 02:28 PM
  3. Collision between 2 rectangles
    By CNew in forum New To Java
    Replies: 1
    Last Post: 12-05-2010, 04:18 AM
  4. Rectangles method
    By bdario1 in forum New To Java
    Replies: 31
    Last Post: 03-31-2010, 09:32 PM
  5. How to Draw Round Rectangles in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:25 PM

Posting Permissions

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