Question

In Java, write a class Rectangle. This Rectangle class should have only the following public methods...

In Java, write a class Rectangle. This Rectangle class should have only the following public methods (you can add other non-public methods):

  • Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative.

  • Write a method overlap(Rectangle other). This method should return true if this rectangle overlaps with other, false otherwise. Rectangles that touch each other are not considered to be overlapping.

  • Write a method intersect(Rectangle other). This method should return a Rectangle object that represents the overlap of the two rectangles. If no intersection exists, it should throw a NoSuchElementException with a helpful message.

  • Write a method union(Rectangle other). This method returns a Rectangle object that represents the union of this rectangle and the other rectangle. The union is the smallest rectangle that contains both rectangles. Note that unlike the intersection, the union always exists.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.NoSuchElementException;

public class Rectangle {
    private int x,y;
    private int height,width;
    /**
     *  creates a rectangle using the x, y coordinates
     *  of its lower left corner, its width and its height
     *  in that order. Creating a rectangle with non-positive
     *  width or height
     *  should not be allowed,
     *  although x and y are allowed to be negative.

     */
    public Rectangle(int x,int y,int height, int width)
    {
        if(height>0)
            this.height= height;
        if(width>0)
            this.width= width;
        this.x= x;
        this.y=y;
    }
    /**
     *  a method overlap(Rectangle other).
     *  This method should return true if this rectangle overlaps
     *  with other, false otherwise. Rectangles
     *  that touch each other are not considered to be overlapping.
     */
    public boolean overlap(Rectangle other)
    {
        return (x < other.x + other.width && x + width > other.x && y < other.y + other.height && y + height > other.y);
    }
    /**
     * a method intersect(Rectangle other).
     * This method should return a Rectangle object that
     * represents the overlap of the two rectangles.
     * If no intersection exists,
     * it should throw a NoSuchElementException .
     */
    public Rectangle intersect(Rectangle other)
    {
        Rectangle rectangle=null;
        //finding new coordinates
        int x2 = this.x+ this.width;
        int x4 = other.x + other.width;
        int y1 = this.y - this.height;
        int y3 = other.y - other.height;
try {
        // find intersection:
    int xLeft = Math.max(this.x, other.x);
    int xRight = Math.min(x2, x4);
    if (xRight <= xLeft)
        throw new NoSuchElementException("No Rectangle intersect.");
    else {
        int yTop = Math.max(y1, y3);
        int yBottom = Math.min(this.y, other.y);
        if (yBottom <= yTop)
            throw new NoSuchElementException("No Rectangle intersect.");
        else
            rectangle= new Rectangle(xLeft, yBottom, xRight - xLeft, yBottom - yTop);
        }
    }
catch (NoSuchElementException ex)
{
    System.out.println(ex);
}
return rectangle;


    }
    /**

     *      * a method union(Rectangle other).
     *      * This method returns a Rectangle object that
     *      * represents the union of this rectangle and the
     *      * other rectangle. The union is the smallest rectangle
     *      * that contains both rectangles.
     *      * Note that unlike the intersection, the union always exists.
     *      *
     *
     */
      public Rectangle union(Rectangle other)
     {
     int Xmin=Math.min(this.x,other.x);
     int Ymin=Math.min(this.y,other.y);
     int Xmax=Math.max((this.x+this.height),(other.x+other.height));
     int Ymax=Math.max((this.y+this.width),(other.y+other.width));
     int newHeight=Xmax-Xmin;
     int newWidth=Ymax-Ymin;
     return new Rectangle(Xmin,Ymin,newHeight,newWidth);
     }


    @Override
    public String toString() {
        return "coordinates of lower left corner ( "+x+","+y+" ) and height = "+height+" width: "+ width;
    }
}






public class Main {
    public static void main(String[] args)
    {
        //Create objects Of Rectangle
        Rectangle rectangle1= new Rectangle(100,150,20,30);
        Rectangle rectangle2 = new Rectangle(200,150,10,20);

        if(rectangle1.overlap(rectangle2))
        {
            System.out.println("Yes overlapping..........");
        }
        else
            System.out.println("No overlapping .............");
        System.out.println(rectangle1.union(rectangle2));

        System.out.println(rectangle1.intersect(rectangle2));
    }
}

//output

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

Add a comment
Know the answer?
Add Answer to:
In Java, write a class Rectangle. This Rectangle class should have only the following public methods...
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
  • In Java programming language. For this assignment, you must write a class Rectangle and a tester...

    In Java programming language. For this assignment, you must write a class Rectangle and a tester RectangleTest. The Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write...

  • 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...

  • 1. In IntelliJ create a new project called F1_2 2. In the Project window create a...

    1. In IntelliJ create a new project called F1_2 2. In the Project window create a new Java package called F1_2. This can be done by right clicking on src and going to New ! Package. 3. In package lab1 2 create a class called Rectangle. This can be done by right clicking on the package and going to New ! Java Class 4. At the beginning of Rectangle.java add the line package lab1 2; 5. In Rectangle.java create a...

  • JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects...

    JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following fields and methods: int width, int height Where width and height are the dimensions of the rectangle. Write accessor methods (i.e. getWidth(), getHeight()) that retrieve the values stored in the fields above. And mutator methods (i.e. setWidthl), setHeight() ) that change the field's values. Finally create a method called getAreal) that returns the area of the rectangle. Like we did...

  • Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...

    Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: · Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. · A no-arg constructor that creates a default rectangle. · A constructor that creates a...

  • Write a class definition for a Rectangle. The data fields should be: • An integer for...

    Write a class definition for a Rectangle. The data fields should be: • An integer for the value of the width of the Rectangle • An integer for the value of the height of the Rectangle An integer for the value of the area of the Rectangle It must have: . A default constructor A constructor that has a parameters for width and height and assigns them to the member variables. • The class should have mutators for all of...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Making a rectangle class in java write a simple class that will represent a rectangle. Later...

    Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Consider the following declaration for a class that will be used to represent rectangles. public class...

    Consider the following declaration for a class that will be used to represent rectangles. public class Rectangle { private double height; private double width; public Rectangle() { height = 2.0; width = 1.0; } public Rectangle(double w, double h) { height = h; width = w; } public double getHeight() { return height; } public double getWidth() { return width; } public void setHeight(double h) { height = h; } public void setWidth(double w) { width = w; } //Other...

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