## Question

Point – You will use the class you have already written to represent a geometric point in a Cartesian plane. You can add methods to it if you want.

Segment – this will represent a line segment. You can add other methods if you think they are needed, but at minimum, Segment should have the following:

Data:

• Data to hold both endpoints. They should be stored as Points.

Constructors:

• A default constructor that will set the endpoints to new Points with values (0,0) and (7, 7)

• A parameterized constructor that accepts (x1, y1, x2, y2) – as ints – and creates and sets the endpoints. But first, check to see if x1 == x2 and y1== y2. This would be a theoretical “Segment” of length 0, so throw a new IllegalArgumentException like this:

throw new IllegalArgumentException(“whatever explanatory String you want”);

• A parameterized constructor that accepts (Point, Point) and sets both the endpoints to the Points that are passed in. But first, check to see if both Points are equal. This would be a theoretical “Segment” of length 0, so throw a new IllegalArgumentException like this:

throw new IllegalArgumentException(“whatever explanatory String you want”);

• A copy constructor that accepts a Segment and initializes the data (of the new Segment being created) to be the same as the Segment that was received. Be sure that it ends up being a deep copy of the Segment that was passed in.

Methods:

• A toString() method that returns a String representing the Segment. It should be in the form:

(x1, y1)---------(x2, y2) //be sure there are 9 dashes between the two Points

• A method called length() which calculates and returns the length of the Segment as a double.

• A method called translate(int xmove, int ymove) that will return nothing and “translates,” or shifts, itself by the distances passed in.

• A method called midpoint() which calculates and returns the midpoint of the Segment as a new Point.

• A method called slope() which calculates and returns the slope of the Segment as a double. If the segment is vertical, the slope is undefined. You can do the calculation (with the denominator coming out to zero), and Java will return Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY, depending on which way you subtract to calculate the slope. You can either check for division by zero or let Java do the calculation. However, your requirements are to always return Double.POSITIVE_INFINITY if it is undefined.

Also – strangely enough, Java may return 0 or -0 for a slope which is horizontal. Your requirements are to always return a slope of 0 for a horizontal line (don’t always just return the Java calculation).

My suggestion is to enhance your Point class to include a method called slopeTo(Point anotherPoint). Then put all the logic described above into this new slopeTo method. Then have Segment’s slope() method just ask an endPoint for the slopeTo the other endpoint. The advantage of this is that the Segment class does not have to look at its endPoint’s x and y values – it just tells asks a Point to calculate the slope.

• A method called isParallelTo(Segment s1) which returns true/false depending on whether it is parallel to the s1 that is received. Recall how you can tell if 2 segments are parallel. (You can also see why I wanted vertical lines to always have a slope of Double.POSITIVE_INFINITY instead of sometimes being Double.POSITIVE_INFINITY and sometimes being Double.NEGATIVE_INFINITY; same for 0 and -0).

• A method called equals(Object otherObj) which returns true if it is equal to the otherObj. Note that it receives an Object; there is a particular way that this method should be implemented (see notes/demos). Also note that a Segment from (1, 2) to (3, 4) should be equal to a Segment from (3, 4) to (1, 2).

• A method called shorten() which changes the endpoints so that they are both halfway to the midpoint.

• Prefer(without using a .getX and .getY in the Point class) A method called contains(Point p1) which returns true if this Segment contains the Point p1 that is received (but not as an endpoint).

As an example, look at the diagram above (for shorten()). Each Segment will contain its midpoint, but not its endpoints. If the Segment on the left is named s1, then:

s1.contains(new Point(6,8)) would return true.

s1.contains(new Point(12,16)) would return false.

StyleSegment – this will be a subclass of Segment and will also store what it looks like (“dashed”, “dotted”, or “double”). As such, almost all of its data/methods can be inherited from its parent class (there is only 1 new data, 2 overridden methods, and 1 new method).

Data:

• (are inherited), but add a another String to hold the style (“dashed”, “dotted”, or “double”)

Constructor:

• A default constructor that sets the endpoints to new Points that are (0,0) and (6,0) and the style to “dashed”

• A parameterized constructor that accepts (x1, y1, x2, y2, newStyle) – the x’s and y’s are ints and the style is a String. It will create and set the endpoints and also store the style. But first, check to see if x1 == x2 and y1== y2. This would be a theoretical “Segment” of length 0, so throw a new IllegalArgumentException like this:

throw new IllegalArgumentException(“whatever explanatory String you want”);

Also, check to see if the style is one of the 3 Strings listed above. If not, throw a new IllegalArgumentException like this:

throw new IllegalArgumentException(“whatever explanatory String you want”);

Methods:

• Almost every method can be inherited except for the following

• toString(): depending on the type, the String representation will look like this

- (x1, y1)- - - - -(x2, y2) //if it is dashed (5 dashes filled in with 4 spaces –be exact)

- (x1, y1)………(x2, y2) //if it is dotted (9 periods, be exact)

- (x1, y1)=========(x2, y2) //if it is double (9 equal signs, be exact)

• .equals(Object obj) - it will specifically refer to the StyleSegment class so you have to write a separate one. Again, be sure that it receives an Object and is implemented with the steps shown in the .equals demo.

• getStyle() – it will return the style as a String (“dashed”, “dotted”, or “double”).

Comments and formatting: Please use the Java conventions for variable names, indenting, and formatting. Each class should have an opening comment which briefly describes the class and includes your name and class on a separate line. Each method should have a short opening comment which describes it. “Sections” of code or parts that are tricky should have comments. See programs from the book for examples (although I prefer that opening and closing “squigglies” be indented the same).

Notes:

- To square a number x, you can either just multiply it times itself (x*x) or use Math.pow(x, 2); In the Math class, there is also a static method that will return the hypotenuse directly.

- Since the Point class has a method called distanceTo(Point another), it becomes very easy for the Segment class to implement is length() method, it just tells one endpoint to return the distanceTo the other endpoint. Because the functionality has already been implemented in the Point’s methods, Segment does not have to go looking for the x’s and y’s and do the calculation itself. The same thing is true for halfwayTo and translate(). Similarly, I think you could implement other methods in Point; for example, if you have a method called slopeTo(Point another) and then the Segment could use it when calculating its slope (again, without having to access individual x’s and y’s). In other words, see if you can implement this program without adding getX() and getY() to Point.

## Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class Segment {protected Point p1;

protected Point p2;

/**

* constructor

* @param p1

* @param p2

*/

public Segment(Point p1, Point p2) {

if (p1 == null || p2 == null) {

throw new IllegalArgumentException("one of recieved Point objects is null");

}

if (p1.getX() == p2.getX() && p1.getY() == p2.getY()) {

throw new IllegalArgumentException("2 Points are euqal");

}

this.p1 = new Point(p1.getX(), p1.getY());

this.p2 = new Point(p2.getX(), p2.getY());

}

/**

* default constructor

*/

public Segment() {

this.p1 = new Point(0,0);

this.p2 = new Point(7, 7);

}

/**

* constructor

* @param x1

* @param y1

* @param x2

* @param y2

*/

public Segment(int x1, int y1, int x2, int y2){

if (x1 == x2 && y1== y2) {

throw new IllegalArgumentException("x's are same and y's are same");

}

this.p1 = new Point(x1,y1);

this.p2 = new Point(x2, y2);

}...