Question
What is wrong with my code at the bottom (union and rec1) ALSO: could someone help me add the following method to the program: public
What is wrong with my code at the bottom (union and rec1)
ALSO: could someone help me add the following method to the program:
public rectangle intersection(rectangle rect)
Return a new rectangle that represents the largest rectangular region completely contained within both this rectangle and the given other rectangle. If the rectangles do not intersect at all, returns a rectangle with both with and height equal to 0.
HERE IS MY CODE:
import java.awt.Point;
public class rectangle {
private int x;
private int y;
private int width;
private int height;
private Point pm;
public rectangle(int x, int y, int width, int height){
if(width < 0 || height < 0){
throw new IllegalArgumentException();
}
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public rectangle(Point p, int width, int height) {
this.pm = p;
this.width = width;
this.height = height;
}
public int getHeight(){
return this.height;
}
public int getWidth(){
return this.width;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public String toString(){
return "Rectangle[ X = " + x + ", Y = " + y + ", Width = " + width + ", Height = " + height + " ]";
}
public String toStringp(){
return "Rectangle[ X = " + pm.x + ", Y = " +pm.y + ", Width = " + width + ", Height = " + height + " ]";
}
public boolean contains(int x,int y){
if(x return true; else return false; } public boolean contains(Point p){ if(p.getX() < x && p.getY() < y){ return true; } else{ return false; } public rectangle union(Rectangle rect){ int hei1, wid1; hei1 = rect.getHeight() + (int)pm.getX(); wid1 = rect.getWidth() + (int)pm.getY(); rectangle rec1 = new rectangle((int)pm.getX(), (int)pm.getY(), hei1, wid1); rec1.height = hei1; rec1.width = wid1; return rec1; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started