# Searching Earthquake Data(2)

#### 2017-11-2(四)

## Programming Exercise: Searching Earthquake Data(2)

今天完成剩下4個assignment

> 1. Filtering by Depth
> 2. Filtering by Phrase in Title
> 3. Finding the Closest Earthquakes to a Location
> 4. Finding the Largest Magnitude Earthquakes

#### Assignment 2: Filtering by Depth

* [code link](https://github.com/YenKang/Java-Programming-Principles-of-Software-Design/blob/master/Assignment%202-Filtering%20by%20Depth/EarthQuakeClient.java)
* 基本的depth大小判定
* Output 圖片 ![](/files/-LcGjhW1fdovQjYYtmCs)

#### Assignment 3: Filtering by Phrase in Title

* [code link](https://github.com/YenKang/Java-Programming-Principles-of-Software-Design/blob/master/Assignment%203-Filtering%20by%20Phrase%20in%20title/EarthQuakeClient.java)
* 關鍵在判斷出phase在info的哪一個位置，是start, end, any三種情況，
* title.startsWith(phrase) 這是重要語法
* 如圖： ![](/files/-LcGjhW3Hs7ytZZQeZrD)

#### Assignment 4: Finding the Closest Earthquakes to a Location

* [code link](https://github.com/YenKang/Java-Programming-Principles-of-Software-Design/tree/master/Assignment%204-Finding%20the%20Closest%20earthquakes%20to%20a%20%20location)
* 因為不是找出最短距離，而是找出最小的4個最短距離，所以要有index去判斷每一個排序的data
* 如圖： ![](/files/-LcGjhW5r3fTiiqF_Uyb)
* 關鍵code如下：

```java
public ArrayList<QuakeEntry> getClosest(ArrayList<QuakeEntry> quakeData, Location current, int howMany) {
        ArrayList<QuakeEntry> ret = new ArrayList<QuakeEntry>();
        // TO DO
        for(int i=0; i< howMany; i++){
            ret.add(quakeData.get(i));
        }

        // for each quake in quakeData
        for(QuakeEntry qe:quakeData){
            // Calculate distance between quakeData quake and current location
            Location qeLocation = qe.getLocation();
            float qeDistance = current.distanceTo(qeLocation);
            System.out.println("Distance between quakeData quake and current location: " + qeDistance);

            // for each quake in ret
            for(QuakeEntry quake: ret){
                // Calculate distance between ret quake and current location

                Location qeRet = quake.getLocation();
                float retDistance = current.distanceTo(qeRet);
                System.out.println("Distance between ret quake and current location: " + retDistance);

                // If quakeData quake is closer than ret quake
                if(qeDistance < retDistance){
                    // replace ret quake with quakeData quake
                    int index = ret.indexOf(quake);
                    ret.remove(index);
                    ret.add(index, qe);

                    System.out.println(retDistance + " " + qeDistance);
                    System.out.println("Added quake to pos " + index);
                    // Check next quakeData quake
                    break;
                }
            }


        }

        System.out.println(ret.size());
        return ret;
    }
```

#### Assignment 5: Finding the Largest Magnitude Earthquakes

* [code link](https://github.com/YenKang/Java-Programming-Principles-of-Software-Design/blob/master/Assignment%205-Finding%20the%20largest%20magnitude%20earthquakes/LargestQuakes.java)
* 關鍵在於判斷，ArrayList largest是否已經達到 howMany的上限，還有dataRemovable裡面是否還有data，並且在執行每一次步驟時，dataRemovable裡的data要移除

```java
 public ArrayList<QuakeEntry> getLargest(ArrayList<QuakeEntry> data, int howMany){
        ArrayList<QuakeEntry> dataRemovable = data; 
        ArrayList<QuakeEntry> largest = new ArrayList<QuakeEntry>();

        // Until either largest contains howMany elements or data runs out
        while(largest.size() < howMany || dataRemovable.size() == 0){
            int index = indexOfLargest(dataRemovable);
            largest.add(dataRemovable.get(index));
            dataRemovable.remove(index);
        }

        return largest;
    }
```

* 結果圖：

  ![](/files/-LcGjhW7FODThQDtv2Mp)

### 總結：

* 我覺得簡單的filter不見得就代表容易，很多skill還要逐步積累，儘管上述assignment並不是太難，但我已經花了早上將近3-4小時去implement，我明白現在還很菜，不過我知道我會越來越好的，重點是要好好做好每一個步驟。
* 當中有很多問題，像是pharse的判斷，我就不知道有startsWith() 這個語法，還有第五題我也不知道要用while，這些後來是參考別人的思路，然後才寫出來的。
* 儘管是參考他人，也要問自己，下次要用這個方法，會用在哪些場景中？能找到哪些data?
* 根據過往經驗，與其快速下一個我學到很多的經驗，不如留一些問題給自己，以便日後再回來看此筆記時，看看自己有沒有進步。

> - 這種找前三名、倒數5名的邏輯，是否能用while以外的用法呢？


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yenkang-2.gitbook.io/free-writing/searching-earthquake-data2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
