Question

1. In IntelliJ create a new project called F1_2 2. In the Project window create a...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Rectangle.java

package F1_2;

public class Rectangle {

   // Instance variables
   private double x; // lower left x
   private double y; // lower left y
   private double length;
   private double width;

   /**
   * Parameterized constructor - creates a rectangle with lower left corner
   * (x,y) and given length and width.
   *
   * @param x
   * - lower left x
   * @param y
   * - lower left y
   * @param length
   * - length of the Rectangle
   * @param width
   * - width of the Rectangle
   */
   public Rectangle(double x, double y, double length, double width) {
       this.x = x;
       this.y = y;
       this.length = length;
       this.width = width;
   }

   /**
   * This method returns the lower left x
   */
   public double getX() {
       return x;
   }

   /**
   * This method returns the lower left y
   */
   public double getY() {
       return y;
   }

   /**
   * This method returns the length of the Rectangle
   */
   public double getLength() {
       return length;
   }

   /**
   * This method returns the width of the Rectangle
   */
   public double getWidth() {
       return width;
   }

   /**
   * This method returns 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.
   *
   * @param a
   * - lower left x/y of this Rectangle
   * @param b
   * - lower right x/y of this Rectangle
   * @param c
   * - lower left x/y of other Rectangle
   * @param d
   * - lower right x/y of other Rectangle
   */
   private static boolean doLinesOverlap(double a, double b, double c, double d) {
       return !((a >= d) || (c >= b));
   }

   /**
   * This 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.
   *
   * @param other
   * - Rectangle reference object
   */
   public boolean overlapsWith(Rectangle other) {
       // Suppose R1 is this Rectangle and R2 is other Rectangle
       // _ Rectangle R1 has lower left corner (x1,y1), length l1, width
       // w1.
       // _ Rectangle R2 has lower left corner (x2,y2), length l2, 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.
       if (doLinesOverlap(this.x, this.x + this.length, other.x, other.x + other.length)
               && doLinesOverlap(this.y, this.y + this.width, other.y, other.y + other.width))
           return true;
       else
           return false;
   }
}

//F1_2.java

package F1_2;

import java.util.Scanner;

public class F1_2 {

   public static void main(String[] args) {
       // Scanner to get user input
       Scanner in = new Scanner(System.in);

       // Display message
       System.out.print("Enter each rectangle in the format\n" + "x y l w\n"
               + "where (x,y) is the lower left corner, l is the length, and w is the width.");

       // Get 1st Rectangle
       System.out.print("\nEnter 1st rectangle: ");

       // Create Rectangle object
       Rectangle r1 = new Rectangle(in.nextDouble(), in.nextDouble(), in.nextDouble(), in.nextDouble());

       // Get 2nd Rectangle
       System.out.print("Enter 2nd rectangle: ");

       // Create Rectangle object
       Rectangle r2 = new Rectangle(in.nextDouble(), in.nextDouble(), in.nextDouble(), in.nextDouble());

       // Check if the rectangle overlaps
       if (r1.overlapsWith(r2))
           System.out.print("The rectangles overlap");
       else
           System.out.print("The rectangles do not overlap");
   }
}

CODE SCREENSHOTS:

SAMPLE OUTPUT:

EXPLANATION FOR doLinesOverlap() method:

You can also check that the above explanation stands true for the y co-ordinate as well

Add a comment
Know the answer?
Add Answer to:
1. In IntelliJ create a new project called F1_2 2. In the Project window create a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

  • In Java programming language. For this assignment, you must write a class Rectangle and a tester...

    In Java programming language. For this assignment, you must write a class Rectangle and a tester RectangleTest. The Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

  • Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

    Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they both say: constructor Shape in class Shape cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length The directions say: The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively a. Implementing constructor with no...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the...

    create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not be modified. Instructions ar egiven below. Shape class The Shape class is an abstract base class from which Rectangle and Circle are derived. bool fits_in(const Rectangle& r) is a pure virtual function that should return true if the Shape fits in the Rectangle r. void draw(void) is a pure virtual function that writes the svg...

  • please do in java and comments the code so i can understand Requirements: Create a Java...

    please do in java and comments the code so i can understand Requirements: Create a Java class named “MyRectangle2D.java”. Your class will have double two variables named x and y. These will represent the center point of your rectangle. Your class will have two double variables named width and height. These will represent the width and height of your rectangle. Create getter and setter methods for x, y, width, and height. Create a “no argument” constructor for your class that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT