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...
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...
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...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
I would like to compare my current code with that of someone else to be sure...
I would like to compare my current code with that of someone else to be sure I did this assignment correctly. My main concern is with Problems 2 and 4. I sometimes struggle with testing the methods I have created. If you can, please answer all of the questions so that I may reference them if I am still lost on Problems 2 and 4. Each problem requires the one before it and so seeing the entire assignment as opposed...
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT