質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

254閲覧

違うクラスのメゾットをクラス内で使う方法 オブジェクト指向

RyosukeKanazawa

総合スコア16

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2018/11/25 03:58

編集2018/11/25 04:07

今現在、アメリカの大学でオブジェクト指向について学んでおりアサイメントを書いています。わからない問題は別のクラスで使ったメゾットを違うメゾットで使う方法です。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(); } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

swordone

2018/11/25 04:03

オリエント指向って何ですかアガサ・クリスティですか(オブジェクト指向といいたいのか)
RyosukeKanazawa

2018/11/25 04:04

すいません完全に勘違いです オブジェクト指向です
guest

回答1

0

ベストアンサー

GeoLocationで緯度・経度を管理しているのですから、PlaceInformationの緯度・経度情報もGeoLocationで管理すると統一しやすいです。

java

1public class PlaceInformation{ 2 private String name1; 3 private String address1; 4 private String tag1; 5 private GeoLocation location; 6 7 public PlaceInformation(String name, String address, String tag, double latitude, double longitude){ 8 name1 = name; 9 address1 = address; 10 tag1 = tag; 11 location = new GeoLocation(latitude, longitude); 12 } 13}

これでgetLocationはこのフィールドのlocationをそのまま返せばいいですし、
距離の測定もGeoLocationのものをそのまま使えばいいことになります。

java

1 public GeoLocation getLocation(){ 2 return location; 3 } 4 5 public double distanceFrom(GeoLocation spot){ 6 return location.distanceFrom(spot); 7 }

追記
toStringは、GeoLocationからゲッターで取ることも出来ますが、GeoLocationでtoStringをオーバーライドしていますので、次のような書き方も出来ます。

java

1 public String toString() { 2 return name1 + "("+ location +")"; 3 }

投稿2018/11/25 04:11

編集2018/11/25 07:09
swordone

総合スコア20649

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

swordone

2018/11/25 04:15

あとよくある間違い ×メゾット→○メソッド(method)
RyosukeKanazawa

2018/11/25 04:17

そうした場合、今まではtoString()をこのように返していたのですがどのように変更すればよろしいでしょうか?それぞれをもとのように付け加えるべきでしょうか? public String toString(){ return name1 + " (latitude: " + latitude1 + ", longitude: " + longitude1 + ")"; }
RyosukeKanazawa

2018/11/25 04:40

location.getLocationで解決しました!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問