I need help on my Java programming code. I am working on a project which incluId
ID: 3568934 • Letter: I
Question
I need help on my Java programming code. I am working on a project which incluIdes three classes and their interfaces! In my segment class, I get error on slope method! It passes all the parts but instead of infinity and 0 I get negative infinity and negative zero. Any idea?
Thank you in advance!
Here is my code:
//Segment.java
public class Segment implements SegmentInterface {
// two Points that hold endpoints of the segment
private Point p1;
private Point p2;
// Default constructor that will set the endpoints to new
// Points with values (0,0) and (4,4)
public Segment() {
//this(0, 0, 4, 4);
this.p1 = new Point(0, 0);
this.p2 = new Point(4, 4);
}
// Parameterized constructor that accepts (int x1, int y1, int x2, int y2)
// and creates and sets the endpoints
public Segment(int x1, int y1, int x2, int y2) {
//this.p1 = new Point(x1, y1);
//this.p2 = new Point(x2, y2);
if(x1 == x2 && y1 == y2){
throw new IllegalArgumentException("Points can't have a lenght of 0!");
}
else{
this.p1 = new Point(x1, y1);
this.p2 = new Point(x2, y2);
}
}
//A parameterized constructor that accepts (Point p1, Point p2) and sets both
//the endpoints to a deep copy of the Points that are passed in.
//Make sure to check to see if both Points are equal. This would be a
//theoretical "Segment" of length 0 which is not allowed, so throw a new
//IllegalArgumentException.
public Segment(Point p1, Point p2) {
this.p1 = new Point(p1.getX(), p1.getY());
this.p2 = new Point(p2.getX(), p2.getY());
if(p1.equals(p2)){
throw new IllegalArgumentException("Points cannot have same values!");
}
}
// 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.
public Segment(Segment other) {
this(other.p1, other.p2);
//if(other.p1 == other.p2){
//throw new IllegalArgumentException("Undefine!");
//}
}
public Point getP1() {
return p1;
}
public Point getP2() {
return p2;
}
// The length method returns the length of the Segment.In fact, this method
// is same as distanceTo method
// So we can use distanceTo method which is already defined in Point class
public double length() {
return (p1.distanceTo(p2));
}
// The translate method returns nothing and should translate, or shift,
// itself (the Segment) by the distances passed in
public void translate(int xMove, int yMove) {
p1.translate(xMove, yMove);
p2.translate(xMove, yMove);
}
// The midpoint method calculates and returns the midpoint of the Segment as
// a new Point
public Point midpoint() {
return (p1.halfwayTo(p2));
}
/*
* The slope method returns the slope of the Segment as a double. If the segment
* is vertical, the slope is undefined. In that case, 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 the slope 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
Explanation / Answer
Replace the slope method with the following code.
comment if you have any doubts
public double slope() {
if (p1.getY() == p2.getY()) {
return 0;
} else if (p1.getX() == p2.getX()) {
double inf = Double.POSITIVE_INFINITY;
return inf;
}
return ((double) (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.