1.编写一个点类Point
package org.jsoft.Point;
public class Point {
int x;
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;
}
int y;
public Point(int x,int y){
this.x=x;
this.y=y;
}
public void display(){
System.out.println("The point is at:("+getX()+","+getY()+")");
}
}
测试类:
package org.jsoft.Point;
public class TestPoint {
public static void main(String[] args) {
Point p=new Point(0,0);
p.setX(100);
p.setY(42);
p.display();
}
}
结果:The point is at:(100,42)
2.编写一个矩形类Rectangle
package org.jsoft.Rectangle;
import org.jsoft.Point.Point;
public class Rectangle {
Point point;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public void setPoint(int x,int y) {
point=new Point(1,1);
point.setX(x);
point.setY(y);
}
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;
}
int width;
int height;
public Rectangle(Point p,int width,int height){
this.point=p;
this.width=width;
this.height=height;
}
public Rectangle(int x,int y,int width,int height){
point=new Point(0,0);
point.setX(x);
point.setY(y);
this.width=width;
this.height=height;
}
public Rectangle(Rectangle r){
this.width=r.width;
this.height=r.height;
}
public Rectangle(){}
public int getArea(){
return width*height;
}
public void display(){
System.out.println("The rectangle's width and height is:"+getWidth()+","+getHeight());
}
}
测试类:
package org.jsoft.Rectangle;
import org.jsoft.Point.Point;
public class TestRectangle {
public static void main(String[] args){
Rectangle r1=new Rectangle(1,1,50,67);
int area=r1.getArea();
System.out.println("The Rectangle's area:"+area);
r1.display();
Point p1=new Point(1,1);
Rectangle r2=new Rectangle(p1,60,67);
System.out.println("The Rectangle's area:"+r2.getArea());
r2.display();
Rectangle r3=new Rectangle();
r3.width=70;
r3.height=67;
System.out.println("The Rectangle's area:"+r3.getArea());
r3.display();
Rectangle r4=new Rectangle(r3);
System.out.println("The Rectangle's area:"+r4.getArea());
r4.display();
Rectangle r5=new Rectangle();
r5.setPoint(100,100);
System.out.println("("+r5.point.getX()+","+r5.point.getY()+")");
}
}
结果:
The Rectangle's area:3350
The rectangle's width and height is:50,67
The Rectangle's area:4020
The rectangle's width and height is:60,67
The Rectangle's area:4690
The rectangle's width and height is:70,67
The Rectangle's area:4690
The rectangle's width and height is:70,67
(100,100)
3.编写一个圆类Circle
package org.jsoft.Circle;
import org.jsoft.Point.Point;
import org.jsoft.Rectangle.Rectangle;
public class Circle {
Point p;//圆心
float radius;//半径
public Point getP() {
return p;
}
public void setP(Point p) {
this.p = p;
}
public void setP(int x,int y){
p=new Point(0,0);
p.setX(x);
p.setY(y);
}
public float getRadius() {
return radius;
}
public void setRadius(float radius) {
this.radius = radius;
}
public Circle(){}//写了带参数的构造方法不能马上知道方法里面该写什么
public Circle(Point p,float r){
this.p=p;
this.radius=r;
}
public Circle(int x,int y,float r){
p=new Point(0,0);
p.setX(x);
p.setY(y);
this.radius=r;
}
public Circle(Circle c){
this.p=c.p;
this.radius=c.radius;
}
public boolean isInside(Point p){//两点间的距离公式判断在不在圆内
double result=(double)Math.sqrt((p.getX()-this.p.getX())*(p.getX()-this.p.getX())+(p.getY()-this.p.getY())*(p.getY()-this.p.getY()));
if(result<radius)
return true;
else
return false;
}
public Circle outerRect(Rectangle r){//返回一个矩形的外接圆,矩形平行于x轴
Point p=new Point(0,0);
p=r.getPoint();
Circle c=new Circle();
p.setX(r.getWidth()/2+p.getX());
p.setY(r.getHeight()/2+p.getY());
c.radius=(float)Math.sqrt(r.getHeight()*r.getHeight()+r.getWidth()*r.getWidth())/2;
c.setP(p);
return c;
}
public Circle join(Circle c1, Circle c2){//返回两个参数圆的最小外接圆,应为外切圆,两个圆x坐标相同
float f=distance(c1,c2);
Circle c=new Circle();
Point p1=new Point(0,0);
Point p2=new Point(0,0);
p1=c1.getP();
p2=c2.getP();
c.radius=(f-c1.radius-c2.radius)/2;
if(p1.getX()<p2.getX()){
c.setP((p2.getX()-(int)(c2.radius+c.radius)), p2.getY());
}
else{
c.setP((p1.getX()-(int)(c1.radius+c.radius)), p1.getY());
}
return c;
}
public float distance(Circle c1, Circle c2){ //返回两个参数圆圆心之间的距离
float d;
d=(float)Math.sqrt((c1.p.getX()-c2.p.getX())*(c1.p.getX()-c2.p.getX())+(c1.p.getY()-c2.p.getY())*(c1.p.getY()-c2.p.getY()));
return d;
}
public String toString(){
return "("+p.getX()+","+p.getY()+","+radius+")";
}
}
测试类:
package org.jsoft.Circle;
import java.util.Scanner;
import org.jsoft.Point.Point;
import org.jsoft.Rectangle.Rectangle;
public class TestCircle {
public static void main(String[] args) {
Circle c=new Circle();
Scanner sc=new Scanner(System.in);
while(true){
System.out.print("请输入命令:(isInside/outerRect/join/distance/quit):");
String str=sc.next();
if(str.equals("isInside")){
Point p=new Point(0,0);
System.out.print("请指定圆心的x坐标:");
int x=sc.nextInt();
System.out.print("请指定圆心y的坐标:");
int y=sc.nextInt();
c.setP(x, y);
System.out.print("请指定半径的长度:");
c.radius=sc.nextFloat();
System.out.print("请输入测试点的x坐标:");
p.setX(sc.nextInt());
System.out.print("请输入测试点y的坐标:");
p.setY(sc.nextInt());
if(c.isInside(p)){
System.out.println("isInside的结果为:测试点("+p.getX()+","+p.getY()+")在你所指定的圆("+c.p.getX()+","+c.p.getY()+","+c.radius+")内");
}
else{
System.out.println("isInside的结果为:测试点("+p.getX()+","+p.getY()+")不在你所指定的圆("+c.p.getX()+","+c.p.getY()+","+c.radius+")内");
}
}
else if(str.equals("outerRect")){
Rectangle r=new Rectangle();
Circle c1=new Circle();
System.out.print("请指定矩形的左下角点的x坐标:");
int x=sc.nextInt();
System.out.print("请指定矩形左下角点的y坐标:");
int y=sc.nextInt();
r.setPoint(x, y);
System.out.print("请指定矩形的宽:");
r.setWidth(sc.nextInt());
System.out.print("请指定矩形的高:");
r.setHeight(sc.nextInt());
c1=c.outerRect(r);
System.out.println("outerRect的结果为:矩形("+x+","+y+","+r.getWidth()+","+r.getHeight()+")的外接圆为:("+c1.p.getX()+","+c1.p.getY()+","+c1.radius+")");
}
else if(str.equals("join")){
Circle c1=new Circle();
Circle c2=new Circle();
Circle c3=new Circle();
System.out.print("请指定第一个圆的圆心x坐标:");
int x1=sc.nextInt();
System.out.print("请指定第一个圆的圆心y坐标:");
int y1=sc.nextInt();
c1.setP(x1, y1);
System.out.print("请指定第一个圆的半径:");
c1.radius=sc.nextFloat();
System.out.print("请指定第二个圆的圆心x坐标:");
int x2=sc.nextInt();
System.out.print("请指定第二个圆的圆心y坐标:");
int y2=sc.nextInt();
c2.setP(x2, y2);
System.out.print("请指定第二个圆的半径:");
c2.radius=sc.nextFloat();
c3=c.join(c1, c2);
System.out.println("join的结果为:两个圆的最小外切圆为:("+c3.p.getX()+","+c3.p.getY()+","+c3.radius+")");
}
else if(str.equals("distance")){
Circle c1=new Circle();
Circle c2=new Circle();
System.out.print("请指定第一个圆的圆心x坐标:");
int x1=sc.nextInt();
System.out.print("请指定第一个圆的圆心y坐标:");
int y1=sc.nextInt();
c1.setP(x1, y1);
System.out.print("请指定第二个圆的圆心x坐标:");
int x2=sc.nextInt();
System.out.print("请指定第二个圆的圆心y坐标:");
int y2=sc.nextInt();
c2.setP(x2, y2);
float d=c.distance(c1, c2);
System.out.println("两个圆圆心之间的距离为:"+d);
}
else if(str.equals("quit")){
System.out.println("程序结束");
break;
}
else{
System.out.println("对不起,你输入的指令有误,请重新输入!");
}
}
}
}