Question

In Java please!

Problem You will be using the point class discussed in class to create a Rectangle class. You also need to have a driver clas• Accessors: getHeight() and getWidth() return the height and width respectively. The getX() and getY() methods will need toSample Output Testing constructors... Rectangle a1 = new Rectangle(5, 12, 4, 6); Rectangle a 2 = new Rectangle(6, 8,5, 7); Re

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java code

public class Point {
    private int x;
    private int y;
    public Point()
    {
        x=0;
        y=0;
    }

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    //getter and setter

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}

//=============================//

public class Rectangle {
    private Point topLeft;
    private int width;
    private int height;
    public Rectangle(int x, int y, int width, int height)
    {
        topLeft = new Point(x,y);
        this.width = width;
        this.height = height;
    }

    public Rectangle(Point topLeft, int width, int height) {
        this.topLeft = topLeft;
        this.width = width;
        this.height = height;
    }
    //getters and setters

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
    public int getX()
    {
        return topLeft.getX();
    }
    public int getY()
    {
        return topLeft.getY();
    }
    public void setX(int x)
    {
        topLeft.setX(x);
    }
    public void setY(int y)
    {
        topLeft.setY(y);
    }
    public boolean contains(int x , int y)
    {
        int endpoint_x = x+topLeft.getX();
        int endpoint_y =x+topLeft.getY();
        return ((x>=topLeft.getX() && x<=endpoint_x)&&(y>=topLeft.getY() && y<=endpoint_y));

    }
    public boolean contains(Point p)
    {
        return contains(p.getX(),p.getY());
    }
    public Rectangle union (Rectangle rect)
    {
        int newX = Math.min(this.getX(), rect.getX());
        int newY = Math.min(this.getY(), rect.getY());
        int newWidth = Math.max(rect.getX() + rect.getWidth() - newX,
                this.getX() + this.getWidth() - newX);
        int newHeight = Math.max(rect.getY() + rect.getHeight() - newY,
                this.getY() + this.getHeight() - newY);
        return new Rectangle(newX, newY, newWidth, newHeight);
    }

    @Override
    public String toString() {
        return "Rectangle[x = "+getX()+", y = "+getY()+", width = "+width+", Height = "+height+"]";
    }
}

//==========================//

public class RectangleDriver {
    public static void main(String[] args)
    {
        runTests();
    }
    public static void runTests()
    {
        Rectangle a1 = new Rectangle(5,12,4,6);
        Rectangle a2 = new Rectangle(6,8,5,7);
        Rectangle b1 = new Rectangle(new Point(14,9),3,3);
        Rectangle b2 = new Rectangle(new Point(10,3),5,8);
        Rectangle c1 = new Rectangle(14,9,3,3);
        Rectangle c2 = new Rectangle(b1.getX(), b1.getY(), b1.getWidth(), b1.getHeight());

        //Testing toString()
        System.out.println("Testing toString()");
        System.out.println(a1);
        System.out.println(a2);
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(c1);
        System.out.println(c2);
        //Testing union
        System.out.println("Testing Union");
        System.out.println(a1.union(a2));
        System.out.println(b1.union(b2));
        System.out.println(c1.union(c2));
        //Confirming rectangle didn't change
        System.out.println("Confirming rectangle didn't change");
        System.out.println(a1);
        System.out.println(a2);
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(c1);
        System.out.println(c2);
        //creating points
        System.out.println("creating points");
        Point p1 = new Point(5,11);
        Point p2 = new Point(7,14);
        //Testing contains
        System.out.println("Testing contains");
        System.out.println(a1.contains(p1));
        System.out.println(a1.union(a2).contains(p1));
        System.out.println(a1.contains(p2));
        System.out.println(b1.contains(10,10));
        System.out.println(b2.contains(10,10));
    }
}

//Output

C: \Program Files\Java\jdk1.8.0_221\bin\java .exe Testing toString ( ) Rectangle [x = 5, y = 12, width = 4, Height 6] Recta

//If you need any help regarding this solution ........ please leave a comment...... thanks

Add a comment
Know the answer?
Add Answer to:
In Java please! Problem You will be using the point class discussed in class to create...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named...

    java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named "MyRectangle3d" Your class must have the following attributes or variables · x (the x coordinate of the rectangle, an integer) * y (the y coordinate of the rectangle, an integer) * length (the length of the rectangle, an integer) * width (the width of the rectangle, an integer) * height (the height of the rectangle, an integer) * color( the color of the rectangle,...

  • I need help with my java code i am having a hard time compiling it //...

    I need help with my java code i am having a hard time compiling it // Cristian Benitez import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JPanel; Face class public class FaceDraw{ int width; int height; int x; int y; String smileStatus; //default constructor public FaceDraw(){ } // Getters and Setters for width,height,x,y public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public int getHeight(){ return height; } public void setHeight(int height){ this.height=height; } public int getX(){ return...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • please do in java and comments the code so i can understand Requirements: Create a Java...

    please do in java and comments the code so i can understand Requirements: Create a Java class named “MyRectangle2D.java”. Your class will have double two variables named x and y. These will represent the center point of your rectangle. Your class will have two double variables named width and height. These will represent the width and height of your rectangle. Create getter and setter methods for x, y, width, and height. Create a “no argument” constructor for your class that...

  • Java homework Imagine that you have a Point class, like the one that we used in...

    Java homework Imagine that you have a Point class, like the one that we used in the improved Circle class. It has the following constructors:    Point(double x, double y)    Point(Point p) And the following methods:    public double getX()    public double getY()    public void setX(double xValue)    public void setY(double yValue)    public double distance(Point p) For this question, write statements that create an array of three Point references. Then initialize each element with a Point object that has with x and...

  • I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...

    I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class. I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width,...

  • What is wrong with the following code: class Point { private : int x, y; public...

    What is wrong with the following code: class Point { private : int x, y; public : Point (int u, int v) : x(u), y(v) {} int getX () { return x; } int getY () { return y; } void setX (int newX ) const { x = newX ; } }; int main () { Point p(5, 3); p.setX (9001) ; cout << p. getX () << ’ ’ << p. getY (); return 0; }

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • Using Python 3: Create a point p1 of coordinates (0; 0) and un point p2 of...

    Using Python 3: Create a point p1 of coordinates (0; 0) and un point p2 of coordinates (1; 2). Print out the coordinates of the two points on the same line, by calling toString on the two points. Print the result of applying the method equals on point p1, using p2 as argument. Set the x coordinate of p2 equal to the x coordinate of p1, using the methods setX and getX. Set the y coordinate of p2 equal to...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT