ITI0011:harjutus 12

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

Kirjeldus

Realiseerida säutsude pärimise funktsionaalsus. Sisendiks on asukoht (linn) ja tulemuseks prinditakse selle asukoha säutsud.

Säutsud tuleb enne printimist salvestada listi, mille element on Tweet tüüpi (interface HW2 kodutööst). Loote uue klassi, mis implementeerib seda interface'i. Loodavas klassis peab lisaks olema alistatud (override) meetod toString().

Märkus. Tweet.getUser() peaks tagastama twitter4j puhul status.getUser().getScreenName(), kus status on üks otsingu tulemus (ehk säuts objekt).

Mall

Allpool on antud interface'id, mis on kõik võetud teisest kodutööst. Te peate iga interface'i jaoks looma implementatsiooni (oma klassi, mis implementeerib antud interface'i: public class SomeVeryReadableAndCoolNameTwitterSearch implements TwitterSearch { ). Peate ära implementeerima kõik meetodid, mis interface on ette määranud.

TwitterQuery.java:

Andmeobjekt, mis hoiab vajalikke väärtusi twitteri päringu tegemiseks. Sisaldab gettereid ja settereid. Selle klassi instants antakse ette twitteri päringule (ehk siis twitteri päring kasutab andmeid sellest objektist). <source lang="java">

/**

* Data object for twitter query parameters.
* It holds all the necessary values to make the
* twitter search. The location is stored for caching.
* When the data is read from the cache, the 
* main location should also be stored here.
* If the geographical data is missing, the main location
* (or the getLocation() value) is used to make the
* query from twitter API.
* @author Ago
*
*/

public interface TwitterQuery { public void setLatitude(double latitude); public void setLongitude(double longitude); public void setRadius(double radius); public void setLocation(String location); /** * The count of tweets to query. * @param count Count of tweets to query */ public void setCount(int count);

public double getLatitude(); public double getLongitude(); public double getRadius(); public String getLocation(); public int getCount();

/** * Checks whether the given instance has necessary * parameters set. This is used in case of caching - * if location exist but all the parameters do * not exist, the cache needs to be updated. * @return Whether latitude, longitude and radius are set */ public boolean isGeoSet(); }

</source>

TwitterSearch.java:

Peamine funktsionaalsus läheb siia klassi: loete andmed kasutades twitter4j teeki ja tagastate nimekirja säutsudest. <source lang="java">

import java.util.List;

/**

* Twitter search functionality. The implementation
* of this interface should be able to search tweets
* from Twitter API.
* @author Ago
*
*/

public interface TwitterSearch { /** * Given an object with query parameters send a * query to Twitter API, reads out tweet information * and returns a list of Tweet objects. * @param query Query parameters to be used for search * @return A list of Tweet objects */ public List<Tweet> getTweets(TwitterQuery query);

}

</source>

Tweet.java:

Andmeobjekt ühe säutsu jaoks. Sisaldab gettereid ja settereid <source lang="java">

import java.util.Date;

/**

* Data object for a tweet. Holds the text,
* the author and the timestamp.
* The methods are regular getters/setters.
* @author Ago
*
*/

public interface Tweet { public String getText(); public void setText(String text);

public String getUser(); public void setUser(String user);

public Date getTimestamp(); public void setTimestamp(Date timestamp); }

</source>

Teie põhiline fail, kust programm käima läheb, võib olla selline:

Ärge klassi muutujate nimesid ega tüüpe muutke - neid kasutab testimootor. Küll peaksite ära muutma klasside nimed seal, kus te oma klassid loote. BestTwitterSearch on lihtsalt näide, te nimetage oma klassid teistmoodi. Interface'e ärge muutke. <source lang="java"> import java.util.List;


public class Main {

public static TwitterSearch twitterSearch; public static List<Tweet> tweets;

public static void main(String[] args) { init(); // tallinn printTweets(59.4372155, 24.7453688, 5, 10); }

/** * Initialize twitter search object. It's similar * to HW2 constructor, where we have to instantiate the service. */ public static void init() { twitterSearch = new BestTwitterSearch();

}

/** * Given parameters, it prints out the tweets. * It uses the service provided in init() * (or test system can manually set its own service). * Tweets have to be stored in tweets variable (for test system) * @param latitude * @param longitude * @param radius * @param count */ public static void printTweets(double latitude, double longitude, double radius, int count) { TwitterQuery twitterQuery = new BestTwitterQuery(); twitterQuery.setCount(count); twitterQuery.setLatitude(latitude); twitterQuery.setLongitude(longitude); twitterQuery.setRadius(radius); // location is not needed tweets = twitterSearch.getTweets(twitterQuery); // print out for (Tweet t : tweets) { System.out.println(t); }

// the test system will check for tweets }

}

</source>