Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Contains Method HELP PLEASEEEE

Hi again

Ive made the IsSqare method but now he asks us to do the Contsains method.

Rectangle class
Suppose we have a Rectangle class that begins:
class Rectangle {

private Point upperLeft; // the upper left corner point
private double height; // the height of the rectangle
private double width; // the width of the rectangle

contains method
Write a contains method that returns true if the point passed in is inside the rectangle

Thats what i wrote :

```package assignement01;

import java.awt.Point;
import java.util.Scanner;

/**
*
* @author mouni
*/
public class Rectangle {

private Point upperLeft; // the upper left corner point
private double height; // the height of the rectangle
private double width; // the width of the rectangle

public static void main(String[] args) {
// TODO code application logic here

Rectangle r = new Rectangle();
Scanner x = new Scanner(System.in);
System.out.print("Enter Rectangle height: ");
double height = x.nextDouble();
System.out.print("Enter Rectangle width: ");
double width = x.nextDouble();
System.out.print("Enter X Point: ");
double xheight = x.nextDouble();
System.out.print("Enter Y Point: ");
double ywidth = x.nextDouble();
r.contains(p);
p(xheight, ywidth);
}

public boolean contains(Point p) {

double x_ul = upperLeft.getX();
double y_ul = upperLeft.getY();
double x_p = p.getX();
double y_p = p.getY();

if ( (x_ul < x_p && x_p < x_ul + width) &&
(y_ul - height < y_p && y_p < y_ul) ){
}else {
}
}

}```

THANKS A LOT !!!!

2. ## Re: Contains Method HELP PLEASEEEE

The question is unclear.

You're not declare p , how can you use it in these two lines ?

```r.contains(p);
p(xheight, ywidth);```

3. ## Re: Contains Method HELP PLEASEEEE

These two llines are for the point coordinate... is it wrong ? Can you show me the correct code for this question ?

We need to ask the user to enter the rectangle height and width and to ask him the point and we have to know if the point is in the rectangle.

Thanks a lot

4. ## Re: Contains Method HELP PLEASEEEE

What is the usage for xheight and ywidth? Do we need a formula in order to know whether it is inside the rectangle ?

5. ## Re: Contains Method HELP PLEASEEEE

For me xheight and ywidth is the coordinate of the point. I think it is wrong. For this i am sure :

public boolean contains(Point p) {

double x_ul = upperLeft.getX();
double y_ul = upperLeft.getY();
double x_p = p.getX();
double y_p = p.getY();

if ( (x_ul < x_p && x_p < x_ul + width) &&
(y_ul - height < y_p && y_p < y_ul) ){
}else {
}
}

6. ## Re: Contains Method HELP PLEASEEEE

Originally Posted by Mounir
For me xheight and ywidth is the coordinate of the point. I think it is wrong. For this i am sure :

public boolean contains(Point p) {

double x_ul = upperLeft.getX();
double y_ul = upperLeft.getY();
double x_p = p.getX();
double y_p = p.getY();

if ( (x_ul < x_p && x_p < x_ul + width) &&
(y_ul - height < y_p && y_p < y_ul) ){
}else {
}
}
Where you get this from ?

7. ## Re: Contains Method HELP PLEASEEEE

I got it by the teacher

8. ## Re: Contains Method HELP PLEASEEEE

So do you think you can help me with because I'm blocked since this morning...

9. ## Re: Contains Method HELP PLEASEEEE

what is the usage of upperLeft ?

10. ## Re: Contains Method HELP PLEASEEEE

Originally Posted by John Joe
what is the usage of upperLeft ?
Upper left is the upper left corner point. But seriously I don't know why we use it for

11. ## Re: Contains Method HELP PLEASEEEE

The question looked similar to this Writing Own .contains Method For Rectangles - Java | Dream.In.Code

Below is my answer, not sure is it correct

```public class Rectangle {
private Point upperLeft; // the upper left corner point
static private double height; // the height of the rectangle
static private double width; // the width of the rectangle

public static void main(String[] args) {
// TODO code application logic here

Rectangle r = new Rectangle();
Scanner x = new Scanner(System.in);
System.out.print("Enter Rectangle height: ");
height = x.nextDouble();
System.out.print("Enter Rectangle width: ");
width = x.nextDouble();
r.isSquare(height,width);
System.out.print("Enter X Point: ");
double xpoint = x.nextDouble();
System.out.print("Enter Y Point: ");
double ypoint = x.nextDouble();
r.contains(xpoint, ypoint);
}

public boolean contains(double xpoint, double ypoint) {

upperLeft = new Point();
double x_ul = upperLeft.getX();
double y_ul = upperLeft.getY();
double x_p = xpoint;
double y_p =ypoint;

if ( (x_ul < x_p && x_p < x_ul + width) &&
(y_ul - height < y_p && y_p < y_ul) ){
}else {
}
}

public boolean isSquare(double height,double width) {
if (height == width) {
System.out.println(" It is square ");
} else{
System.out.println(" Not a square ");
}
}
}```

Hope it helped you

12. ## Re: Contains Method HELP PLEASEEEE

It works Thanks a lot <3 <3 <3

13. ## The Following User Says Thank You to Mounir For This Useful Post:

John Joe (October 17th, 2017)