java作业 类和对象

第二题风扇:

我的代码:

package pack1;

public class Fan {
	final int SLOW=1;
	final int MEDIUM=2;
	final int FAST=3;
	
	private int speed=SLOW;
	private boolean on=false;
	private double radius=5;
	public String color="blue";
	
	public void getSpeed() {
		System.out.print(speed);
	}
	public void getOn() {
		System.out.print(on);
	}
	public void getRadius() {
		System.out.print(radius);
	}
	public void getColor() {
		System.out.print(color);
	}
	
	public void setSpeed(int s) {
		speed=s;
	}
	public void setOn(boolean o) {
		on=o;
	}
	public void setRadius(double r) {
		radius=r;
	}
	public void setColor(String c) {
		color=c;
	}
	
	public Fan(){}//	(什么时候初始化数据?)
	public String toString() {
		if (speed!=0)
			on=true;
		String s;
		if(on) 
			s="speed is "+speed+", color is "+color+", radius is "+radius+"\n";
		else
			s="fan is off"+", color is "+color+", radius is "+radius+"\n";
		return s;
	}
	
}
package pack2;
import pack1.Fan;
import java.util.*;
import java.util.ArrayList;

public class text2 {
	public static void main(String[] args) {
		ArrayList<Fan> fan = new ArrayList<Fan>();
		System.out.println("Please enter the speed of fan:(0, 1, 2 or 3)");
		Scanner reader=new Scanner(System.in);
		int speed1;
		for(int i=0;i<10;i++) {
			while(reader.hasNext()) {
			speed1=reader.nextInt();
			if(speed1==0) {
				Fan fan0=new Fan();
				fan.add(fan0);
				break;
			}else if(speed1==1||speed1==2||speed1==3){
				Fan fan1=new Fan();
				fan1.setSpeed(speed1);
				double radius1=Math.random()*(10-3)+3;
				while(radius1==5) {
					radius1=Math.random()*(10-3)+3;
				}
				fan1.setRadius(radius1);
				String[] colors= {"red","orange","yellow","green","blue","white","black"};
				int colorNum=(int)(Math.random()*7);//为什么都是red?括号优先级最高
				fan1.setColor(colors[colorNum]);
				fan.add(fan1);
				break;
			}else {
				System.out.println("Wrong input! Please enter the speed of fan again:(0, 1, 2 or 3)");
			}
			}
		
		}
		reader.close();
		System.out.println(fan);
	}
}

鸿俊的:

package pack1;

public class Fan {
	final int SLOW = 1;
	final int MEDIUM =2;
	final int FAST =3;
	private int speed;
	private boolean on;
	private double radius;
	String color;	
	public Fan()
	{		
		speed = SLOW;
		on = false;
		radius = 5;
		color = "blue";	
	}
	/*---------------------------------------*/
	public void set_speed(int newspeed)
	{
		this.speed = newspeed;
	}
	public void set_on(Boolean newon)
	{
		this.on = newon;
	}
	public void set_radius(double newradius)
	{
		this.radius = newradius;
	}
	public void set_color(String newcolor)
	{
		this.color = newcolor;
	}
	/*---------------------------------------*/
	public int get_speed()
	{
		return this.speed;
	}
	public boolean get_on()
	{
		return this.on;
	}
	public double get_radius()
	{
		return this.radius;
	}
	public String get_color()
	{
		return this.color;
	}

	
	public Fan(int newspeed, int newradius, String newcolor) {
		this.speed = newspeed;
        this.on = true;
        this.radius = newradius;
        this.color = newcolor;
	}
	public String toSring()
	{
		if(this.on)
		{
			return "fan is off"+", color=" + this.color +", radius=" + this.radius ;
		}
		else
		{
			return "Fan{" +"speed=" + this.speed +", color=" + this.color +", radius='" + this.radius + '\'' +'}';

		}
		
	}
}
package pack2;
import java.util.ArrayList;
import java.util.*;

