I have to add the following to my class method but have no idea how to do that p
ID: 3846563 • Letter: I
Question
I have to add the following to my class method but have no idea how to do that properly:
- Add the following to your constructor class:
public rectangle(Point p, int width, int height)
- Add the following accessor method to your rectangle class:
publioc boolean contains (int x, int y)
public boolean contains(Point p)
Returns whether the given Point or coordinate lie inside the bounds of the rectangle
- Add the following method to your rectangle class:
public rectangle union(rectangle rect)
Returns a new rectangle that represents the area occupied by the tightest bounding box that contains both this rectangle and given other rectangle.
HERE IS THE CODE I CURRENTLY HAVE.
import java.awt.Point;
public class rectangle {
private int x;
private int y;
private int width;
private int height;
private int Point;
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(p.getX(), p.getY(), width, 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 + " ]";
}
}
Explanation / Answer
import java.awt.*;
public class Rectangle {
private int x;
private int y;
private int width;
private int height;
private Point pp;
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.pp=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 = " + pp.x + ", Y = " +pp.y + ", Width = " + width + ", Height = " + height + " ]";
}
public boolean contains(int x,int y){
if(x<pp.getX()&&y<pp.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 r){
int ht,wt;
ht=r.getHeight()+(int)pp.getX();
wt=r.getWidth()+(int)pp.getY();
Rectangle rb=new Rectangle((int)pp.getX(),(int)pp.getY(),ht,wt);
rb.height=ht;
rb.width=wt;
return rb;
}
}
allmain.java
import java.util.*;
import java.awt.*;
public class allmain {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
Rectangle rr=new Rectangle(2,3,12,22);
Point p=new Point(3,4);
Rectangle r2=new Rectangle(p,3,5);
System.out.println(r2.tostringp());
System.out.println(rr.tostring());
boolean yes=r2.contains(rr.getX(), rr.getY());
System.out.println("does it contains:"+yes);
Rectangle newrectangle=r2.union(rr);
System.out.println(newrectangle.tostring());
}
}
output:
Rectangle[ X = 3, Y = 4, Width = 3, Height = 5 ]
Rectangle[ X = 2, Y = 3, Width = 12, Height = 22 ]
does it contains:true
Rectangle[ X = 3, Y = 4, Width = 16, Height = 25 ]