Question: I don't know how to do this coding homework and it is due tomorrow, please help Classes and Interfaces Write each of the following: A
I don't know how to do this coding homework and it is due tomorrow, please help
Classes and Interfaces
Write each of the following:
A class Point to represent points in a two-dimensional plane. Each point has an x and a y coordinate, determined at creation time, and provides at least the following methods:
double getX(); // return x-coordinate of point
double getY(); // return y-coordinate of point
double distance(Point other); // computes and returns the distance from this point to another point
String toString(); // returns the string "(x,y)"
void draw(); // calls StdDraw.point(x, y)
The distance between two points is given by the square root of (x1 - x2)2 + (y1 - y2)2
Before drawing a point, it might be helpful to call StdDraw.setPenRadius(0.05) and StdDraw.setPenColor. StdDraw is available from http://introcs.cs.princeton.edu/java/stdlib/StdDraw.java (copy and paste into a file)
If you find it helpful for the rest of the assignment, you may implement additional methods in the Point class, beyond the ones required above.
An interface SegmentInterface to represent line segments in the two-dimensional plane. Each line segment has two endpoints, initialized at creation time, and provides at least the following methods:
Point left(); // the point at the left end of the segment
Point right(); // the point at the right end of the segment
Point upper(); // the point at the upper end of the segment
Point lower(); // the point at the lower end of the segment
double length(); // return the length of the segment, that is, the distance between its endpoints
void draw(); // draw the line segment
For horizontal segments the upper and lower methods may return either endpoint, and for vertical segments the left and right methods may also return either endpoint.
A class Segment that implements SegmentInterface, and also provides these additional methods:
boolean isHorizontal();
boolean isVertical();
void move(Point newLowerOrLeft);
String toString();
The move method changes the position of the entire segment by moving the lower point to the new position. In other words, the entire segment moves, without changing its length or angle, so that the lower point is in the new position. For horizontal segments (where both endpoints have the same y coordinate), it moves the left point to the new position.
The toString method returns the string "(x1,y1)(x2,y2)", where (x1,y1) is the lower endpoint of the segment (the left endpoint for horizontal segments).
The draw method calls StdDraw.line
A class CreateSegments that has the following:
A method printFour to print the values of 4 segment variables, that is, one method that takes four SegmentInterface variables as parameters and prints all four.
A method static void unitTest (); with all of the following:
Two variables declared as SegmentInterface, one of them with coordinates (0.1,0.2)(0.4,0.5), the other (0.1,0.2)(0.4,0.2)
Two variables declared as Segment, one of them with coordinates (0.4,0.5)(0.4,0.2), the other (0.5,0.5)(0.7,0.15)
code to:
print the four segment (including SegmentInterface) variables, then
draw the four segment variables, then
move the last segment to the left by 0.3 units, so its endpoints become (0.2, 0.5)(0.4, 0.15), and
print all the segment variables again, then
draw all the segment variables again.
For each class (except CreateSegments), you will have to provide one or more constructors that take the appropriate parameters, as well as a default (no-arguments) constructor that initializes all coordinates to zero.
In order to test your code, you will have to create a main method in a different class. That main method calls CreateSegment.unitTest();
Turning in the Assignment
Email your source code to the TA.
Do not turn in your main method, or any other test code other than the unit test.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
