導航:首頁 > 編程語言 > java點類

java點類

發布時間:2023-06-05 21:31:21

java定義一個點類Point, 具備坐標系中的橫坐標x, 和縱坐標y, 並實現如下功能:

Point類


publicclassPoint{
privatefloatx;
privatefloaty;
publicPoint(floatx,floaty){
this.x=x;
this.y=y;
}

publicfloatgetX(){
returnx;
}

publicvoidsetX(floatx){
this.x=x;
}

publicfloatgetY(){
returny;
}

publicvoidsetY(floaty){
this.y=y;
}

publicfloatdistanceToOrigin(){
return(float)Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2));
}

publicfloatdistanceToOther(intx,inty){
return(float)Math.sqrt(Math.pow(this.x-x,2)+Math.pow(this.y-y,2));
}

publicfloatdistanceToOther(Pointpoint){
return(float)Math.sqrt(Math.pow(this.x-point.x,2)+Math.pow(this.y-point.y,2));
}
}

測試類

publicclassTestPoint{
publicstaticvoidmain(String[]args){
Pointp1=newPoint(3,5);
Pointp2=newPoint(7,8);
System.out.println(p1.distanceToOrigin());
System.out.println(p2.distanceToOrigin());
System.out.println(p1.distanceToOther(20,30));
System.out.println(p2.distanceToOther(20,30));
System.out.println(p1.distanceToOther(p2));
}
}
閱讀全文

與java點類相關的資料

熱點內容
紅包廣告平台源碼 瀏覽:768
硬碟格式化時用的dos命令是 瀏覽:940
找人緩解壓力 瀏覽:933
iphone的pdf 瀏覽:346
90壓縮餅干怎麼吃 瀏覽:664
php教材下載 瀏覽:910
什麼解壓密碼最好 瀏覽:586
資料庫與伺服器如何連接 瀏覽:440
架構師需要閱讀的源碼 瀏覽:479
ch編譯器 瀏覽:454
java必須自己寫一個編譯器嗎 瀏覽:942
如何製作androidrom 瀏覽:474
單片機萬能板怎麼寫入程序 瀏覽:25
邁銳寶xl壓縮比 瀏覽:344
靠演算法買彩票 瀏覽:501
程序員考核d 瀏覽:242
自助游中國pdf 瀏覽:751
安卓p40是什麼手機 瀏覽:91
24cxx編程器 瀏覽:595
陰陽師如何查看哪個伺服器有ID 瀏覽:320