Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. In IntelliJ create a new project called F1_2 2. In the Project window create a new Java package called F1_2. This can be done

1. In IntelliJ create a new project called F1_2

2. In the Project window create a new Java package called F1_2. This can be done by right clicking on src and

going to New ! Package.

3. In package lab1 2 create a class called Rectangle. This can be done by right clicking on the package and going

to New ! Java Class

4. At the beginning of Rectangle.java add the line

package lab1 2;

5. In Rectangle.java create a class called Rectangle with data fields for storing the lower left x coordinate, lower left y coordinate, length and width of a rectangle. These should be private data fields of type double.

6. In class Rectangle add and implement the following public methods:

// Constructor for creating a rectangle with lower left corner (x,y)

// and given length and width.

public Rectangle(double x, double y, double length, double width)

// get method for the lower left x coordinate

public double getX()

// get method for the lower left y coordinate

public double getY()

// get method for the length

public double getLength()

// get method for the width

public double getWidth()

// The following method return true if and only if the line segment

// from a to b overlaps with the line segment from c to d on the real line.

// Touching only at one point is not considered overlapping.

// This method is to be called by method overlapsWith to help compute if

// two rectangles overlap.

private static boolean doLinesOverlap(double a, double b, double c, double d)

// The following method tests if two Rectangle objects overlap.

// This method returns true if and only if the invoking object (the object

// referenced by the keyword this) overlaps with Rectangle other.

// Touching only at a corner or only on an edge is not considered overlapping.

// See section below on how to test if two rectangles overlap.

public boolean overlapsWith(Rectangle other)

Suppose we have the following 2 rectangles:

_ Rectangle R1 has lower left corner (x1; y1), length l1 and width w1.

_ Rectangle R2 has lower left corner (x2; y2), length l2 and width w2.

To compute if R1 overlaps with R2 check that the following is true:

_ The line segment from x1 to x1 + l1 overlaps with the line segment from x2 to x2 + l2, and

_ the line segment from y1 to y1 + w1 overlaps with the line segment from y2 to y2 + w2.

Use method doLinesOverlap to test if two line segments overlap.

7. Add another class to package F1_2 called F1_2 (in its own file). In here create a main method.

8. In the main method of class F1_2 prompt the user for two rectangles, each with an lower left x and y coordinate, length, and width. Use this input to create two different Rectangle object. Next, call method overlapsWith to test if these Rectangle objects overlap. Finally, display the result to the screen.

9. When you are done, zip up your project in a file called F1_2.zip and upload it into your cs111 folder on the server for grading (see Part 3).

Sample runs of F1_2:

Enter each rectangle in the format

x y l w

where (x,y) is the lower left corner, l is the length, and w is the width.

Enter 1st rectangle: 2.5 5 4.5 3

Enter 2nd rectangle: 6 7.1 2 6.4

The rectangles overlap

Enter each rectangle in the format

x y l w

where (x,y) is the lower left corner, l is the length, and w is the width.

Enter 1st rectangle: 2.5 5 4.5 3

Enter 2nd rectangle: 6 8 2 5.5

The rectangles do not overlap

Enter each rectangle in the format

x y l w

where (x,y) is the lower left corner, l is the length, and w is the width.

Enter 1st rectangle: 4 1 3 2

Enter 2nd rectangle: 1.5 2.5 6 1

The rectangles overlap

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions