7-2 JLine类

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 方程的示例。

EquationSlope (–a / b)Is point (5, -2) on the line?
5x + 4y - 17 = 0-5 / 4 = -1.25Yes, because 5(5) + 4(-2) + (-17) = 0
-25x + 40y + 30 = 025 / 40 = 0.625No, 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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值