Java面向对象案例详解

1.
/*(中)设计一个圆类,
具有属性:圆心(点)、半径。
添加一个方法:判断一个圆是否包含一个点。*/
public class Demo1 {
    public static void main(String[] args) {
        Point point = new Point();
        point.x=2.0;
        point.y=3.0;

        Point point2 = new Point();
        Circle circle = new Circle();
        circle.center = point2;

        point2.x=5.0;
        point2.y=4.0;
        circle.radius = 10.0;

        boolean judge = circle.method(point);
        /*包含就写true 返回的是boolean值*/
        System.out.println(judge);
    }
}
class Point {
    public double x;
    public  double y;
}
class Circle {
    /*利用的是圆心和输入的这个点
     * 的距离的平方进行比较*/
    /*圆心点:*/
    public Point center;
    /*半径:*/
    public double radius;
    public  boolean method(Point point) {
        double distance = (center.x - point.x)*(center.x - point.x)+(center.y - point.y)*(center.y - point.y);
        return distance <= radius*radius;
    }
}
run:
true


2.
/*(中)设计一个点类,
具有属性:x、y坐标,
具有方法:和另外一个点相加,得到一个新的点,
新的点的x坐标是原来两个点的x坐标和,y是原来两个点的y坐标和*/

import java.util.Arrays;

public class Demo2 {
    public static void main(String[] args) {
        Point p = new Point();
        p.x = 3;
        p.y = 2;

        Point p2 = new Point();
        p2.x = 4;
        p2.y = 5;

        System.out.println(Arrays.toString(Point2.add(p,p2)));
    }

}
class Point2{
    public double x;
    public double y;

    public static double[] add(Point p,Point p2){
        double x2;
        double y2;

        x2 = (p.x+p2.x);
        y2 = (p.y+p2.y);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值