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:Step One
  • 步驟一:逐行逐字讀文檔,對照source code
  • 步驟二:Assignment:FirstRatings.java

Was this helpful?

Recommendation week1

2017-12-28

Programming Exercise:Step One

  • 前言:在上完Advanced Software Engineering後,發在testing的另外意涵,

"Testing can only show the the presence of errors, not their absence"

  • 看完這句後,發現Duke Java的quiz中的 Test case 都是精心設計的,起初還覺得很煩瑣,為什麼要輸入這麼多data,現在才慢慢理解,要有一個好的software,這是不可省略的步驟,甚至,我重新看Programming exercise的requirement,到implement的過程,並且第二次去看code,熟練自己的搬磚能力。

步驟一:逐行逐字讀文檔,對照source code

  • 此文檔寫出有三個file的java檔,分別是Movie.java, Rating.java, Rater.java,盡可能英翻中為自己理解的句子

Movie.java

  • 八種 private variables

  • 八個 method的data擷取

  • 修改 public String toString(),目的是representing movie information

Raing.java

  • 目的是存one movie與他的rating值

  • 兩個private variables,一個是String item, 另一個 double value

  • 兩個method,來取值

  • 另外兩個method,一個是修改toString,另一個是 compareTo(),rating值的比較

Rater.java

  • 兩個private variables, myID-這是rater的特別ID, myRating-代表 an ArrayList of Ratings

  • 有六個method,addRating, hasRating, getID, getRating, numRatings, getItemsRated,

  • addRating(String item, double rating) ,因為 ArrayList myRatings,用

myRatings.add(new Rating(item, rating))

後者是, new Rating(item, rating),也能當作是current rating

  • hasRating(String item) 目的是確定 myRatings 裡的,確定item是否有再myRatings裡面

  • String getID(),取得此rater's ID,也就是myID

  • public double getRating(String item),輸入item 的 parameter(movie的ID),去查詢次電影在我的myRatings中,rating分數是多少

  • numRatings 目的是回復rating number of ratings

  • getItemRated,把所有rated movie都存進 list裡面,顯示所有被評論過的電影

步驟二:Assignment:FirstRatings.java

LoadMovies:

  • 沒有parameter的帶入,單純讀file ratedmovies_short.csv,並且用parser的格式,去解析資料,並且把資料存進arrayList movieList裡面,

      FileResource fr = new FileResource(filename);
    
      ArrayList<Movie> movieList = new ArrayList<Movie>(); 
    
      for(CSVRecord record:fr.getCSVParser()){
         // Movie currMovie = new Movie(record.get("id"));
         // movieList.add(currMovie);
      }
      return movieList;

testLoadMovies:

    1. call void method named loadMovies on the file ratedmovie_short.csv and print the number of

Results

number of movies: 5

loadedMovie:

[Movie [id=0006414, title=Behind the Screen, year=1916, genres= Short, Comedy, Romance],

Movie [id=0068646, title=The Godfather, year=1972, genres= Crime, Drama],

Movie [id=0113277, title=Heat, year=1995, genres= Action, Crime, Drama],

Movie [id=1798709, title=Her, year=2013, genres= Drama, Romance, Sci-Fi],

Movie [id=0790636, title=Dallas Buyers Club, year=2013, genres= Biography, Drama]]

2.How many movies include the Comedy genre? (多少電影是喜劇類別?)

for(int k=0; k<loadedMovies.size();k++){

   Movie currMovie = loadedMovies.get(k);

    if(currMovie.getGenres().contains("Comedy")){
       genreCount++;
    }

}

  • 3.how many movies are greater than 150 mins

if(currMovie.getMinutes()>150){ moreThan150min++;}

  • 4.哪一部movie 被最多導演directored過

// 用hashmap存each movie with directors

HashMap directorCounts = new HashMap();

// 取得current movie的director

String currDirector = currMovie.getDirector();

// 把data放進 directorCounts的hashmap中

if(directorCounts.containsKey(currDirector)){
    directorCounts.put(currDirector, directorCounts.get(currDirector));
}

else{
    directorCounts.put(currDirector,1)
}
int dirWithMaxMovies = Collections.max(directorCounts.values());
ArrayList<String> movieWithMaxdirs = new ArrayList();
for(String dir:directorCounts.keySet()){
    // 如果 dirWithMaxMovies 數字一樣
    if(directorCounts.get(dir)== dirWithMaxMovies){
        movieWithMaxdirs.add(dir)
    }
}

loadRaters

  • 跟loadMovies有些類似,一樣也要read file

FileResource fr = new FileResource(filename)

// create two kind of arraylist

ArrayList raters = new ArrayList();

ArrayList raterIDList = new ArrayList()

ArrayList movieList = new ArrayList();

    for(CSVRecord record:fr.getCSVParser()){
        String rater_id = record.get("rater_id");
        String movie_id = record.get("movie_id");
        double rating = Double.parseDouble(record.get("rating"));

    }
  • 細節參考source code

testLoadRaters

  • print total number of raters 作法與 movie類似

find the number of ratings for a particular rating

  • 首先需要,兩個input 的 papameter,分別是(ArrayList raters,String rater_id))

public int getRatingsPerRater(ArrayList<Rater> raters, String rater_id){
        int numOfRatingsPerRater=0;
        for(Rater currRater:raters){
            if(currRater.getID().equals(rater_id)){
               numOfRatingsPerRater +=1;
            }
        }
        return numOfRatingsPerRater;
    }

how many different movies have been rated by all these raters

// - 與求movieWithMaxdirs 類似,建立一個hashmap去存data

HashMap<String, Integer> movieRatingCounts = new HashMap();

        for(int k=0;k<loadedRaters.size();k++){
            Rater currRater = loadedRaters.get(k);
            for(int i=0;i< currRater.numRatings();i++){

                String currMovieID = currRater.getItemsRated().get(i);

                if(movieRatingCounts.containsKey(currMovieID)){
                    movieRatingCounts.put(currMovieID, movieRatingCounts.get(currMovieID)+1);
                }
                else{
                    movieRatingCounts.put(currMovieID, 1);
                }
            }
        }
PrevioustestNextAn Alert for the World

Last updated 6 years ago

Was this helpful?