In: Computer Science
I have a program to code for my computer science class, and Im not sure how to do it. If someone can explain it step by step I would appreciate it.
Problem Description:
Define the GeometricObject2D class that contains the properties color and filled and their appropriate getter and setter methods. This class also contains the dateCreated property and the getDateCreated() and toString() methods. The toString() method returns a string representation of the object.
Define the Rectangle2D class that extends the GeometricObject2D class and also contains:
Two double data fields named x and y that specify the lower left corner of the rectangle with get
methods.
A data field width with a get method.
A data field height with a get method.
A no-arg constructor that creates a default rectangle with (0, 0) for (x, y), 1 for width and 1 for
height.
A constructor that creates a rectangle with the specified x, y, width and height.
A method getArea() that returns the area of the rectangle.
A method getPerimeter() that returns the perimeter of the rectangle.
A method contains(double x, double y) that returns true if the specified point (x, y) is inside this
rectangle. See Figure below.
A method contains(Rectangle 2D rectangle) that returns true if the specified rectangle is inside
this rectangle. See Figure.
A method overlaps(Rectangle 2D rectangle) that returns true if the specified rectangle overlaps
with this rectangle. See the figure below.
(a) (b) (c)
Figure
(a) A point is inside the rectangle. (b) A rectangle is inside another rectangle. (c) A rectangle overlaps another rectangle.
Draw the UML diagram for the class before implement the classes. Write a program that reads from the keyboard the lower left corner’s x and y coordinates, width and height of two rectangles. The program will then displays true if rectangle 1 contains rectangle 2 or false rectangle 1 does not contain rectangle 2.
Note: Run and make sure that your program produces exactly the same output for each of the sample inputs listed below.
Sample Input 1:
1 3.4 2 5
2 2.5 3 6
Sample Output 1: false
Sample Input 2:
1 1 6.1 7
2 323
Sample Output 2: true
Sample Input 3:
10 5 5 4.2
12 6.2 2 1.2
Sample Output 3: true
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// GeometricObject2D.java
import java.util.Date;
public class GeometricObject2D {
private String color;
private boolean filled;
private Date dateCreated;
public GeometricObject2D() {
this.dateCreated = new
Date();
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public Date getDateCreated() {
return dateCreated;
}
}
_________________________
// Rectangle2D.java
public class Rectangle2D extends GeometricObject2D {
private double x;
private double y;
private double width;
private double height;
public Rectangle2D(double x, double y, double
width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public Rectangle2D() {
this.x = 0;
this.y = 0;
this.width = 1;
this.height = 1;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public boolean contains(double x, double y) {
return
Math.abs(x-this.x)<=width/2 &&
Math.abs(y-this.y)<=height/2;
}
private double getDistance(double p1, double p2)
{
return Math.sqrt(Math.pow(p2 - p1,
2));
}
public boolean contains(Rectangle2D r) {
return
contains(r.x-r.width/2,r.y+r.height/2) &&
contains(r.x-r.width/2,r.y-r.height/2)
&&
contains(r.x+r.width/2,r.y+r.height/2)
&&
contains(r.x+r.width/2,r.y-r.height/2);
}
public boolean overlaps(Rectangle2D r) {
return !contains(r)
&& ((getX() + width / 2 > r.getX() -
r.getWidth()) || (getY()
+ height /
2 > r.getY() - r.getHeight()))
&& (getDistance(getY(), r.getY()) <
height / 2 + r.getHeight()/ 2)
&& (getDistance(getX(), r.getX()) <
width / 2 + r.getWidth()/ 2);
}
}
____________________________
// Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
double x,y;
double width,height;
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
x=sc.nextDouble();
y=sc.nextDouble();
width=sc.nextDouble();
height=sc.nextDouble();
Rectangle2D rect1=new
Rectangle2D(x, y, width, height);
x=sc.nextDouble();
y=sc.nextDouble();
width=sc.nextDouble();
height=sc.nextDouble();
Rectangle2D rect2=new
Rectangle2D(x, y, width, height);
System.out.println(rect1.contains(rect2));
}
}
_____________________________
// Output#1
1 3.4 2 5
2 2.5 3 6
false
_____________________
// Output#2:
1 1 6.1 7
2 3 2 3
true
____________________Thank You