吉软_java57_第六次作业

package org.jsoft.homework;

public class Circle {
    //属性
    Point p;
    float radius;
    //构造
    public Circle(){}
    public Circle(Point p,float r){
        this.p=p;
        this.radius=r;
    }
    public Circle(int x, int y, float r){
        this.radius=r;
        p.setX(x);
        p.setY(y);
    }
    public Circle(Circle c){
        this.p=c.getP();
        this.radius=c.getRadius();
    }
    //get set方法
    public Point getP() {
        return p;
    }
    public void setP(Point p) {
        this.p = p;
    }
    public float getRadius() {
        return radius;
    }
    public void setRadius(float radius) {
        this.radius = radius;
    }
    public void setPoint(int x, int y){
        p.setX(x);
        p.setY(y);
    }
    //方法
    判断一个点是否在圆内
    public boolean isInside(Point p1){
        int x1=p1.getX();
        int y1=p1.getY();
        int x=p.getX();
        int y=p.getY();
        if((x1>=x-radius||x1<=x+radius)&&(y1>=y-radius||y1<=y+radius)){
            return true;
        }
        else
            return false;
    }
    public Circle outerRect(Rectangle r){
        Circle c1=new Circle();
        int w=r.getWidth();
        int h=r.getHeight();
        c1.radius=(float)((Math.sqrt(w*w+h*h))/2);
        c1.p=r.getPoint();
        return c1;
    }
    public Circle join(Circle c1, Circle c2){
        Circle c=new Circle();
        int x1=c1.p.getX();
        int x2=c2.p.getX();
        int y1=c1.p.getY();
        int y2=c2.p.getY();
        float r1=c1.getRadius(),r2=c2.getRadius(),r;
        Point p=new Point((x1+x2)/2, (y1+y2)/2);
        c.setP(p);
        float a,b=x1-x2,d=y1-y2;
        a=(float)(Math.sqrt(b*b+d*d))/2;
        if(r1<r2){
            r=r2+a;
        }else{
            r=r1+a;
            
        }
        c.setRadius(r);
        return c;
    }
    public float distance(Circle c1, Circle c2){
        int x1=c1.p.getX();
        int x2=c2.p.getX();
        int y1=c1.p.getY();
        int y2=c2.p.getY();
        float dis,b=x1-x2,d=y1-y2;
        dis=(float)(Math.sqrt(b*b+d*d));
        return dis;
    }
    //返回本圆的基本信息,格式为(圆心x坐标值,圆心y坐标值,圆半径)。如:(0, 0, 2)
    public String toString(){
        return "("+p.getX()+","+p.getY()+","+radius+")";
    }
}
 

package org.jsoft.homework;

import java.util.Scanner;


public class CircleTest {
    public static void main(String[] args) {
        Point p=new Point(0, 0);
        Point p2=new Point(-1,-1);
        Circle c1=new Circle(p,2);
        Circle c2=new Circle(p,2);
        Rectangle re=new Rectangle(p2,2,2);
        Point p1=new Point(1, 1);
        boolean a=c1.isInside(p1);
        String str=c1.toString();
        if(a){
            System.out.println("测试点(1,1)在你所指定的圆"+str+"内");
        }
        else{
            System.out.println("测试点(1,1)不在你所指定的圆"+str+"内");
        }
        System.out.println(c1.outerRect(re));
    }
}

 

package org.jsoft.homework;

public class Point {
    private int x;
    private int y;
    public Point(int x,int y){
        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 void display(){
        System.out.println("the point is that ("+x+","+y+")");
    }
}
 

 

package org.jsoft.homework;

public class Rectangle {
    //属性
    Point point;
    int width;
    int height;
    //构造
    public Rectangle(Point p,int width,int height){
        this.height=height;
        this.point=p;
        this.width=width;
    }
    public Rectangle(int x,int y,int width,int height){
        this.height=height;
        this.width=width;
        point.setX(x);
        point.setY(y);
    }
    public Rectangle(Rectangle r){
        this.height=r.getHeight();
        this.point=r.getPoint();
        this.width=r.getWidth();
    }
    public Rectangle(){}
    public Point getPoint() {
        return point;
    }
    public void setPoint(Point point) {
        this.point = point;
    }
    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    //display方法
    public void display(){
        System.out.println("This rectangle's width and Height is:"+width+","+height);
    }
    public int getArea(){
        int area=width*height;
        return area;
    }
}

 

 

package org.jsoft.homework;

public class TestRectangle {
    public static void main(String[] args) {
        Point p=new Point(50,67);
        int width=50,height=67;
        Rectangle r1=new Rectangle(p,width,height);
        r1.display();
        System.out.println("面积为:"+r1.getArea());
    }
}
 

 

转载于:https://my.oschina.net/u/3716895/blog/1568164

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值