This commit is contained in:
2026-03-18 00:39:35 -07:00
commit b4fdc98f10
43 changed files with 85012 additions and 0 deletions

Binary file not shown.

71
debugging/Book.java Normal file
View File

@@ -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";
}
}

54
debugging/Debugging.java Normal file
View File

@@ -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;
}
}

View File

@@ -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;
}
}

41
debugging/Media.java Normal file
View File

@@ -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();
}

40
debugging/Testing.java Normal file
View File

@@ -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());
}
}