Java黑皮书课后题第10章:10.4(MyPoint类)设计一个名为MyPoint的类,代表一个以x坐标和y坐标表示的点
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第10章:10.4(MyPoint类)设计一个名为MyPoint的类,代表一个以x坐标和y坐标表示的点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
10.4(MyPoint類)設計一個名為MyPoint的類,代表一個以x坐標和y坐標表示的點
- 題目
- 程序
- 代碼
- Test4.java
- Test4_MyPoint.java
- 運行實例
- UML
題目
程序
Test4.java:測試程序
Test4_MyPoint.java:構造程序
代碼
Test4.java
public class Test4 {public static void main(String[] args) {Test4_MyPoint mp = new Test4_MyPoint();System.out.println(mp.distance(10, 30.5));// 優化輸出:System.out.printf("%.4f", mp.distance(10, 30.5));} }Test4_MyPoint.java
public class Test4_MyPoint {// 坐標double x, y;// 無參構造方法public Test4_MyPoint(){x = 0;y = 0;}// 有參構造方法public Test4_MyPoint(double x, double y){this.x = x;this.y = y;}// distance方法:到MyPoint類public double distance(Test4_MyPoint mp){return Math.sqrt((mp.x - this.x) * (mp.x - this.x) + (mp.y - this.y) * (mp.y - this.y));}// distance方法:到兩個坐標值public double distance(double x, double y){return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y));} }運行實例
32.09750769140807 32.0975UML
總結
以上是生活随笔為你收集整理的Java黑皮书课后题第10章:10.4(MyPoint类)设计一个名为MyPoint的类,代表一个以x坐标和y坐标表示的点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第10章:10.3(
- 下一篇: java美元兑换,(Java实现) 美元