PTA|Java-两点成线

设计一个直线类Line,需要通过两个点Point对象来确定。Line类具体要求如下:

1)定义两个Point对象p1,p2;

2)写出有参构造方法,传递两个对象值给p1,p2

3)为p1,p2写出setters,和getters方法

4)为Line写出一个getLength方法求直线中两点的长度

5) 为LIne写一个ToString方法,方法如下所示:

public String toString() {
return "Line [p1=" + p1 + ", p2=" + p2 + "]";
}

在Main类的main方法中,定义一个Line数组,数组长度通过键盘给出,然后通过键盘为每线段的两个Point对象的坐标赋值,并生成相应Line对象放入数组中,循环数组,输出每条直线的信息,以及两个点之间的距离。

Point 类如下所示:

public class Point {
    private int x, y;// x,y为点的坐标
    //求两点之间的距离
    public double distance(Point p1) {
        return Math.sqrt((p1.x -this.x)*(p1.x -this.x)+(p1.y-this.y)*(p1.y-this.y));
    }
    public Point(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Point() {
        super();
        x = y =0;
    }
    @Override
    public String toString() {
        return "Point [x=" + x + ", y=" + y + "]";
    }
}

输入格式:

第一行输入数组的长度n
每一行输入一个Line对象的两个点对象的 x y的坐标,中间用空格隔开

输出格式:

循环输出,输出直线的信息,以及每条直线的两个点的距离,保留一位小数。

输入样例:

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

2
0 0 2 3
1 3 2 5

输出样例:

在这里给出相应的输出。例如:

Line [p1=Point [x=0, y=0], p2=Point [x=2, y=3]]
此线段的长度为:3.6
Line [p1=Point [x=1, y=3], p2=Point [x=2, y=5]]
此线段的长度为:2.2

参考代码如下:

import java.util.Scanner;

class Point {
    private int x, y;// x,y为点的坐标
    //求两点之间的距离
    public double distance(Point p1) {
        return Math.sqrt((p1.x -this.x)*(p1.x -this.x)+(p1.y-this.y)*(p1.y-this.y));
    }
    public Point(int x, int y) {
        super(); this.x = x; this.y = y;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Point() {
        super(); x = y =0;
    }
    public String toString() {
        return "Point [x=" + x + ", y=" + y + "]";
    }
}
class Line{
    Point p1 = new Point(), p2 = new Point();
    public Line(int q, int w, int e, int r){
        p1.setX(q); p1.setY(w); p2.setX(e); p2.setY(r);
    }
    public String toString(){
        return "Line [p1="+p1+", p2="+p2+"]";
    }
    public double distance(){
        return p1.distance(p2);
    }
}
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Line[] l = new Line[sc.nextInt()];
        for(int i = 0; i<l.length; i++){
            l[i] = new Line(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt());
            System.out.println(l[i]);
            System.out.println("此线段的长度为:"+String.format("%.1f", l[i].distance()));//String.format("格式", 值)方法进行四舍五入
        }
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸡『』

最不值钱的便是钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值