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

熱點內容
php獲取原始數據 瀏覽:244
pic單片機如何編程 瀏覽:888
javabyte寫文件 瀏覽:319
java獲取類包名 瀏覽:888
ftp命令編碼格式 瀏覽:298
程序員那麼可愛陸離穿正裝 瀏覽:169
源碼的賬號密碼在哪個文件 瀏覽:437
如何在中國農業銀行app綁定銀行卡 瀏覽:990
shopnum1多用戶商城系統源碼 瀏覽:744
紅包廣告平台源碼 瀏覽:772
硬碟格式化時用的dos命令是 瀏覽:942
找人緩解壓力 瀏覽:937
iphone的pdf 瀏覽:351
90壓縮餅干怎麼吃 瀏覽:668
php教材下載 瀏覽:915
什麼解壓密碼最好 瀏覽:590
資料庫與伺服器如何連接 瀏覽:444
架構師需要閱讀的源碼 瀏覽:483
ch編譯器 瀏覽:456
java必須自己寫一個編譯器嗎 瀏覽:944