Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please just start writing the rectangle2D class, Thanks! you dont need to finish it, just write the first few lines using point as a data

Please just start writing the rectangle2D class, Thanks! you dont need to finish it, just write the first few lines using point as a data type.

1. Write class Rectangle2D that derives from Rectangle. It adds a Point data member (x,y) to store the coordinates of the upper left corner of the rectangle. Point offers constructors, accessors, modifiers and toString methods to report the point in (x,y) notation. Add Rectangle2D constructors, accessors, modifiers, and a toString method that reports the upper left coordinates, width, and height. Add the following methods:

public Boolean contains (Point other)

// POST: return true if object contains other point

{ }

A rectangle contains a point if point lies within it (may lie on border).

public Boolean contains (Rectangle2D other)

// POST: return true if object contains other rectangle

{ }

A rectangle contains a rectangle if other rectangle lies within it

(may lie on border).

public Boolean overlaps (Rectangle2D other)

// POST: return true if other overlaps object

{ }

The other rectangle overlaps the object if it is not contained in object but maintains some overlap.

2. Version 1: Test the Rectangle2D class thoroughly. A test program A1Tester.java is provided on Blackboard. (Note: if you do not complete the application submit a .jar file of the tester for partial credit for completing the Rectangle2D class.)

3. Write a console application that uses the Rectangle2D class. A field (20 points each direction) is filled randomly with small plots (4 points each direction) that are contained within it and do not overlap. This problem should be solved with the Rectangle2D class; do not use a 2D array of characters to represents the field.

4. Create two sample screen shots. Output lists your name, the start coordinates of each plot as shown below, and a grid using P for a plot and period . otherwise. Paste the two screenshots into a single document to staple to this cover sheet.

Here are the sample classes:

GeometricObject.Java:

package cos225assignment1s18; 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"; this.filled = false; } // POST: shape set by user protected GeometricObject(String color, boolean filled) { this.color = color; this.filled = filled; } // accessors and modifiers 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 String toString() { // POST: string representation of shape return "color: " + color + " filled: " + filled; } public abstract double getArea(); // POST: return area of geometric object public abstract double getPerimeter(); // POST: return perimeter of geometric object } 

Point.Java

package cos225assignment1s18; public class Point { private double x ; // x coordinate private double y; // y coordinate public Point ( ) // POST: point (0,0) { x = y = 0; } public Point (double x, double y) // POST: point (x,y) { this.x = x; this.y = y; } // accessors and modifiers 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 String toString() // POST: return (x,y) { return " (" + x + "," + y + ") "; } } 

Tester.Java

package cos225assignment1s18; import java.util.Scanner; public class Tester { public static void main(String[] args) { Scanner scan = new Scanner (System.in); System.out.print("Enter (x,y) for start rectangle: "); double x = scan.nextDouble(); double y = scan.nextDouble(); System.out.print("Enter width,height for start rectangle: "); double w = scan.nextDouble(); double h = scan.nextDouble(); Rectangle2D rec = new Rectangle2D (x,y,w,h); String code; System.out.print("Contains or overlap (C/O): "); code = scan.next(); if (code.equals("C")) { System.out.print("Compare to point or rectangle: (P/R): "); code = scan.next(); if (code.equals("P")) { Point p = new Point(); System.out.print("Enter (x,y) for point: "); p.setX(scan.nextDouble()); p.setY(scan.nextDouble()); if (rec.contains(p))System.out.println("rec contains point"); else System.out.println("rec does not contain point"); } else { System.out.print("Enter (x,y) for second rectangle: "); x = scan.nextDouble(); y = scan.nextDouble(); System.out.print("Enter width,height for second rectangle: "); w = scan.nextDouble(); h = scan.nextDouble(); Rectangle2D rec2 = new Rectangle2D (x,y,w,h); if (rec.contains(rec2))System.out.println("rec contains second rec" + ""); else System.out.println("rec does not contain second rec"); } } else { System.out.print("Enter (x,y) for second rectangle: "); x = scan.nextDouble(); y = scan.nextDouble(); System.out.print("Enter width,height for second rectangle: "); w = scan.nextDouble(); h = scan.nextDouble(); Rectangle2D rec2 = new Rectangle2D (x,y,w,h); if (rec.overlaps(rec2))System.out.println("second rec overlaps rec" + ""); else System.out.println("second rec does not overlap rec"); } scan.close(); } } 

Rectangle.java

public class Rectangle extends GeometricObject{ private double width; private double height; public Rectangle() { super(); width = 2.0; height = 1.0; } public Rectangle(double width, double height, String color, boolean filled) { super (color,filled); this.width = width; this.height = height; }

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; } @Override public double getArea() { return width * height; } @Override public double getPerimeter() { return ((2 * width) + (2 * height)); } @Override public String toString ( ) { return super.toString() + " height: " + height + " width: " + width + " area: " + String.format("%-6.2f",getArea()) + " perimeter: " + String.format("%-10.2f",getPerimeter()); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Intelligent Databases Object Oriented Deductive Hypermedia Technologies

Authors: Kamran Parsaye, Mark Chignell, Setrag Khoshafian, Harry Wong

1st Edition

0471503452, 978-0471503453

More Books

Students also viewed these Databases questions

Question

To be aware of some object-oriented metrics

Answered: 1 week ago