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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Contains Method HELP PLEASEEEE

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Angry 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) {
     
            boolean answer;
            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) ){
            answer = true;
            System.out.print("The Answer is " + answer);
        }else {
            answer = false;
            System.out.print("The Answer is " + answer); 
        }
            return answer;
        }
     
    }




    THANKS A LOT !!!!

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default 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);
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default 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 ?
    Whatever you are, be a good one

  5. #5
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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) {

    boolean answer;
    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) ){
    answer = true;
    System.out.print("The Answer is " + answer);
    }else {
    answer = false;
    System.out.print("The Answer is " + answer);
    }
    return answer;
    }

  6. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Contains Method HELP PLEASEEEE

    Quote Originally Posted by Mounir View Post
    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) {

    boolean answer;
    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) ){
    answer = true;
    System.out.print("The Answer is " + answer);
    }else {
    answer = false;
    System.out.print("The Answer is " + answer);
    }
    return answer;
    }
    Where you get this from ?
    Whatever you are, be a good one

  7. #7
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Contains Method HELP PLEASEEEE

    I got it by the teacher

  8. #8
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Contains Method HELP PLEASEEEE

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

  9. #9
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Contains Method HELP PLEASEEEE

    what is the usage of upperLeft ?
    Whatever you are, be a good one

  10. #10
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Contains Method HELP PLEASEEEE

    Quote Originally Posted by John Joe View Post
    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. #11
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default 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
        static boolean answer;
     
        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) {
     
            boolean answer;
            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) ){
                answer = true;
                System.out.print("The Answer is " + answer);
            }else {
                answer = false;
                System.out.print("The Answer is " + answer);
            }
            return answer;
        }
     
        public boolean isSquare(double height,double width) {
            if (height == width) {
                System.out.println(" It is square ");
            } else{
                System.out.println(" Not a square ");
            }
            return answer;
        }
    }

    Hope it helped you
    Last edited by John Joe; October 16th, 2017 at 10:34 PM.
    Whatever you are, be a good one

  12. #12
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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)

Similar Threads

  1. Replies: 4
    Last Post: October 2nd, 2014, 06:26 AM
  2. Replies: 1
    Last Post: May 27th, 2014, 07:39 PM
  3. Replies: 2
    Last Post: May 27th, 2014, 12:36 PM
  4. Replies: 6
    Last Post: December 29th, 2013, 02:34 PM
  5. Replies: 1
    Last Post: January 23rd, 2013, 06:29 AM

Tags for this Thread