Question

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

  • Write a method toString that returns a String. The string should be formatted exactly as: “x:2, y:3, w:4, h:5” without the quotation marks and replacing the numbers with the actual attributes of the object.

There exists a class called Rectangle in Java already. You are not allowed to use this class in any way! Make sure that you are not accidentally importing it!

A few suggestions about tests:

  • You need more than one tests for overlap, because there can be several kinds of overlap. Think about it!

  • Write as many tests as you can think of. But you do not need to conflate many tests into one method: for example, you can write several different methods to test just overlap provided you isolate the objective of each test.

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

Short Summary:

  • Modify the main class rectangle1 and rectangle2 constructor values to produce various output.
  • Shown the pictorial representation as well for the rectangles used in the sample runs

Source Code:

Rectangle.java

import java.util.NoSuchElementException;

/**
* Rectangle Java class
*/
public class Rectangle {
  
//Private member instances to store input values
private int x, y,height,width;
  
/**
* Constructor
* Creates a rectangle using the x, y coordinates of its lower left corner, its width and its height
*/
public Rectangle(int x, int y, int height, int width)
{
//Creating a rectangle with non-positive width or height should not be allowed
if(height > 0)
this.height = height;
if(width > 0)
this.width = width;
//although x and y are allowed to be negative.
this.x = x;
this.y =y;
}
  
/**
* Method: overlap(Rectangle other).
* Returns true if this rectangle overlaps with other, false otherwise.
* Rectangles that touch each other are not considered to be overlappingg.
*/
public boolean overlap(Rectangle other)
{
return (x < other.x + other.width && x + width > other.x &&
y < other.y + other.height && y + height > other.y);
}
  
/**
* Method intersect(Rectangle other).
* Returns a Rectangle object that represents the overlap of the two rectangles.
* throws a NoSuchElementException with a helpful message, If no intersection exists.
*/
public Rectangle intersect(Rectangle other)
{
//Return type object rectangle
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)
{
//Throw the exception
throw ex;
}
return rectangle;
}

/**
* Method union(Rectangle other).
* 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)
{
//find x point minimum
int Xminimum = Math.min(this.x, other.x);
//find y point minimum
int Yminimum = Math.min(this.y, other.y);
//find x point maximum
int Xmaximum = Math.max((this.x + this.height), (other.x + other.height));
//find y point maximum
int Ymaximum = Math.max((this.y + this.width), (other.y + other.width));
int newHeight = Xmaximum - Xminimum;
int newWidth = Ymaximum - Yminimum;
//Draw new rectangle with calculated points
return new Rectangle(Xminimum, Yminimum, newHeight, newWidth);
}

  
/**
* Method: toString() - override
* Returns a String.
* The string should be formatted exactly as: “x:2, y:3, w:4, h:5”
*/
@Override
public String toString() {
return "x:" + this.x +", y:" + y + ", w: " + width + ", h: " + height;
}
}

Main.java - RectangleTest

//Main.java
public class Main {
public static void main(String[] args)
{
//rectangle1
Rectangle rectangle1= new Rectangle(100, 150, 100, 200);
//Print rectangle1
System.out.println("Rectangle1 :");
System.out.println(rectangle1);
  
//rectangle2
Rectangle rectangle2 = new Rectangle(100, 150, 100, 200);
//Print rectangle2
System.out.println("Rectangle2 :");
System.out.println(rectangle2);
  
//Check overlapping
if(rectangle1.overlap(rectangle2))
{
System.out.println("There is overlapping");
}
else
{
System.out.println("There is no overlapping");
}
  
//check Intersect
try {
System.out.println("Intersect: " + rectangle1.intersect(rectangle2));
} catch(Exception e) {
System.out.println("Intersect: " + e.getMessage());
}
  
//Get Union
System.out.println("Union: " + rectangle1.union(rectangle2));
}
}

Refer the following screenshots for code indentation:

Sample Run -1

Sample Run -2:

Sample Run -3: Rectangle 1 == Rectangle 2

Feel free to rate the answer and comment your questions, if you have any.

Happy Studying!!!

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

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

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

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

  • My assignment is to create a rectangle class in java. •Create a MyRectangleClass •There are two...

    My assignment is to create a rectangle class in java. •Create a MyRectangleClass •There are two private instance variables, length l and width w with double data type. •There is a constructor with two parameter •There are 4 public methods which are circumference, area, getWidth, and getLength. •circumference method will return circumference to the caller.(the formula for circumference is 2 *l + 2*w ) •Area method Return the area to the caller(:l*w) •getLength method will return length to the caller....

  • In Java please! Problem You will be using the point class discussed in class to create...

    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 class that tests your rectangle. Requirements You must implement the 3 classes in the class diagram below and use the same naming and data types. Following is a description of what each method does. Point Rectangle RectangleDriver - x: int - top Left: Point + main(args: String[) - y: int - width: int +...

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

  • Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet...

    Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet object holds integers in the range 0-100 Represented by an array of booleans, such that array element a[i] is set to true if integer i is in the set, and false otherwise Create these constructors and methods for the class IntegerSet() public IntegerSet union(IntegerSet iSet) public IntegerSet intersection(IntegerSet iSet) public IntegerSet insertElement(int data) public IntegerSet deleteElement(int data) public boolean isEqualTo(IntegerSet iSet) public String toString()...

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

  • What if you had to write a program that would keep track of a list of...

    What if you had to write a program that would keep track of a list of rectangles? This might be for a house painter to use in calculating square footage of walls that need paint, or for an advertising agency to keep track of the space available on billboards. The first step would be to define a class where one object represents one rectangle's length and width. Once we have class Rectangle, then we can make as many objects of...

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