In: Computer Science
Need some assistance with a java program “Square,” that will implement a data type Square that represents squares with the x and y coordinates of their upper-left corners and the length.
The API should be as follows.
Square(double x, double y, double length) //constructor
double area() //returns the area of the square
double perimeter() //returns the perimeter of the square
boolean intersects(Square b) //does this square intersect b? Two squares would intersect if they share one or more common points
boolean contains(Square b) //does this square contain b?
void draw() //draw this square on standard drawing
Note: The program should include a main method to test that it does the following.
Instantiate a Square object whose upper-left corner coordinates and length are given as command-line arguments. It should print out the area and perimeter of the square.
Prompt the user for a second square’s upper-left corner coordinates and length, and indicate whether it intersects with the square specified earlier and also whether it contains the square specified earlier.
Provide a pop-up window that displays the two squares.
A sample run would be as follows.
>java Square 0.2 0.7 0.3
The area is 0.09
The perimeter is 1.2
Enter the upper-left coordinates and the length of a square: 0.3 0.6 0.4
import java.util.Scanner;
public class Square {
private double x;
private double y;
private double length;
public Square(double x, double y, double length) {
this.x = x;
this.y = y;
this.length = length;
}
public double area() {
return length * length;
}
public double perimeter() {
return 4 * length;
}
public boolean intersects(Square other) {
// Two Square do not overlap, if one rectangle is on left of other, or on
// top of other.
// If one Square is on left side of other
if ((this.x >= (other.x + other.length)) || (other.x >= (this.x + this.length))) {
return false;
}
// If one Square is above other
if ((this.y >= (other.y + other.length)) || (other.y >= (this.length + this.y))) {
return false;
}
return true;
}
public boolean contains(Square b) {
return (this.x <= b.x) && (this.x + length < b.x + b.length)
&& (this.y <= b.y) && (this.y + length < b.y + b.length);
}
public void draw() {
System.out.println("The area is " + area());
System.out.println("The perimeter is " + perimeter());
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Double x = Double.parseDouble(args[0]);
Double y = Double.parseDouble(args[1]);
Double l = Double.parseDouble(args[2]);
Square s = new Square(x, y, l);
s.draw();
System.out.println("Enter the upper-left coordinates and the length of a square: ");
x = Double.parseDouble(in.nextLine());
y = Double.parseDouble(in.nextLine());
l = Double.parseDouble(in.nextLine());
Square r = new Square(x, y, l);
r.draw();
if(s.intersects(r)) {
System.out.println("Squares intersect.");
} else {
System.out.println("Squares do not intersect.");
}
if(s.contains(r)) {
System.out.println("the new Squares is inside old square.");
} else {
System.out.println("the new Squares is not contained in old square.");
}
in.close();
}
}
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.