今現在、アメリカの大学でオブジェクト指向について学んでおりアサイメントを書いています。わからない問題は別のクラスで使ったメゾットを違うメゾットで使う方法です。PlaceInformation classにおいてGeoLocation Classのメゾットを使って重複を避けたいのですがそれの引用方法がわかりません。
英語で学んでおり日本語で自分がわからないことを伝えるのが少し難しいのですがよろしくお願いします。
java
1public class PlaceInformation{ 2 private String name1; 3 private String address1; 4 private String tag1; 5 private double latitude1; 6 private double longitude1; 7 8 public PlaceInformation(String name, String address, String tag, double latitude, double longitude){ 9 name1 = name; 10 address1 = address; 11 tag1 = tag; 12 latitude1 = latitude; 13 longitude1 = longitude; 14 } 15 16 public String getName(){ 17 return name1; 18 } 19 20 public String getAddress(){ 21 return address1; 22 } 23 24 public String getTag(){ 25 return tag1; 26 } 27 28 public String toString(){ 29 return name1 + " (latitude: " + latitude1 + ", longitude: " + longitude1 + ")"; 30 } 31 32 public GeoLocation getLocation(){ 33 //ここがわかりません 34 } 35 36 public double distanceFrom(GeoLocation spot){ 37 //ここがわかりません 38 } 39}
java 参照したいクラス
// This class stores information about a location on Earth. Locations are // specified using latitude and longitude. The class includes a method for // computing the distance between two locations. public class GeoLocation { public static final double RADIUS = 3963.1676; // Earth radius in miles private double latitude; private double longitude; // constructs a geo location object with given latitude and longitude public GeoLocation(double theLatitude, double theLongitude) { latitude = theLatitude; longitude = theLongitude; } // returns the latitude of this geo location public double getLatitude() { return latitude; } // returns the longitude of this geo location public double getLongitude() { return longitude; } // returns a string representation of this geo location public String toString() { return "latitude: " + latitude + ", longitude: " + longitude; } // returns the distance in miles between this geo location and the given // other geo location public double distanceFrom(GeoLocation other) { double lat1 = Math.toRadians(latitude); double long1 = Math.toRadians(longitude); double lat2 = Math.toRadians(other.latitude); double long2 = Math.toRadians(other.longitude); // apply the spherical law of cosines with a triangle composed of the // two locations and the north pole double theCos = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2); double arcLength = Math.acos(theCos); return arcLength * RADIUS; } }
クライアントコード
// This program constructs several PlaceInformation objects and prints // information about them and the distances between them and two locations // (London and Kane Hall). It is intended to be used to test whether the // PlaceInformation class is implemented correctly. public class PlaceInformationClient { public static void main(String[] args) { PlaceInformation[] data = {new PlaceInformation("Space Needle", "Seattle Center", "tourist", 47.6205063, -122.3492774), new PlaceInformation("Husky Union Building", "4001 Stevens Way", "restaurant", 47.6554785, -122.3050906), new PlaceInformation("UW Guggenheim Hall", "UW campus", "university, aa", 47.6545769, -122.3064568)}; GeoLocation london = new GeoLocation(51.5112139, -0.1198244); GeoLocation kane = new GeoLocation(47.6566273, -122.3091503); for (PlaceInformation info : data) { System.out.println("name : " + info.getName()); System.out.println("address : " + info.getAddress()); System.out.println("tags : " + info.getTag()); System.out.println("toString: " + info); System.out.println("London : " + info.distanceFrom(london)); System.out.println("Kane : " + info.distanceFrom(kane)); System.out.println(); } } }
回答1件
あなたの回答
tips
プレビュー