1

What I have here is the skeleton of the project in Java. This is based off of creating data types and I will basically have it find the area, perimeter, find if they intersect or contain each other. I understand the formulas I need to use but how would I actually create rectangle b so just like the initial rectangle I have to have an x,y center and a width and height? I had tried in a similar fashion to declare rectangle b but it refuses to assign any of the variables.

In order to find if it rectangle b intersects with the other rectangle I need to define it in order to do calculations for the corners of the rectangle etc, here is my code:

public class OwnRectangles {

    private final double x,y;  //center of rectangle
    private final double width; //width of rectangle
    private final double height; //height of rectangle

    public Rectangle(double x0, double y0, double w, double h)
    {
        x=x0;
        y=y0;
        width=w;
        height=h;
    }
    public double area()
    {
        return width*height;
    }

    public double perimeter()
    {
        return height*2 + width*2;
    }

    public boolean intersects(Rectangle b)
    {

    }

    public boolean contains(Rectangle b)
    {

    }

    public void draw(Rectangle b)
    {
        /*Draw rectangle on standard drawing*/
    }

}

I essentially am trying to create another rectangle, I tried something like this which would not work:

public OwnRectanglesb(double x2, double y2, double w2, double h2)
{
    x=x2;
    y=y2;
    width=w2;
    height=h2;
}

Not only does this name not match the OwnRectangles b, but the public... is supposed to be just OwnRectangles if that makes sense. Very simply I want to define a second rectangle to use.

5
  • a) for class OwnRectangles the constructor should have the same name; b) last two methods are missing return statements (so it won't compile); c) what do you mean by "it refuses to assign any of the variables"? Commented Apr 17, 2018 at 16:59
  • @khachik when I try creating a Rectangle B, now changed to match the name properly as others just mentioned below, it won't allow me to assign something to the x value such as the original rectangle. Commented Apr 17, 2018 at 17:05
  • Your code does not compile and the compiler error should be a hint as to what is wrong with you corde (check answers below). "double x0, double y0, double w, double h" - Please don't do that. Give the parameters some meaningful names. Commented Apr 17, 2018 at 17:07
  • @Turing85 I will this is just a skeleton as mentioned above but thank you for the advice Commented Apr 17, 2018 at 17:09
  • @khachik never mind solved it myself! I was declaring something like ownrectangles b when it is NOT supposed to have a space. Edit not solved because it still would have to match the name of the project so I cant use ownrectanglesb Commented Apr 17, 2018 at 17:10

4 Answers 4

1

The class OwnRectangle needs to match the use of Rectangle in your code, for example change to:

public class Rectangle /* CHANGED FROM OwnRectangle to Rectangle */ {
    private final double x,y;  //center of rectangle
    private final double width; //width of rectangle
    private final double height; //height of rectangle

    public Rectangle(double x0, double y0, double w, double h)
    {
        x=x0;
        y=y0;
        width=w;
        height=h;
    }
    public double area()
    {
        return width*height;
    }

    public double perimeter()
    {
        return height*2 + width*2;
    }

    public boolean intersects(Rectangle b)
    {

    }

    public boolean contains(Rectangle b)
    {

    }

    public void draw(Rectangle b)
    {
    /*Draw rectangle on standard drawing*/
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

In order to create the rectangle objects and determine if they intersect, your code would look something like this:

Rectangle firstRectangle = new Rectangle(1, 2, 3, 4);
Rectangle secondRectangle = new Rectangle(5, 6, 7, 8);
boolean intersects = firstRectangle.intersects(secondRectangle);

Where the values 1, 2, 3, 4, 5, 6, 7, 8 are just arbitrary values you would replace with the true values. Then in your intersects method you would just compare x, y, width, and height to b.x, b.y, b.width, and b.height. Lastly, all references to the Rectangle object should be called the same thing Rectangle, not interchanging Rectangle and OwnRectangle.

1 Comment

This is a nice example however I am looking to set up a completely different rectangle because I then want to use the x,y,width and height in order to find the corners of the rectangle and use this for my calculation
1

I have figured out the answer. To do so I just create different instances of the class, putting it directly under:

public OwnRectangles(double x0, double y0, double w, double h)
{
    x=x0;
    y=y0;
    width=w;
    height=h;

    Rectangle a = new OwnRectangles(//put arguments here);
    Rectangle b = new OwnRectangles(//put arguments here);
}

As can be seen I have created two different rectangles based off of the class rectangle, and of course in the parenthesis I would put the arguments allowing the user to set the width height etc of the rectangles to anything that they want.

Comments

0

Your class name is OwnRectangles but what appears to be a constructor is named Rectangle. The constructor name must match the class name, otherwise it's just a method. You could correct it by writing:

public class OwnRectangles {

  private final double x,y;  //center of rectangle
  private final double width; //width of rectangle
  private final double height; //height of rectangle

  public OwnRectangles(double x0, double y0, double w, double h) {
  ...

Comments