您的位置:首页 > 其它

09-04-02 Class

2015-04-09 14:53 260 查看


BJP3 Exercise 8.5: slopePoint

Status:

You
have solved this problem; good work!

Added by:Will Beebe on 2013/04/01

Language:Java

Keywords:classes

Popularity:14 likes

Like


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 an
IllegalArgumentException
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的结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: