Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the Point 2D class public abstract class Shape2D { final private String color; final private String shapeName; final public static double THRESH =

image text in transcribedimage text in transcribed

Here is the Point 2D class

public abstract class Shape2D {

final private String color;

final private String shapeName;

final public static double THRESH = 1.0E-5;

/**

* Returns true if two numbers

* are considered close enough

*

* @param a value 1

* @param b value 2

* @return true if a and b are considered close enough

*/

public static boolean closeEnough(double a, double b) {

return Math.abs(a-b)

}

/**

* Constructs the shape

*

* @param color shape color

* @param shapeName name of the shape (e.g. Triangle, Rectangle, etc.)

*/

public Shape2D(String color, String shapeName) {

this.color = color;

this.shapeName = shapeName;

}

/**

* Gets the color of the shape

*

* @return shape color

*/

final public String getColor() {

return color;

}

private String vToString() {

final StringBuilder sb = new StringBuilder();

final Point2D[] v = getVertices();

if (v.length > 0) {

sb.append(v[0]);

for (int i=1; i

sb.append(String.format(", %s", v[i]));

}

}

return sb.toString();

}

/**

* A friendly text representation of the shape

*

* @return something like "Red Rectangle @ ((0.000, 0.000), (0.000, 1.000), (1.000, 1.000), (1.000, 0.000)): center=(0.000, 0.000), perimeter=4.000, area=1.000"

*/

@Override

final public String toString() {

return String.format("%s %s @ (%s): center=%s, perimeter=%.3f, area=%.3f",

color, shapeName, vToString(), getCenter(), getPerimeter(), getArea());

}

/**

* Gets the area of the shape

*

* @return area

*/

public abstract double getArea();

/**

* Gets the perimeter of the shape

*

* @return perimeter

*/

public abstract double getPerimeter();

/**

* Gets the center of the shape

*

* @return center of the shape

*/

public abstract Point2D getCenter();

/**

* Gets the vertices of the shape

*

* @return shape vertices

*/

public abstract Point2D[] getVertices();

}

Here is the rectangle class:

public class Rectangle extends Shape2D {

/**

* Constructs a rectangle given two points

*

* @param color rectangle color

* @param p1 point 1

* @param p2 point 2

*/

public Rectangle(String color, Point2D p1, Point2D p2) {

super(color, ""); // replace with your code

}

/**

* Returns true if provided

* another rectangle whose

* lower-left and upper-right

* points are equal to this

* rectangle

*

* @param o another object

* @return true if the same rectangle

*/

@Override

public boolean equals(Object o) {

return false; // replace with your code

}

/**

* Gets the lower-left corner

*

* @return lower-left corner

*/

public Point2D getLowerLeft() {

return null; // replace with your code

}

/**

* Gets the upper-right corner

*

* @return upper-right corner

*/

public Point2D getUpperRight() {

return null; // replace with your code

}

@Override

public double getArea() {

return 0; // replace with your code

}

@Override

public double getPerimeter() {

return 0; // replace with your code

}

@Override

public Point2D getCenter() {

return null; // replace with your code

}

@Override

public Point2D[] getVertices() {

return null; // replace with your code

}

}

Here is the Triangle class:

public class Triangle extends Shape2D {

/**

* Constructs a triangle given

* three points

*

* @param color color

* @param p1 point 1

* @param p2 point 2

* @param p3 point 3

*/

public Triangle(String color, Point2D p1, Point2D p2, Point2D p3) {

super(color, ""); // replace with your code

}

/**

* Returns the axis-aligned

* bounding box for this

* triangle

*

* @return axis-aligned bounding box

*/

public Rectangle getAxisAlignedBoundingBox() {

return null; // replace with your code

}

@Override

public double getArea() {

return 0; // replace with your code

}

@Override

public double getPerimeter() {

return 0; // replace with your code

}

@Override

public Point2D getCenter() {

return null; // replace with your code

}

@Override

public Point2D[] getVertices() {

return null; // replace with your code

}

}

Here is the main program(Reffered to as PA4b):

public class PA4b {

/**

* Error if incorrect command-line arguments are supplied

*/

public static final String ERR_USAGE = "Please supply correct inputs: color x1 y1 x2 y2 x3 y3";

/**

* Number of command-line arguments

*/

public static final int NUM_ARGS = 7;

/**

* Produces a string representing

* all vertex information in CSV

* format:

* "color",x,y

*

* For all shape vertices,

* including axis-aligned bounding

* boxes for any included triangles

*

* @param shapes array of shapes

* @return string of CSV information

*/

public static String shapeVertices(Shape2D[] shapes) {

return ""; // replace with your code (use a StringBuilder for efficiency!)

}

/**

* Outputs vertex information in CSV

* format about the triangle supplied

* via command-line arguments, including

* its axis-aligned bounding box

*

* @param args command-line arguments: color x1 y1 x2 y2 x3 y3

*/

public static void main(String[] args) {

// replace with your code

}

}

Your task is to write a program that will output colored shape vertices in CSV format (comma-separated value?; something Microsoft Excel can open!). To build up to this capability, you will be implementing a set of classes, including two subclasses. Here is the recommended sequence To start, implement the Point2D class. It is quite similar to the class in a previous assignment, but now has you override the equals method to detect points that represent the same location, within a distance threshold Then implement the Rectangle class, which is a subclass of Shape2D (provided for you). You will use simple arithmetic to implement such functionality as computing area, perimeter, and center point. The vertices (think corner points) for a rectangle are expected to start at the lower-left corner and proceed clockwise. Two rectangles are considered "equal" if they have lower-left and upper-right points that are "equa" Now move on to the Triangle class, which is also a subclass of Shape2D. The math here can get a little more complex for area3 and center point (centroid4). An axis-aligned bounding box (AABB5) is simply the rectangle that most closely bounds the vertices of the triangle. The vertices should be returned in the order they were provided in the constructor. Now move on to the PA4b class. The shapeVertices method receives an array of Shape2D objects and must produce a string which includes all the vertices in CSV format (on each line: "color",x,y). If the method encounters a triangle object, it should include the vertices of the AABB after the triangle. This is an opportunity to experiment with polymorphic code. When this method is complete, proceed to main - validate command-line arguments for a single colored triangle and use the method you just completed to output the corresponding CSV to the terminal. 1. 2. 3. 4. Here are example runs of a completed program java edu.wit.cs.comp1050.PA4b Please supply correct inputs: color x1 y1 x2 y2 x3 y Your task is to write a program that will output colored shape vertices in CSV format (comma-separated value?; something Microsoft Excel can open!). To build up to this capability, you will be implementing a set of classes, including two subclasses. Here is the recommended sequence To start, implement the Point2D class. It is quite similar to the class in a previous assignment, but now has you override the equals method to detect points that represent the same location, within a distance threshold Then implement the Rectangle class, which is a subclass of Shape2D (provided for you). You will use simple arithmetic to implement such functionality as computing area, perimeter, and center point. The vertices (think corner points) for a rectangle are expected to start at the lower-left corner and proceed clockwise. Two rectangles are considered "equal" if they have lower-left and upper-right points that are "equa" Now move on to the Triangle class, which is also a subclass of Shape2D. The math here can get a little more complex for area3 and center point (centroid4). An axis-aligned bounding box (AABB5) is simply the rectangle that most closely bounds the vertices of the triangle. The vertices should be returned in the order they were provided in the constructor. Now move on to the PA4b class. The shapeVertices method receives an array of Shape2D objects and must produce a string which includes all the vertices in CSV format (on each line: "color",x,y). If the method encounters a triangle object, it should include the vertices of the AABB after the triangle. This is an opportunity to experiment with polymorphic code. When this method is complete, proceed to main - validate command-line arguments for a single colored triangle and use the method you just completed to output the corresponding CSV to the terminal. 1. 2. 3. 4. Here are example runs of a completed program java edu.wit.cs.comp1050.PA4b Please supply correct inputs: color x1 y1 x2 y2 x3 y

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

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions