7-2 JLine类

发布于:2022-12-13 ⋅ 阅读:(1029) ⋅ 点赞:(0)

import java.util.Scanner;

class JLine {
    public int a;
    public int b;
    public int c;
    public JLine(int a,int b,int c){
        this.a=a;
        this.b=b;
        this.c=c;
    }
    public void getSlope(){
        System.out.println("slope is assigned"+" "+-(double)a/b);
    }
         public Boolean isOnLine(int x,int y){
             if(a*x+b*y+c==0)
                 return true;
             else
                 return false;
         }
    }
    


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."); 
          }
  }
}


JLine 是由方程 ax+by+c=0 定义的线,其中a不等于零,b不等于零并且a、bc都是整数。JLine 的斜率Slope定义为 double 类型的 -a/b。当xy值代入方程时,如果满足 JLine 方程,则点(由整数xy表示)位于JLine上。也就是说,如果 ax+by+c 等于0,则由xy表示的点在直线上。下表中显示了两个 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."); 
          }
  }
}


输入样例:

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

5
4
-17
5
-2

输出样例:

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

网站公告

今日签到

点亮在社区的每一天
去签到