導航:首頁 > 編程語言 > 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點類相關的資料

熱點內容
phpppt下載 瀏覽:298
1929pdf 瀏覽:364
編譯器是終端嗎 瀏覽:521
pdf改b4 瀏覽:379
命令通道 瀏覽:690
pdf去 瀏覽:543
嵌入式編譯器優化 瀏覽:127
不同品牌安卓一鍵換機用什麼軟體 瀏覽:957
二年下冊運演算法則 瀏覽:137
蘭溪兩級壓縮空壓機 瀏覽:137
網頁如何取回伺服器上的文件 瀏覽:96
linuxphp重啟命令行 瀏覽:575
為什麼我的所有app都登錄不了 瀏覽:558
別樣app海淘怎麼樣 瀏覽:512
聯通app扣費是什麼意思 瀏覽:387
android應用程序管理員許可權 瀏覽:85
石家莊十萬程序員 瀏覽:974
java繼承類實例 瀏覽:289
疫情期間程序員的工資 瀏覽:30
最好的python編譯器 瀏覽:189