java-1105-夏丽昀

1,Point 

package org.jsoft.circle;

public class Point {
//Point 的属性x,y
 private float x, y;
 //get set的方法
 public float getX() {
  return x;
 }

 public void setX(float x) {
  this.x = x;
 }

 public float getY() {
  return y;
 }

 public void setY(float y) {
  this.y = y;
 }
//构造方法
 public Point(float x, float y) {
  this.x = x;
  this.y = y;
 }
//显示输出 x,y
 public void dispaly() {
  System.out.print("The point is:" + x + "," + y);
 }
}

2测试Point

package org.jsoft.circle;

public class TestPoint {
 //引入main方法
public static void main(String[]args){
 //创造对象
 Point p=new Point(3,4);
 //调用dispaly
 p.dispaly();
}
}

2 Rectangle

package org.jsoft.circle;

public class Rectangle {
    //引入类Point 属性width height
 private Point point;
    //加入set get 方法
 public Point getPoint() {
  return point;
 }

 public void setPoint(Point point) {
  this.point = point;
 }

 public float getWidth() {
  return width;
 }

 public void setWidth(float width) {
  this.width = width;
 }

 public float getHeight() {
  return height;
 }

 public void setHeight(float height) {
  this.height = height;
 }

 private float width;
 private float height;
    //构造Rectangle 方法 矩形的引入p,width ,height
 public Rectangle(Point p, float width, float height) {
  this.point = p;
  this.width = width;
  this.height = height;
 }
     //构造方法 把Point 具体化到点
 public Rectangle(float x, float y, float width, float height) {
  point = new Point(0, 0);
  point.setX(x);
  point.setY(y);
  this.width = width;
  this.height = height;
 }
     //构造方法是无用的
 public Rectangle(Rectangle r) {
  // this.height=r.height;
  // this.width=r.width;
 }
    //构造方法是无用的
 public Rectangle() {
 }
    //产生矩形的面积
 public float getArea() {
  float a = width * height;
  return a;
 }
     //显示矩形的长 高
 public void display() {
  System.out.println("The rectangle's width and height is:"
    + point.getX() + "," + point.getY());
 }
}

3 测试Rectangle

package org.jsoft.circle;

public class TestRectangle {
public static void main(String[]args){
 //创建对象
 Rectangle r=new Rectangle(2,2,2,2);
 Point p=new Point(3,3);
 //调用对象的方法
 r.display();
 r.getArea();
 System.out.println(r.getArea());
}
}

4 Circle

package org.jsoft.circle;

public class Circle {
 //构造类的属性。引入 Point类
 private Point p;
    //创建 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(float x,float y){
      this.p.setX(x);
      p.setY(y);
     }
 private float radius;
 //构造方法 ,无用
  public Circle() { 
   
 }
     //构造圆 有圆心 半径
 public Circle(float x, float y, float r) {
  p = new Point(x, y);
  p.setX(x);
  p.setY(y);
  this.radius = r;
 }
    //构造圆 有Point(圆心) 半径
 public Circle(Point p, float r) {
  this.p=p;
  this.radius=r;
 }
     //构造一个新圆
 public Circle(Circle c) {
  p=new Point(0,0);
   this.p=c.p;
   this.radius=c.radius;
 }
     //判断点与圆心的距离
 public boolean isInside(Point point) {
  boolean b = false;
  float result = (float) Math.sqrt((point.getX() - p.getX())
    * (point.getX() - p.getX()) + (point.getY() - p.getY())
    * (point.getX() - p.getX()));
  if (result < radius) {
   b = true;
  } else {
   b = false;
  }
  return b;
 }
    //产生矩形的外拉圆
 public Circle outerRect(Rectangle r) {
  float x = r.getHeight() / 2 + r.getPoint().getX();
  float y = r.getWidth() / 2 + r.getPoint().getY();
  float rr = (float) Math.sqrt((r.getHeight() * r.getHeight() + r
    .getWidth() * r.getWidth()) / 2);
  Circle c = new Circle((float)x,(float) y, rr);
  return c;
 }
 //两个圆的最小外接圆
 public Circle join(Circle c1,Circle c2){
  float x=(c1.getP().getX()+c2.getP().getX())/2;
  float y=(c1.getP().getY()+c2.getP().getY())/2;
  float r=(float) (Math.sqrt((c1.getP().getX()-c2.getP().getX())*(c1.getP().getX()-c2.getP().getX())+(c1.getP().getY()-c2.getP().getY())*(c1.getP().getY()-c2.getP().getY()))+c1.radius+c2.radius)/2;
  Circle c=new Circle(x,y,r);
  return c;
 }
 //两个圆心的距离
    public float distance (Circle c1, Circle c2){
     float distance=(float) Math.sqrt((c1.getP().getX()-c2.getP().getX())*(c1.getP().getX()-c2.getP().getX())+(c1.getP().getY()-c2.getP().getY())*(c1.getP().getY()-c2.getP().getY()));
     return distance;
     
    }
    //把输出圆的属性转化
 public String toString() {
  return "半径为" + radius + "点的坐标" + p.getX() + "," + p.getY();
 }
}

