-->
当前位置:首页 > 题库 > 正文内容

编程题:JLine类设计与实现

Luz3年前 (2022-09-07)题库403
**JLine** 是由方程 *ax+by+c=0* 定义的线,其中*a*不等于零,*b*不等于零并且*a、b*和*c*都是整数。**JLine** 的斜率Slope定义为 double 类型的 *-a/b*。当*x*和*y*值代入方程时,如果满足 **JLine** 方程,则点(由整数*x*和*y*表示)位于**JLine**上。也就是说,如果 *ax+by+c* 等于0,则由*x*和*y*表示的点在直线上。下表中显示了两个 *JLine* 方程的示例。



| Equation | Slope (–a / b) | Is point (5, -2) on the line? |
| -------- | -------- | -------- |
| 5x + 4y - 17 = 0 | -5 / 4 = -1.25 | Yes, because 5(5) + 4(-2) + (-17) = 0 |
| -25x + 40y + 30 = 0 |25 / 40 = 0.625 | No, because -25(5) + 40(-2) + 30 ≠ 0 |


编写JLine类。
1)您的实现必须包含一个具有三个整数参数的构造函数, 参数依次代表a、b和c。假设a和b不为0;
2)它还必须包括一个方法getSlope,该方法计算并返回直线的斜率;
3)同时需要编写一个方法isOnLine,如果由两个参数(x和y)表示的点在JLine上则返回true,否则返回false。



### 提示代码:


import java.util.Scanner;

class JLine {
//JLine类需要补全的内容
//请在此处添加代码
}

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a=input.nextInt();
int b=input.nextInt();
int c=input.nextInt();
int x=input.nextInt();
int y=input.nextInt();
if(a!=0&&b!=0){
JLine line = new JLine(a, b, c);
line.getSlope();
if(line.isOnLine(x, y))
System.out.println("Point("+x+","+y+") on line.");
else
System.out.println("Point("+x+","+y+") is not on line.");
}
}
}






### 输入样例:

在这里给出一组输入。例如:

in
5
4
-17
5
-2



### 输出样例:


out
slope is assigned -1.25
Point(5,-2) on line.










答案:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。