Could someone make a main class to test out my class methods? import java.awt.Po
ID: 3846625 • Letter: C
Question
Could someone make a main class to test out my class methods?
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;
}
}
Explanation / Answer
Your Rectangle class has some issues. So I fixed them.
PROGRAM CODE:
Rectangle.java
import java.awt.Point;
public class Rectangle {
// These are not required since point instance is available
// 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 = new Point(x, y);
// point instance have to be initialized
//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(){
//this should return y from point
return pm.y;
// return this.y;
}
public String toString(){
return "Rectangle[ X = " + pm.x + ", Y = " + pm.y + ", Width = " + width + ", Height = " + height + " ]";
}
// this method is not required
/* 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() < pm.x && p.getY() < pm.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);
// these two lines are not required since they are already passed to the constructor above
// 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);
// these two lines are not required since they are already passed to the constructor above
// rec1.height = hei1;
// rec1.width = wid1;
return rec1;
}
}
RectangleTester.java
import java.awt.Point;
public class RectangleTester {
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle(10, 10, 20, 30);
System.out.println(rectangle1);
System.out.println(rectangle1.intersection(new Rectangle(new Point(5, 5), 15, 12)));
System.out.println(rectangle1.union(new Rectangle(new Point(5, 5), 15, 12)));
System.out.println(rectangle1.contains(5, 5) == true? "Yes, (5,5) is present in " + rectangle1 : "Oops, this is wrong for " + rectangle1 );
System.out.println(rectangle1.contains(10, 12) == true? "Oops, this is wrong for " + rectangle1 : "No, (10,12) is not present in " + rectangle1 );
}
}
OUTPUT:
Rectangle[ X = 10, Y = 10, Width = 20.0, Height = 30.0 ]
Rectangle[ X = 10, Y = 10, Width = 2.0, Height = 5.0 ]
Rectangle[ X = 10, Y = 10, Width = 22.0, Height = 25.0 ]
Yes, (5,5) is present in Rectangle[ X = 10, Y = 10, Width = 20.0, Height = 30.0 ]
No, (10,12) is not present in Rectangle[ X = 10, Y = 10, Width = 20.0, Height = 30.0 ]