diff options
Diffstat (limited to 'debugging')
| -rw-r--r-- | debugging/2XIGPFWWQh6rdNJQSyU34zis | bin | 0 -> 2610 bytes | |||
| -rw-r--r-- | debugging/Book.java | 71 | ||||
| -rw-r--r-- | debugging/Debugging.java | 54 | ||||
| -rw-r--r-- | debugging/InvertedIndex.java | 52 | ||||
| -rw-r--r-- | debugging/Media.java | 41 | ||||
| -rw-r--r-- | debugging/Testing.java | 40 |
6 files changed, 258 insertions, 0 deletions
diff --git a/debugging/2XIGPFWWQh6rdNJQSyU34zis b/debugging/2XIGPFWWQh6rdNJQSyU34zis Binary files differnew file mode 100644 index 0000000..1d097ce --- /dev/null +++ b/debugging/2XIGPFWWQh6rdNJQSyU34zis diff --git a/debugging/Book.java b/debugging/Book.java new file mode 100644 index 0000000..05ef36d --- /dev/null +++ b/debugging/Book.java @@ -0,0 +1,71 @@ +import java.util.*; + +public class Book implements Media { + + private String title; + private String author; + private List<String> authors; + private List<Integer> ratings; + + public Book(String title, String author) { + this.title = title; + this.author = author; + } + + public Book(String title, List<String> authors) { + this.title = title; + this.authors = authors; + } + + public String getTitle() { + return this.title; + } + + public List<String> getArtists() { + List<String> artists = new ArrayList<>(); + if (this.author != null) { + artists.add(this.author); + } + + if (this.authors != null) { + for (String author : authors) { + artists.add(author); + } + } + return artists; + } + + public void addRating(int score) { + if (this.ratings == null) { + ratings = new ArrayList<>(); + } + this.ratings.add(score); + } + + public int getNumRatings() { + + if (this.ratings == null) { + return 0; + } + + return this.ratings.size(); + } + + public double getAverageRating() { + if (this.ratings == null) { + return 0; + } + + int sum = 0; + for (int rating : ratings) { + sum += rating; + } + + return (double)sum / this.ratings.size(); + } + + public String toString() { + return this.title + " by " + this.getArtists() + ": " + this.getAverageRating() + + (this.ratings.size()) + " ratings"; + } +} diff --git a/debugging/Debugging.java b/debugging/Debugging.java new file mode 100644 index 0000000..a9e301f --- /dev/null +++ b/debugging/Debugging.java @@ -0,0 +1,54 @@ +import java.util.*; + +public class Debugging { + public static void main(String[] args) { + Map<String, Set<Integer>> testMap = new TreeMap<>(); + Set<Integer> c121 = arrToSet(new int[]{42, 17, 42, 42}); + Set<Integer> c122 = arrToSet(new int[]{10, 12, 14}); + Set<Integer> c123 = arrToSet(new int[]{100, 99, 98, -97}); + testMap.put("cse121", c121); + testMap.put("cse122", c122); + testMap.put("cse123", c123); + + Map<String, Set<Integer>> deepCopyMap = deepCopy(testMap); + + if (deepCopyMap.isEmpty()) { + System.out.println("{}"); + } else { + String line = ""; + for (String key : deepCopyMap.keySet()) { + line += key + "=" + deepCopyMap.get(key).toString() + ", "; + } + System.out.println("{" + line.substring(0, line.length() - 2) + "}"); + } + } + + public static Set<Integer> arrToSet(int[] arr) { + Set<Integer> s = new TreeSet<>(); + for (int num : arr) { + s.add(num); + } + return s; + } + + // Produces and returns a "deep copy" of the parameter map, which has the same + // structure and values as the parameter, but with all internal data structures + // and values copied. After calling this method, modifying the parameter or + // return value should NOT affect the other. + // + // Parameters: + // inputMap - the map to duplicate + // + // Returns: + // A deep copy of the parameter map. + public static Map<String, Set<Integer>> deepCopy(Map<String, Set<Integer>> inputMap) { + Map<String, Set<Integer>> deepCopy = new TreeMap<>(); + + for (String key : inputMap.keySet()) { + Set<Integer> inputSet = new TreeSet<>(inputMap.get(key)); + // Set<Integer> inputSet = inputMap.get(key); + deepCopy.put(key, inputSet); + } + return deepCopy; + } +} diff --git a/debugging/InvertedIndex.java b/debugging/InvertedIndex.java new file mode 100644 index 0000000..65284f7 --- /dev/null +++ b/debugging/InvertedIndex.java @@ -0,0 +1,52 @@ +import java.util.*; + +public class InvertedIndex { + public static void main(String[] args) { + List<String> docs = new ArrayList<>(); + docs.add("Raiders of the Lost Ark"); + docs.add("The Temple of Doom"); + docs.add("The Last Crusade"); + + Map<String, Set<String>> result = createIndex(docs); + System.out.println(docs); + System.out.println(); + System.out.println(result); + } + + // TODO: Write and document your createIndex method here + + public static Map<String, Set<String>> createIndex(List<String> docs) { + Map<String, Set<String>> index = new TreeMap<>(); + Set<String> uniqueWords = getUniqueWords(docs); + + for (String uniqueWord : uniqueWords) { + index.put(uniqueWord.toLowerCase(), new HashSet<String>()); + } + + for (String word : index.keySet()) { + for (int i = 0; i < docs.size(); i++) { + Scanner wordScanner = new Scanner(docs.get(i)); + while (wordScanner.hasNext()) { + if (wordScanner.next().equalsIgnoreCase(word)) { + index.get(word).add(docs.get(i)); + } + } + } + } + return index; + } + + public static Set<String> getUniqueWords(List<String> docs) { + Set<String> uniqueWords = new HashSet<>(); + for (String title : docs) { + Scanner titleScanner = new Scanner(title); + while (titleScanner.hasNext()) { + uniqueWords.add(titleScanner.next()); + } + titleScanner.close(); + } + return uniqueWords; + } + + +} diff --git a/debugging/Media.java b/debugging/Media.java new file mode 100644 index 0000000..ee9614e --- /dev/null +++ b/debugging/Media.java @@ -0,0 +1,41 @@ +import java.util.*; + +/** + * An interface to represent various types of media (movies, books, tv shows, songs, etc.). + */ +public interface Media { + /** + * Gets the title of this media. + * + * @return The title of this media. + */ + public String getTitle(); + + /** + * Gets all artists associated with this media. + * + * @return A list of artists for this media. + */ + public List<String> getArtists(); + + /** + * Adds a rating to this media. + * + * @param score The score for the new rating. Should be non-negative. + */ + public void addRating(int score); + + /** + * Gets the number of times this media has been rated. + * + * @return The number of ratings for this media. + */ + public int getNumRatings(); + + /** + * Gets the average (mean) of all ratings for this media. + * + * @return The average (mean) of all ratings for this media. If no ratings exist, returns 0. + */ + public double getAverageRating(); +} diff --git a/debugging/Testing.java b/debugging/Testing.java new file mode 100644 index 0000000..a50fe62 --- /dev/null +++ b/debugging/Testing.java @@ -0,0 +1,40 @@ +import org.junit.jupiter.api.*; +import static org.junit.jupiter.api.Assertions.*; +import java.util.*; + +public class Testing { + + @Test + @DisplayName("EXAMPLE TEST CASE - createIndex Example") + public void firstCaseTest() { + List<String> documents = new ArrayList<>(List.of("The Bee Movie is great!", + "I love the Bee Movie", + "Y'all seen Dune 2?")); + Map<String, Set<String>> index = InvertedIndex.createIndex(documents); + + // Make sure that tokens are correctly converted to lower case + assertTrue(index.containsKey("bee")); + assertFalse(index.containsKey("Bee")); + + // Make sure that punctuation is ignored + assertTrue(index.containsKey("great!")); + assertFalse(index.containsKey("great")); + + // Check one of the sets + assertEquals(Set.of("The Bee Movie is great!", + "I love the Bee Movie"), + index.get("movie")); + } + + @Test + @DisplayName("EXAMPLE TEST CASE - Book 2 String constructor + getters") + public void secondCaseTest() { + Book b = new Book("Title", "Author"); + + // Test all getters after constructing with 2 Strings + assertEquals("Title", b.getTitle()); + assertEquals(List.of("Author"), b.getArtists()); + assertEquals(0, b.getNumRatings()); + assertEquals(0.0, b.getAverageRating()); + } +} |
