Free Writing
  • Introduction
  • 過去、現在、未來(1)
  • Searching Earthquake Data(1)
  • Searching Earthquake Data(2)
  • test
  • Recommendation week1
  • An Alert for the World
  • 容錯系統(一)
Powered by GitBook
On this page
  • Programming Exercise: Searching Earthquake Data(2)
  • 總結:

Was this helpful?

Searching Earthquake Data(2)

PreviousSearching Earthquake Data(1)Nexttest

Last updated 6 years ago

Was this helpful?

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

  • 基本的depth大小判定

  • Output 圖片

Assignment 3: Filtering by Phrase in Title

  • 關鍵在判斷出phase在info的哪一個位置,是start, end, any三種情況,

  • title.startsWith(phrase) 這是重要語法

  • 如圖:

Assignment 4: Finding the Closest Earthquakes to a Location

  • 因為不是找出最短距離,而是找出最小的4個最短距離,所以要有index去判斷每一個排序的data

  • 關鍵code如下:

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

  • 關鍵在於判斷,ArrayList largest是否已經達到 howMany的上限,還有dataRemovable裡面是否還有data,並且在執行每一次步驟時,dataRemovable裡的data要移除

 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;
    }
  • 結果圖:

總結:

  • 我覺得簡單的filter不見得就代表容易,很多skill還要逐步積累,儘管上述assignment並不是太難,但我已經花了早上將近3-4小時去implement,我明白現在還很菜,不過我知道我會越來越好的,重點是要好好做好每一個步驟。

  • 當中有很多問題,像是pharse的判斷,我就不知道有startsWith() 這個語法,還有第五題我也不知道要用while,這些後來是參考別人的思路,然後才寫出來的。

  • 儘管是參考他人,也要問自己,下次要用這個方法,會用在哪些場景中?能找到哪些data?

  • 根據過往經驗,與其快速下一個我學到很多的經驗,不如留一些問題給自己,以便日後再回來看此筆記時,看看自己有沒有進步。

  • 這種找前三名、倒數5名的邏輯,是否能用while以外的用法呢?

如圖:

code link
code link
code link
code link