BJP3 Exercise 8.5: slopePoint
Status:
You have solved this problem; good work!
Language:Java
Keywords:
classes
Add the following method to the Point
class:
public double slope(Point other)
Returns the slope of the line drawn between this Point
and the given other Point
. Use the formula (y2 - y1) / (x2 - x1) to determine the slope between two points (x1, y1) and (x2, y2). Note that this formula fails for points with identical x-coordinates, so throw anIllegalArgumentException
in this case.
public class Point {
private int x;
private int y;
// your code goes here
}
我的答案:
public double slope(Point other){
if (other.x == x) {
throw new IllegalArgumentException();
}else{
return (double)(other.y - y)/(other.x - x);
}
}
这样也能最后得到诸如0.5的结果