java实验2 对象、类、继承、包

(1)声明位于包test.exam中Point类,有坐标x、y两个私有成员变量,有一个返回与其它点的距离的方法,还有一个移动点的方法

package com.qst.fuxi;
public class Point {
    double x;
    double y;
    public Point() {
    
    }
    public Point(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public void movePoint(double dx,double dy) {
           System.out.println("移动前的坐标为:"+this.x+this.y);
           System.out.println("移动量:"+dx+dy);
           this.x+=dx;
           this.y+=dy;
           System.out.println("移动后的坐标为:"+this.x+this.y);
       }
}
测试函数
1、返回与其它点的距离    
package com.qst.fuxi;
import java.util.Scanner;
import java.math.*;
public class PointDemo {
    Point p1;
    Point p2;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一个点的x值x1");
        Double x1 = scanner.nextDouble();
        System.out.println("请输入第一个点的y值y1");
        Double y1 = scanner.nextDouble();
        System.out.println("请输入第二个点的x值x2");
        Double x2 = scanner.nextDouble();
        System.out.println("请输入第二个点的y值y2");
        Double y2 = scanner.nextDouble();
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        PointDemo pd2= new PointDemo(p1,p2);
        pd2.distance();
    }
    public void distance() {
      Double a=Math.sqrt(Math.abs(p1.getX()-p2.getX())*Math.abs(p1.getX()-p2.getX())+Math.abs(p1.getY()-p2.getY())*Math.abs(p1.getY()-p2.getY()));
      System.out.println(a);
    }
    public PointDemo() {
        super();
    }
    public PointDemo(Point p1, Point p2) {
        super();
        this.p1 = p1;
        this.p2 = p2;
    }
    public Point getP1() {
        return p1;
    
    public void setP1(Point p1) {
        this.p1 = p1;
    }
    public Point getP2() {
        return p2;
    }
    public void setP2(Point p2) {
        this.p2 = p2;
    }
}
2、移动点    
package com.qst.fuxi;
public class PointMove {
        public double x;
        public double y;
        public static void main(String[] args) {
        Point p1=new Point(1.0,2.0);
        Point p2=new Point(3.0,4.0);
        p1.movePoint(p2.x,p2.y);    
        }
}

运行结果:

(2)声明一个位于包test.abc中的圆类,圆由圆心和

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值