import pack1.Fan;
public class text2 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		ArrayList<Fan> listfan = new ArrayList<Fan>();
		for (int i = 0; i < 4; i++) {
            int speed = scanner.nextInt();
            if( speed == 0){
                Fan fn = new Fan();
                listfan.add(fn);
            }
            else
           {	Random random = new Random();
            	int ranRadius = random.nextInt(10-3+1)+3;
            	while (ranRadius == 5){
            		ranRadius = random.nextInt(10-3+1)+3;
            	}
            //生成颜色的随机字符串
            	int ranColor = random.nextInt(7-1+1)+1;
            	switch (ranColor){
                case 1:{Fan fn = new Fan(speed,ranRadius,"red");listfan.add(fn);}break;
                case 2:{Fan fn = new Fan(speed,ranRadius,"orange");listfan.add(fn);}break;
                case 3:{Fan fn = new Fan(speed,ranRadius,"yellow");listfan.add(fn);}break;
                case 4:{Fan fn = new Fan(speed,ranRadius,"green");listfan.add(fn);}break;
                case 5:{Fan fn = new Fan(speed,ranRadius,"blue");listfan.add(fn);}break;
                case 6:{Fan fn = new Fan(speed,ranRadius,"white");listfan.add(fn);}break;
                case 7:{Fan fn = new Fan(speed,ranRadius,"black");listfan.add(fn);}break;
            	}
           }
		}
	            System.out.println(listfan);
 }
}

他的代码输出特别奇怪,都是地址,为什么?

第三题:

判断三点是否能构成三角形:

1.https://blog.csdn.net/ch717828/article/details/43375303  很麻烦

2.直接判断三点是否共线即可:

https://zhidao.baidu.com/question/587056628063463165.html

目前已经打出来,但是感觉存在问题,浮点数的比较存在精确度问题

package pack1;
public class Point2D {
	public double x;
	public double y;//这里用public是否妥当
	
	public double getX(){
		return x;
	}
	public double getY() {
		return y;
	}
	public void create() {
		x=0.0;
		y=0.0;
	}
	public void create(double x,double y) {
		this.x=x;
		this.y=y;
	}
	public double distance(Point2D b) {
		double disX=this.x-b.x;
		double disY=this.y-b.y;
		double dis2=Math.pow(disX,2)+Math.pow(disY,2);
		return Math.sqrt(dis2);
	}
	public double areaOfTriangle(Point2D b,Point2D c) {
		double ab=distance(b);
		double ac=distance(c);
		double bc=b.distance(c);
		if(ab+bc>ac&&ab+ac>bc&&ac+bc>ab) {
			double p=(ab+bc+ac)/2.0;
			double area=Math.sqrt(p*(p-ab)*(p-ac)*(p-bc));
			return area;
		}
		else {
			return 0;
		}
	}
}
package pack2;
import pack1.Point2D;


public class text {
	public static void main(String[] args) {
		Point2D a=new Point2D();
		Point2D b=new Point2D();
		Point2D c=new Point2D();
		a.create();
		b.create(3.4, 2.5);
		c.create(1.3, 5.0);
		double area=a.areaOfTriangle(b,c);
		if(area!=0) {
			System.out.println(" The area is "+area+". ");
		}
		else{
			System.out.println(" Wrong input! ");
		}
 }
}
package pack2;
import pack1.Point2D;

public class segment {
	private Point2D A;
	private Point2D B;
	segment(Point2D a){
		A=a;
		B.create();
	}
	segment(Point2D a,Point2D b){
		A=a;
		B=b;
	}
	void extension(Point2D a) {
		double aA=A.distance(a);
		double aB=B.distance(a);
		double AB=A.distance(B);
		if(aA+aB==AB||aA+AB==aB||aB+AB==aA) {
			System.out.println("It is on the segment AB or its extension line. "); 
		}
		else {
			System.out.println("It is not on the segment AB or its extension line. "); 
		}
	}
	int relationship(segment l) {
		double k1=(A.y-B.y)/(A.x-B.x);
		double k2=(l.A.y-l.B.y)/(l.A.x-l.B.x);
		if(k1-k2==0) 
			return 1;//精确度修正,注意这一点
		else return 0;
	}
}//浮点数的比较大小,精确度问题,这是这道题的关键点

在求斜率的时候遇到了一点小问题,莫名其妙就不报错了

Point2D:
public double k2(Point2D b) {
		if(this.x==b.x) {
			return Double.POSITIVE_INFINITY;
		}
		else
			return (this.y-b.y)/(this.x-b.x);
	}//斜率不存在的情况怎么处理?表示为无穷大


segment:

double k(segment h) {
		return h.A.k2(h.B);
	}
	double k() {
		return A.k2(B);
	}
	int relationship(segment l) {
		double k1=k();
		double k2=k(l);
		if(k1-k2==0) 
			return 1;//精确度修正,注意这一点.还有直线斜率90度的情况
		else return 0;
	}

大体编写完成,但是报错了:

package pack1;
public class Point2D {
	private double x;
	private double y;//这里用public是否妥当
	
	public double getX(){
		return x;
	}
	public double getY() {
		return y;
	}
	public void create() {
		x=0.0;
		y=0.0;
	}
	public void create(double x,double y) {
		this.x=x;
		this.y=y;
	}
	public double distance(Point2D b) {
		double disX=this.x-b.x;
		double disY=this.y-b.y;
		double dis2=Math.pow(disX,2)+Math.pow(disY,2);
		return Math.sqrt(dis2);
	}
	public double areaOfTriangle(Point2D b,Point2D c) {
		double ab=distance(b);
		double ac=distance(c);
		double bc=b.distance(c);
		if(ab+bc>ac&&ab+ac>bc&&ac+bc>ab) {
			double p=(ab+bc+ac)/2.0;
			double area=Math.sqrt(p*(p-ab)*(p-ac)*(p-bc));
			return area;
		}
		else {
			return 0;
		}
	}
	public double k2(Point2D b) {
		if(this.x==b.x) {
			return Double.POSITIVE_INFINITY;
		}
		else
			return (this.y-b.y)/(this.x-b.x);
	}//斜率不存在的情况怎么处理?表示为无穷大
}
	



package pack2;
import pack1.Point2D;

public class segment {
	private Point2D A;
	private Point2D B;
	segment(Point2D a){
		A=a;
		B.create();
	}
	segment(Point2D a,Point2D b){
		A=a;
		B=b;
	}
	void extension(Point2D a) {
		double aA=A.distance(a);
		double aB=B.distance(a);
		double AB=A.distance(B);
		if(aA+aB==AB||aA+AB==aB||aB+AB==aA) {
			System.out.println("It is on the segment AB or its extension line. "); 
		}
		else {
			System.out.println("It is not on the segment AB or its extension line. "); 
		}
	}
	double k(segment h) {
		return h.A.k2(h.B);
	}
	double k() {
		return A.k2(B);
	}
	int relationship(segment l) {
		double k1=k();
		double k2=k(l);
		if(k1-k2==0) 
			return 1;//精确度修正,注意这一点.还有直线斜率90度的情况
		else return 0;
	}
}//浮点数的比较大小,精确度问题,这是这道题的关键点


package pack2;
import pack1.Point2D;
import java.util.*;


public class text {
	public static void main(String[] args) {
		segment[] list=new segment[5];
		Point2D a=null;
		Point2D b=null;
		Scanner read=new Scanner(System.in);
		System.out.println("请问你需要系统产生随机数,还是手动输入?选择随机数请输入1,手动输入请输入2");//产生随机数or手动输入
		while(read.hasNext()) {
			int num=read.nextInt();
			if(num==1) {
				for(int i=0;i<5;i++) {
					a.create(Math.random()*10, Math.random()*10);
					b.create(Math.random()*10, Math.random()*10);
					list[i]=new segment(a,b);
				}
				break;
			}
			if(num==2){
				for(int i=0;i<5;i++) {
					System.out.println(" 请依次输入线段两端点的坐标值: ");
					System.out.printf(" a: ");
					a.create(read.nextDouble(),read.nextDouble());
					System.out.printf(" b: ");
					b.create(read.nextDouble(),read.nextDouble());
					list[i]=new segment(a,b);
				}
				break;
			}
			else {
				System.out.println("输入错误!请重新输入1或2");
			}
		}
		read.close();
		for(int i=0;i<5;i++) {
			for(int j=i+1;j<5;j++) {
				int key=list[i].relationship(list[j]);
				if(key==1) {
					System.out.println("线段list"+i+"与线段list"+j+"平行");//判断线段是否相交?天哦
				}
				else {
					System.out.println("线段list"+i+"与线段list"+j+"所在直线相交");
				}
			}
		}
	}
}




报错:

请问你需要系统产生随机数,还是手动输入?选择随机数请输入1,手动输入请输入2
1
Exception in thread "main" java.lang.NullPointerException

请问你需要系统产生随机数,还是手动输入?选择随机数请输入1,手动输入请输入2
2
 请依次输入线段两端点的坐标值: 
 a: 5 6
Exception in thread "main" java.lang.NullPointerException
    at project1/pack2.text.main(text.java:27)

报错没有问题了,是因为创建对象的时候没有初始化,改成:

Point2D a=new Point2D();
Point2D b=new Point2D();

即可解决报错问题。

目前存在的问题是,求斜率的函数有问题,不知什么情况求的都是同一个值

package pack1;
public class Point2D {
	private double x;
	private double y;//这里用public是否妥当
	
	public double getX(){
		return x;
	}
	public double getY() {
		return y;
	}
	public void create() {
		x=0.0;
		y=0.0;
	}
	public void create(double x,double y) {
		this.x=x;
		this.y=y;
	}
	public void get() {
		System.out.printf("  ("+this.x+","+this.y+") ");
	}
	public double distance(Point2D b) {
		double disX=this.x-b.x;
		double disY=this.y-b.y;
		double dis2=Math.pow(disX,2)+Math.pow(disY,2);
		return Math.sqrt(dis2);
	}
	public double areaOfTriangle(Point2D b,Point2D c) {
		double ab=distance(b);
		double ac=distance(c);
		double bc=b.distance(c);
		if(ab+bc>ac&&ab+ac>bc&&ac+bc>ab) {
			double p=(ab+bc+ac)/2.0;
			double area=Math.sqrt(p*(p-ab)*(p-ac)*(p-bc));
			return area;
		}
		else {
			return 0;
		}
	}
	public double k2(Point2D b) {
		if(this.x==b.x)
			return Double.POSITIVE_INFINITY;
		else
			System.out.println((this.y-b.y)/(this.x-b.x));
			return (this.y-b.y)/(this.x-b.x);
	}//斜率不存在的情况怎么处理?表示为无穷大
}
	


package pack2;
import pack1.Point2D;

public class segment {
	Point2D A;
	 Point2D B;
	segment(Point2D a){
		A=a;
		B.create();
	}
	segment(Point2D a,Point2D b){
		A=a;
		B=b;
	}
	void extension(Point2D a) {
		double aA=A.distance(a);
		double aB=B.distance(a);
		double AB=A.distance(B);
		if(aA+aB==AB||aA+AB==aB||aB+AB==aA) {
			System.out.println("It is on the segment AB or its extension line. "); 
		}
		else {
			System.out.println("It is not on the segment AB or its extension line. "); 
		}
	}
	double k(segment h) {
		//System.out.println(h.A+","+h.B);
		return h.A.k2(h.B);
	}
	double k() {
		return A.k2(B);
	}
	int relationship(segment l) {
		double k1=k();
		double k2=k(l);
		//System.out.println(l);每一条直线都有相同的元素,只能是赋值上有了问题
		if(k1-k2==0.0) 
			return 1;//精确度修正,注意这一点.还有直线斜率90度的情况
		else return 0;
	}
}//浮点数的比较大小,精确度问题,这是这道题的关键点



package pack2;
import pack1.Point2D;
import java.util.*;


public class text {
	public static void main(String[] args) {
		segment[] list=new segment[5];
		Point2D a=new Point2D();
		Point2D b=new Point2D();
		Scanner read=new Scanner(System.in);
		System.out.println("请问你需要系统产生随机数,还是手动输入?选择随机数请输入1,手动输入请输入2");//产生随机数or手动输入
		while(read.hasNext()) {
			int num=read.nextInt();
			if(num==1) {
				for(int i=0;i<5;i++) {
					a.create(Math.random()*10, Math.random()*10);
					b.create(Math.random()*10, Math.random()*10);
					System.out.printf(" a ");
					//a.get();
					System.out.printf(" b ");
					//b.get();
					System.out.println();
					list[i]=new segment(a,b);
					//list[i].A.get();//为什么没有定义?A设为私有,外界不能访问
					//list[i].B.get();//经过检验,赋值是成功的
				}
				break;
			}
			if(num==2){
				for(int i=0;i<5;i++) {
					System.out.println(" 请依次输入线段两端点的坐标值: ");
					System.out.printf(" a: ");
					a.create(read.nextDouble(),read.nextDouble());
					System.out.printf(" b: ");
					b.create(read.nextDouble(),read.nextDouble());
					list[i]=new segment(a,b);
				}
				break;
			}
			else {
				System.out.println("输入错误!请重新输入1或2");
			}
		}
		read.close();
		for(int i=0;i<5;i++) {
			for(int j=i+1;j<5;j++) {
				int key=list[i].relationship(list[j]);
				if(key==1) {
					System.out.println("线段list"+i+"与线段list"+j+"平行");//判断线段是否相交?天哦
				}
				else {
					System.out.println("线段list"+i+"与线段list"+j+"所在直线相交");
				}
			}
		}
	}
}

测试的过程中出现了奇怪的现象:

int relationship(segment l) {
		double k1=(A.y-B.y)/(A.x-B.x);
		double k2=(l.A.y-l.B.y)/(l.A.x-l.B.x);
		System.out.println(l.A.x+" "+l.B.x);
		System.out.println(k1+" "+k2);
		if(k1-k2==0.0) 
			return 1;//精确度修正,注意这一点.还有直线斜率90度的情况
		else return 0;
	}

真的奇怪!对每一条不同的直线输出它的坐标居然都是一个值,那么只能是赋值方面有问题

请问你需要系统产生随机数,还是手动输入?选择随机数请输入1,手动输入请输入2
1
 a   (8.55523692839673,7.868518961991748)  b   (2.262983989693641,0.26576325760056085) 
 a   (7.327012431086665,7.997733999341152)  b   (9.349496174740914,7.652227225672106) 
 a   (4.209056232052285,2.309849511190909)  b   (0.24965209557152201,2.6504861092847056) 
 a   (7.075205762058752,0.7841508219106896)  b   (0.762495716361905,2.196237439335543) 
 a   (4.88770323053067,9.526691945010208)  b   (4.895289016581971,7.08821927635043) 
4.88770323053067 4.895289016581971
-321.4528662117397 -321.4528662117397
线段list0与线段list1平行
4.88770323053067 4.895289016581971
-321.4528662117397 -321.4528662117397
线段list0与线段list2平行
4.88770323053067 4.895289016581971
-321.4528662117397 -321.4528662117397
线段list0与线段list3平行
4.88770323053067 4.895289016581971
-321.4528662117397 -321.4528662117397
线段list0与线段list4平行
4.88770323053067 4.895289016581971
-321.4528662117397 -321.4528662117397
线段list1与线段list2平行
4.88770323053067 4.895289016581971
-321.4528662117397 -321.4528662117397
线段list1与线段list3平行
4.88770323053067 4.895289016581971
-321.4528662117397 -321.4528662117397
线段list1与线段list4平行
4.88770323053067 4.895289016581971
-321.4528662117397 -321.4528662117397
线段list2与线段list3平行
4.88770323053067 4.895289016581971
-321.4528662117397 -321.4528662117397
线段list2与线段list4平行
4.88770323053067 4.895289016581971
-321.4528662117397 -321.4528662117397
线段list3与线段list4平行

又检验了一下,发现每一次调用判断是否平行的函数时,传入的两条直线都是最后一条,自然平行。

请问你需要系统产生随机数,还是手动输入?选择随机数请输入1,手动输入请输入2
1
 a   (8.056947862802186,6.313295895183951)  b   (8.381272929600275,3.223321023552775) 
  (8.056947862802186,6.313295895183951)  a   (9.511281769412621,7.414990266305254)  b   (4.2112692944242855,6.033580187810351) 
  (9.511281769412621,7.414990266305254)  a   (3.907782559679347,9.774255461254986)  b   (2.709671133239513,1.2151575161222394) 
  (3.907782559679347,9.774255461254986)  a   (9.833752935340076,7.558961190032214)  b   (1.538796359932526,1.4726693774475075) 
  (9.833752935340076,7.558961190032214)  a   (0.8003758893200896,4.792113722956123)  b   (2.360136714005127,3.464228976963949) 
  (0.8003758893200896,4.792113722956123) 0.8003758893200896 2.360136714005127
-0.8513386956364377 -0.8513386956364377
线段list0与线段list1平行
0.8003758893200896 2.360136714005127
-0.8513386956364377 -0.8513386956364377
线段list0与线段list2平行
0.8003758893200896 2.360136714005127
-0.8513386956364377 -0.8513386956364377
线段list0与线段list3平行
0.8003758893200896 2.360136714005127
-0.8513386956364377 -0.8513386956364377
线段list0与线段list4平行
0.8003758893200896 2.360136714005127
-0.8513386956364377 -0.8513386956364377
线段list1与线段list2平行
0.8003758893200896 2.360136714005127
-0.8513386956364377 -0.8513386956364377
线段list1与线段list3平行
0.8003758893200896 2.360136714005127
-0.8513386956364377 -0.8513386956364377
线段list1与线段list4平行
0.8003758893200896 2.360136714005127
-0.8513386956364377 -0.8513386956364377
线段list2与线段list3平行
0.8003758893200896 2.360136714005127
-0.8513386956364377 -0.8513386956364377
线段list2与线段list4平行
0.8003758893200896 2.360136714005127
-0.8513386956364377 -0.8513386956364377
线段list3与线段list4平行

果然是赋值的问题!就好像我的 list[ ] 数组中只定义了一个元素,不断地替换掉进行新的赋值!

for(int i=0;i<5;i++) {
					a.create(Math.random()*10, Math.random()*10);
					b.create(Math.random()*10, Math.random()*10);
					System.out.printf(" a ");
					a.get();
					System.out.printf(" b ");
					b.get();
					System.out.println();
					list[i]=new segment(a,b);
					list[i].A.get();//为什么没有定义?A设为私有,外界不能访问
					//list[i].B.get();//经过检验,赋值是成功的
					if(i!=0) {
						System.out.println(list[i-1].A.x+" "+list[i-1].A.y);
					}
				}
1
 a   (6.279862048167228,8.12095389061181)  b   (8.49974281997981,3.2257996319832625) 
  (6.279862048167228,8.12095389061181)  a   (7.253383892935874,5.967408886168206)  b   (4.689529770353676,2.541689606627978) 
  (7.253383892935874,5.967408886168206) 7.253383892935874 5.967408886168206
 a   (8.032938460848872,9.815391851259294)  b   (9.098251434708159,7.112691830180646) 
  (8.032938460848872,9.815391851259294) 8.032938460848872 9.815391851259294
 a   (4.58092800451436,1.068320645695141)  b   (8.372492082021449,2.4585309165286287) 
  (4.58092800451436,1.068320645695141) 4.58092800451436 1.068320645695141
 a   (9.507451644658339,3.6278142077468334)  b   (7.469254153180852,4.9365956296622056) 
  (9.507451644658339,3.6278142077468334) 9.507451644658339 3.6278142077468334

果然每一个 list 中的元素都被覆盖了。

目前我代码的问题就是对象数组在每一次赋值的时候都覆盖了前面对的值,但是每个i难道对应的地址不是新的吗

终于搞定了

package pack1;
public class Point2D {
	private double x;
	private double y;
	
	public double getX(){//获取坐标
		return x;
	}
	public double getY() {
		return y;
	}
	public Point2D() {//初始化
		x=0.0;
		y=0.0;
	}
	public Point2D(double x,double y) {
		this.x=x;
		this.y=y;
	}
	public Point2D(int x,int y) {
		this.x=x;
		this.y=y;
	}
	public void get() {//输出点的坐标位置
		System.out.printf("  ("+this.x+","+this.y+") ");
	}
	public double distance(Point2D b) {//求与另一点之间距离
		double disX=this.x-b.x;
		double disY=this.y-b.y;
		double dis2=Math.pow(disX,2)+Math.pow(disY,2);
		return Math.sqrt(dis2);
	}
	public double areaOfTriangle(Point2D b,Point2D c) {//判断与另外两点是否能组成三角形,能则输出面积
		double ab=distance(b);
		double ac=distance(c);
		double bc=b.distance(c);
		if(ab+bc>ac&&ab+ac>bc&&ac+bc>ab) {
			double p=(ab+bc+ac)/2.0;
			double area=Math.sqrt(p*(p-ab)*(p-ac)*(p-bc));
			return area;
		}
		else {
			return 0;
		}
	}
	public double k2(Point2D b) {//求与另一点构成直线的斜率
		if(x==b.x)
			return Double.POSITIVE_INFINITY;
		else
			return (y-b.y)/(x-b.x);
	}
}
	

package pack2;
import pack1.Point2D;

public class segment {
	private Point2D A;
	private Point2D B;
	public segment(){//初始化
		A= new Point2D();
		B= new Point2D();
	}
	public segment(Point2D a){
		A=a;
		B= new Point2D();
	}
	public segment(Point2D a,Point2D b){
		A=a;
		B=b;
	}//这里没有定义public,为什么text类中可以引用,而point2D就不行
	void extension(Point2D a) {//点是否在线段上
		double aA=A.distance(a);
		double aB=B.distance(a);
		double AB=A.distance(B);
		if(aA+aB==AB||aA+AB==aB||aB+AB==aA) {
			System.out.println("It is on the segment AB or its extension line. "); 
		}
		else {
			System.out.println("It is not on the segment AB or its extension line. "); 
		}
	}
	double k(segment h) {//求线段所在直线斜率
		return h.A.k2(h.B);
	}
	double k() {
		return A.k2(B);
	}
	int relationship(segment l) {//判断两线段关系
		double k1=k();
		double k2=k(l);
		if(k1-k2==0.0) 
			return 1;
		else return 0;
	}
}

猜你喜欢

转载自blog.csdn.net/Perce_Issac/article/details/82960203