What is wrong with my code at the bottom (union and rec1) ALSO: could someone he
ID: 3846610 • Letter: W
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 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<pm.getX()&&y<pm.getY())
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;
}
}
}
Explanation / Answer
package sample1;
import java.awt.Point;
public class Rectangle {
private int x;
private int y;
private double width;
private double height;
private Point pm;
public Rectangle(int x, int y, double width,double height){
if(width < 0 || height < 0){
throw new IllegalArgumentException();
}
pm.x = x;
pm.y = y;
this.width = width;
this.height = height;
}
public Rectangle(Point p, double width, double height) {
this.pm = p;
this.width = width;
this.height = height;
}
public double getHeight(){
return this.height;
}
public double getWidth(){
return this.width;
}
public int getX(){
return this.pm.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<pm.getX()&&y<pm.getY())
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)
{
double 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;
}
public Rectangle intersection(Rectangle rect){
double hei1, wid1;
hei1 = Math.abs(rect.getHeight() - (int)pm.getX());
wid1 = Math.abs(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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.