5.圆的测试


import java.util.Scanner;

public class TestCircle {
 public static void main(String[] args) {
  //创建对象
  Point p = new Point(0, 0);
  Circle c = new Circle(0, 0, 0);
  //调入输入语句
  Scanner sc = new Scanner(System.in);
  //进行循环
  while (true) {
   //输入命令
   System.out.print("请输入命令:(isInside/outerRect/join/distance/quit)");
   String st = sc.next();
   //判断命令是否是isInside
   if (st.equals("isInside")) {
    System.out.print("请指定圆心的x坐标:");
    float x = sc.nextFloat();
    System.out.print("请指定圆心的y坐标:");
    float y = sc.nextFloat();
    System.out.print("请指定半径的长度:");
    float r= sc.nextFloat();
    // c.setRadius(sc.nextFloat());
    System.out.print("请输入测试点的x坐标:");
    p.setX(sc.nextFloat());
    System.out.print("请输入测试点的y坐标:");
    p.setY(sc.nextFloat());
    // c.setP(p);
    
    c = new Circle(x, y, r);
    if (c.isInside(p)) {
     System.out.println("isInside的结果 为:测试点(" + p.getX() + ","
       + p.getY() + ")在你所指定的圆");

    } else {
     System.out.print("isInside的结果为:测试点(" + p.getX() + ","
       + p.getY() + ")不在你所指定的圆");
    }
   }
   //判断命令outerRect
   else if (st.equals("outerRect")) {
    Rectangle r = new Rectangle();
    Circle cc = new Circle();
    System.out.print("请输入矩形的左下角的x坐标:");
    float x=sc.nextFloat();//r.setPoint(p);
    //r.getPoint().setX(sc.nextFloat());

    System.out.print("请指定矩形的左下角点的y坐标:");
    float y=sc.nextFloat();//r.getPoint().setY(sc.nextFloat());
    System.out.print("请指定矩形的宽:");
    float w=sc.nextFloat();//r.setWidth(sc.nextFloat());
    System.out.print("请指定矩形的高:");
    float h=sc.nextFloat();//r.setHeight(sc.nextFloat());
    r=new Rectangle(x,y,w,h);//r.setPoint(p);
    cc.outerRect(r);
    System.out.print("outerRect的结果为:矩形(" + x
      + "," + y + "," + w + ","
      + h + ")的外接圆为(" + cc.outerRect(r)+ ")");
   } 
   //判断命令join
   else if (st.equals("join")) {
    Circle c1 = new Circle();
    Circle c2 = new Circle();
    Circle c3 = new Circle();
    System.out.print("请输入圆1的x坐标:");
    float x1 = sc.nextFloat();
    System.out.print("请输入圆1的y坐标:");
    float y1 = sc.nextFloat();
    System.out.print("请输入圆2的x坐标:");
    float x2 = sc.nextFloat();
    System.out.print("请输入圆2的y坐标:");
    float y2 = sc.nextFloat();
    System.out.print("请输入圆1的半径:");
    float r1 = sc.nextFloat();
    System.out.print("请输入圆2的半径:");
    float r2 = sc.nextFloat();
    c1 = new Circle(x1, y1, r1);
    c2 = new Circle(x2, y2, r2);
   c3.join(c1, c2);
    //c.setP(p);
                System.out.print(c3.join(c1, c2));
   }
   //判断 命令distance
   else if (st.equals("distance")) {
    Circle c1 = new Circle();
    Circle c2 = new Circle();
    System.out.print("请输入圆1的x坐标:");
    float x1 = sc.nextFloat();
    System.out.print("请输入圆1的y坐标:");
    float y1 = sc.nextFloat();
    System.out.print("请输入圆2的x坐标:");
    float x2 = sc.nextFloat();
    System.out.print("请输入圆2的y坐标:");
    float y2 = sc.nextFloat();
    c1 = new Circle(x1, y1, 0);
    c2 = new Circle(x2, y2, 0);
    System.out.print("两个圆心的距离是:" + c.distance(c1, c2));

   }
   //输入离开
   else if (st.equals("quit")) {
    System.out.println("结束 ");
    break;

   }
   //输入错误
   else {
    System.out.println("对不起,你输入的命令有误,请重新输入。");
   }
  }
 }
}

转载于:https://my.oschina.net/u/3715015/blog/1570239

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值