类和对象例子(矩形,圆,PI的计算)

矩形类

public class Rectangle {
    private int width;
    private int height;

    private String shape;
    Rectangle(){
        width = 10;
        height = 10;
        shape = "#";
    }
    Rectangle(int in_width, int in_height, String in_shape){
        width = in_width;
        height = in_height;
        shape = in_shape;
    }
    int getArea(){
        int result = width * height;
        return result;
    }

    int getRoundLen(){
        int result = (width + height) * 2;
        return result;
    }

    void scaleWidth(double factor){
        if(factor <= 10){
            width *= factor;
        }
        else{
            width *= 10;
        }
    }

    void printSelf(){
        for(int i = 0; i < height; i++){
            for(int j = 0; j < width; j++){
                System.out.print(shape);
            }
            System.out.println();
        }
    }

    void changeShape(String _shape)
    {
        shape = _shape;
    }
}

测试矩形类

import java.util.Random;

public class Main3 {
    public static void main(String[] args) {
//        int a = 33;
//        int a = 90;
        Rectangle rect = new Rectangle(5,3,"@");
        int area = rect.getArea();
        int len = rect.getRoundLen();
        rect.printSelf();
//        rect.width *= 100;
        rect.scaleWidth(2);
        rect.changeShape("#");
        rect.printSelf();

        System.out.println(area);
        System.out.println(len);

        Rectangle rect2 = new Rectangle(5,8,"#");
        area = rect2.getArea();
        len = rect2.getRoundLen();
        System.out.println(area);
        System.out.println(len);

        rect2.scaleWidth(2);
        area = rect2.getArea();
        len = rect2.getRoundLen();
        System.out.println(area);
        System.out.println(len);
    }
}

Point类

public class Point {
    private double x;
    private double y;

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public Point(double _x, double _y) {
        x = _x;
        y = _y;
    }
}

Circle类

public class Circle {
    private double radius;
    private Point center;

    Circle(){
        radius = 1;
        center = new Point(0,0);
    }
    Circle(double _radius, double _center_x, double _center_y){
        radius = _radius;
        center = new Point(_center_x, _center_y);
    }
    boolean isPointInCircle(Point point){
        double distance = Math.sqrt((center.getX() - point.getX())*(center.getX() - point.getX())
                + (center.getY() - point.getY())*(center.getY() - point.getY()));
        if(distance < radius){
            return true;
        }
        else{
            return false;
        }
    }
}

测试Circle类

蒙特卡洛求pi近似值

import java.util.Random;

public class TestCircle {
    public static void main(String[] args) {
        Circle circle = new Circle(2,2,2);
        Point p1 = new Point(0,0);
        System.out.println(circle.isPointInCircle(p1));
        Point p2 = new Point(3,3);
        System.out.println(circle.isPointInCircle(p2));

        Random random = new Random();

        int in_sum = 0;
        int sum = 0;
        for(int i = 0; i < 1000000; i++){
            double x = random.nextDouble()*4;
            double y = random.nextDouble()*4;
            Point p = new Point(x,y);
            if(circle.isPointInCircle(p))
            {
                in_sum++;
            }
            sum++;
        }

        double result = in_sum*1.0/sum;
        System.out.println(result*4);

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值