Question

In: Computer Science

I have a program to code for my computer science class, and Im not sure how...

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:

  1. Two double data fields named x and y that specify the lower left corner of the rectangle with get

    methods.

  2. A data field width with a get method.

  3. A data field height with a get method.

  4. A no-arg constructor that creates a default rectangle with (0, 0) for (x, y), 1 for width and 1 for

    height.

  5. A constructor that creates a rectangle with the specified x, y, width and height.

  6. A method getArea() that returns the area of the rectangle.

  7. A method getPerimeter() that returns the perimeter of the rectangle.

  8. A method contains(double x, double y) that returns true if the specified point (x, y) is inside this

    rectangle. See Figure below.

  9. A method contains(Rectangle 2D rectangle) that returns true if the specified rectangle is inside

    this rectangle. See Figure.

  10. 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

Solutions

Expert Solution

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


Related Solutions

I get errors in my merge method and Im not sure how to fix it to...
I get errors in my merge method and Im not sure how to fix it to make the code work public class OrderedApp1 { public static void main(String[] args) {    int maxSize = 100; // array size int searchKey = 55; OrdArray1 arr, a1, a2, a3; // reference to arrays arr = new OrdArray1(maxSize); // create the arrays a1 = new OrdArray1(maxSize); a2 = new OrdArray1(maxSize); a3 = new OrdArray1(maxSize);    //int a3[] =new int [ a1.length + a2.length];...
This is my presentation/ research topic, Professionals conduct in Computer Science. I have to give a...
This is my presentation/ research topic, Professionals conduct in Computer Science. I have to give a 10 minutes presentation about it but I dont understand this topic and really dont know what to talk about to make it interesting for the class. I have to talk about what are the problems, questions, my assumption, point of view, and conclusion about the topic, which I am very confused. Im really scared of doing public speaking buy will try my best.
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
( In R / R studio ) im not sure how to share my data set,...
( In R / R studio ) im not sure how to share my data set, but below is the title of my data set and the 12 columns of my data set. Please answer as best you can wheather its pseudo code, partial answers, or just a suggestion on how i can in to answer the question. thanks #---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The dataset incovid_sd_20201001.RDatacontains several variables related to infections of covid-19 for eachzip code in San Diego County as of October...
My question is on this program I have difficulty i marked by ??? public class SortedSet...
My question is on this program I have difficulty i marked by ??? public class SortedSet { private Player[] players; private int count; public SortedSet() { this.players = new Player[10]; } /** * Adds the given player to the set in sorted order. Does not add * the player if the case-insensitive name already exists in the set. * Calls growArray() if the addition of the player will exceed the size * of the current array. Uses an insertion sort...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
I am unsure as to why I cannot solve my Computer Science question I was just...
I am unsure as to why I cannot solve my Computer Science question I was just wondering if someone could show me exactly what they would do the code is in C++: write a program that display the following manual for user to chose among calculations Please select one of the following: addition subtraction multiplication division exit The program will then read in the user entry. when the user chose "1", it will ask the user "Please enter two numbers...
I am working on a project for my Computer Science course. I am trying to create...
I am working on a project for my Computer Science course. I am trying to create a Battleship game where a user names two coordinates on a grid and is then told whether this results in a "Hit" or a "Miss". Once the ship has been hit a certain number of times (based on the size of the ship) the ship is sunk. I have written all the code but it now fails to execute when I try to run...
Hi, Im doing my final project for quanatative analysis class. I was tasked to create a...
Hi, Im doing my final project for quanatative analysis class. I was tasked to create a regression analysis on Does the number of probowl player have bearing on becoming all pro. I ran the regression for the following data and got this output from Megastat but I'm not sure what it tells me. Regression Analysis r² 0.006 n   239 r   0.076 k   1 Std. Error   0.494 Dep. Var. All Pro ANOVA table Source SS   df   MS F p-value Regression 0.3397...
I know how to do this with arrays, but I have trouble moving my code to...
I know how to do this with arrays, but I have trouble moving my code to use with linked lists Write a C program that will deal with reservations for a single night in a hotel with 3 rooms, numbered 1 to 3. It must use an infinite loop to read commands from the keyboard and quit the program (return) when a quit command is entered. Use a switch statement to choose the code to execute for a valid command....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT