1. 程式人生 > >java作業 類和物件

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;
	}
}