summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorpants <niklasjo2004@gmail.com>2026-09-21 14:17:15 -0700
committerpants <niklasjo2004@gmail.com>2026-09-21 14:17:15 -0700
commit3813621611945a0be23384c0c4d395b4c4a614ad (patch)
tree1812fd440839272e7dd16f5635e180769a66f5f1 /java
Diffstat (limited to 'java')
-rw-r--r--java/MineSweeper.java99
-rw-r--r--java/Music.java132
-rw-r--r--java/MusicPlaylist.java135
-rw-r--r--java/Review.java30
-rw-r--r--java/Stonks.java169
-rw-r--r--java/WordStats.java75
-rw-r--r--java/absurdle/Absurdle.java188
-rw-r--r--java/absurdle/small_dict.txt9
-rw-r--r--java/helloworld.java8
-rw-r--r--java/printer.java7
-rw-r--r--java/stonks.tsv7
-rw-r--r--java/variables.java10
12 files changed, 869 insertions, 0 deletions
diff --git a/java/MineSweeper.java b/java/MineSweeper.java
new file mode 100644
index 0000000..22b8966
--- /dev/null
+++ b/java/MineSweeper.java
@@ -0,0 +1,99 @@
+// Miya Natsuhara
+// CSE 122
+// 1-5-2024
+
+import java.util.*;
+
+// This program randomly-generates and displays a MineSweeper board according to the difficulty
+// level specified by the user.
+
+public class MineSweeper {
+ public static final int BOARD_SIZE = 15; // assume a board size board, square
+ public static final int MINE_INDICATOR = -1;
+
+ public static void main(String[] args) {
+ int[][] board = new int[BOARD_SIZE][BOARD_SIZE];
+
+ int difficultyLevel = intro(); // introduce game and get level of difficulty (more mines -> harder)
+ int numMines = getNumMines(difficultyLevel);
+ generateMines(numMines, board); // generate coordinates for all the mines
+ updateCounts(board); // updates the board with all the counts of mines
+ displayBoard(board);
+ }
+
+ // Prints a welcome message and prompts the user for difficulty level (between 1 and 10).
+ // Restricts the user's response to this range and returns the result.
+ public static int intro() {
+ Scanner console = new Scanner(System.in);
+ System.out.println("Welcome to MineSweeper!");
+ System.out.print("Please choose game difficulty (NOTE: Difficulty: 1 to 10): ");
+ int userNum = console.nextInt();
+ if (userNum > 10) {
+ userNum = 10;
+ } else if (userNum < 1) {
+ userNum = 1;
+ }
+ return userNum;
+ }
+
+ // Given a difficulty level, returns the number of mines that should be placed on the board.
+ public static int getNumMines(int difficultyLevel) {
+ return difficultyLevel * (BOARD_SIZE / 5); // arbitrary
+ }
+
+ // Given the desired number of mines and game board, randomly places the specified number of
+ // mines throughout the board.
+ public static void generateMines(int numMines, int[][] board) {
+ Random r = new Random();
+ for (int i = 0; i < numMines; i++) {
+ int xCoord = r.nextInt(BOARD_SIZE);
+ int yCoord = r.nextInt(BOARD_SIZE);
+ board[xCoord][yCoord] = MINE_INDICATOR;
+ }
+ }
+
+ // Given the board and a location (x, y) to check, returns the number of mines adjacent to
+ // location (x, y).
+ public static int placeCount(int[][] board, int x, int y) {
+ int count = 0;
+ for (int i = -1; i < 2; i++) {
+ for (int j = -1; j < 2; j++) {
+ int xCoord = x + i;
+ int yCoord = y + j;
+ boolean validSquare = xCoord >= 0 && xCoord < BOARD_SIZE &&
+ yCoord >= 0 && yCoord < BOARD_SIZE;
+ if (validSquare && board[xCoord][yCoord] == MINE_INDICATOR) {
+ count++;
+ }
+ }
+ }
+ return count;
+ }
+
+ // Given the game board, places the appropriate numbers in non-mine cells throughout the
+ // board.
+ public static void updateCounts(int[][] board) {
+ for (int i = 0; i < BOARD_SIZE; i++) {
+ for (int j = 0; j < BOARD_SIZE; j++) {
+ if (board[i][j] != MINE_INDICATOR) {
+ board[i][j] = placeCount(board, i, j);
+ }
+ }
+ }
+ }
+
+ // Prints the board to the console, with each square's contents evenly-spaced.
+ public static void displayBoard(int[][] board) {
+ for (int i = 0; i < BOARD_SIZE; i++) {
+ for (int j = 0; j < BOARD_SIZE; j++) {
+ if (board[i][j] == MINE_INDICATOR) {
+ System.out.print("X ");
+ } else {
+ System.out.print(board[i][j] + " ");
+ }
+ }
+ System.out.println();
+ }
+ System.out.println();
+ }
+}
diff --git a/java/Music.java b/java/Music.java
new file mode 100644
index 0000000..082c5b0
--- /dev/null
+++ b/java/Music.java
@@ -0,0 +1,132 @@
+// Nik Johnson
+// 1-7-2024
+// CSE 122
+
+import java.util.*;
+
+public class Music {
+ public static final String NOTES = "CDEFGAB";
+ public static final String SHARP = "♯";
+ public static final String FLAT = "♭";
+
+ public static void main(String[] args) {
+ Scanner scanner = new Scanner(System.in);
+ String[][] song = composeSong(scanner);
+ mostCommonNaturals(song);
+ System.out.println(Arrays.toString(mostCommonNaturals(song)));
+ }
+
+// "parent" method that calls helper methods getSongParams and composeMelodies
+// returns 2D array containing user-composed song
+
+ public static String[][] composeSong(Scanner scanner) {
+
+ int[] songParams = getSongParams(scanner);
+
+ return composeMelodies(scanner, songParams[0], songParams[1]);
+ }
+
+ // get user input on song parameters, return for use with composeMelodies
+ public static int[] getSongParams(Scanner scanner) {
+
+ System.out.print("Enter the number of melodies: ");
+ String inputNum = scanner.nextLine();
+ int numMelodies = Integer.parseInt(inputNum);
+
+ System.out.print("Enter the length of each melody: ");
+ String inputLength = scanner.nextLine();
+ int lengthMelodies = Integer.parseInt(inputLength);
+
+
+
+ return new int[] {numMelodies, lengthMelodies};
+ }
+ // iterate through array of dimensions defined by list songParams, storing user-inputted notes as we go
+ public static String[][] composeMelodies(Scanner scanner, int numMelodies, int lengthMelodies) {
+ System.out.println("");
+
+ String[][] songArray = new String[numMelodies][lengthMelodies];
+
+ for (int n = 0; n < numMelodies; n++) {
+
+ System.out.println("Composing melody #" + (n+1));
+
+ for (int i = 0; i < lengthMelodies; i++) {
+ System.out.print(" Enter note #" + (i+1) + ": ");
+ String note = scanner.nextLine();
+ songArray[n][i] = note;
+
+
+ if (numMelodies > 1) {
+
+ } else if (lengthMelodies > 1) {
+ System.out.println();
+ }
+ }
+ System.out.println();
+ }
+ return songArray;
+ }
+
+ // parent method, use data from helper methods to assemble output array
+ public static String[] mostCommonNaturals(String[][] song) {
+ // new function
+
+ // string array thats # of melodies long -> since there will be that many notes
+
+ String[] notes = NOTES.split("");
+
+ String[] result = new String[song.length];
+ for (int i = 0; i < result.length; i++) {
+ // get frequency list PER melody
+ int[] numNaturals = getNumNaturals(song[i]);
+
+ // get largest frequency # in list
+
+ int largestFreq = getLargestFrequency(numNaturals);
+
+ // put note matching that largest frequency (in order of NOTES) into result array
+ // "CDEFGAB";
+ // [0, 1, 2, 1, 2, 0, 0]
+ // 2
+ // result = ["E"]
+
+ for (int n = 0; n < numNaturals.length; n++) {
+ if (numNaturals[n] == largestFreq) {
+ result[i] = notes[n];
+ break;
+ }
+ }
+ }
+
+
+ return result;
+ }
+
+ // look for frequency of note that appears most frequently in song
+ public static int getLargestFrequency(int[] numNaturals) {
+ int freq = 0;
+ for (int j = 0; j < numNaturals.length; j++) {
+ if (numNaturals[j] > freq) {
+ freq = numNaturals[j];
+ }
+ }
+ return freq;
+ }
+
+ public static int[] getNumNaturals(String[] melody) {
+ int[] numNaturals = new int[NOTES.length()];
+
+ for (int n = 0; n < melody.length; n++) {
+ String note = melody[n];
+ if (note.length() == 1) {
+ int index = NOTES.indexOf(note);
+ numNaturals[index]++;
+ }
+
+ }
+ return numNaturals;
+ }
+
+
+}
diff --git a/java/MusicPlaylist.java b/java/MusicPlaylist.java
new file mode 100644
index 0000000..b127dd7
--- /dev/null
+++ b/java/MusicPlaylist.java
@@ -0,0 +1,135 @@
+// Nik Johnson
+// 1-31-2024
+// TA: Andy Ruan
+
+import java.util.*;
+
+// music playlist manager with functionality to add and play songs while keeping track of play history,
+// which can be cleared, or deleted from in batches
+public class MusicPlaylist {
+
+ public static void main(String[] args) {
+ Scanner console = new Scanner(System.in);
+ Queue<String> playlist = new LinkedList<>();
+ Stack<String> history = new Stack<>();
+ System.out.println("Welcome to the CSE 122 Music Playlist!");
+
+ String destination = navigator(console);
+ while (!destination.equalsIgnoreCase("Q")) {
+ if (destination.equalsIgnoreCase("A")) {
+ addSong(console, playlist);
+ } else if (destination.equalsIgnoreCase("P")) {
+ playSong(playlist, history);
+ } else if (destination.equalsIgnoreCase("Pr")) {
+ printHistory(history);
+ } else if (destination.equalsIgnoreCase("C")) {
+ clearHistory(history);
+ } else if (destination.equalsIgnoreCase("D")) {
+ deleteFromHistory(console, history);
+ }
+ destination = navigator(console);
+ }
+ }
+
+ // menu function, called initially and after any other functions
+ // return String for use in main menu loop
+ public static String navigator(Scanner console) {
+ System.out.println("(A) Add song");
+ System.out.println("(P) Play song");
+ System.out.println("(Pr) Print history");
+ System.out.println("(C) Clear history");
+ System.out.println("(D) Delete from history");
+ System.out.println("(Q) Quit");
+ System.out.println();
+ System.out.print("Enter your choice: ");
+ String destination = console.nextLine();
+ return destination;
+ }
+
+ // add song to playlist according to user input
+ // no return
+ public static void addSong(Scanner console, Queue<String> playlist) {
+ System.out.print("Enter song name: ");
+ String songToAdd = console.nextLine();
+ playlist.add(songToAdd);
+ System.out.println("Successfully added " + songToAdd);
+ System.out.println();
+ System.out.println();
+ }
+
+ // play song at the front of the playlist
+ // no return
+ public static void playSong(Queue<String> playlist, Stack<String> history) {
+ // if user attempts to play song when playlist is empty, throw exception
+ if (playlist.isEmpty()) throw new IllegalStateException();
+ String songToPlay = playlist.remove();
+ System.out.println("Playing song: " + songToPlay);
+ history.push(songToPlay);
+ System.out.println();
+ System.out.println();
+ }
+
+ // print history of played songs
+ // return unmodified history after printing it
+ public static void printHistory(Stack<String> history) {
+ int size = history.size();
+ // if there is no history and user attempts to print history, throw exception
+ if (history.isEmpty()) throw new IllegalStateException();
+ String[] historyArray = new String[size];
+ if (size != 0) {
+ for (int i = (size - 1); i >= 0; i--) {
+ historyArray[i] = history.pop();
+ System.out.println(" " + historyArray[i]);
+ }
+ for (int n = 0; n < size; n++) {
+ history.push(historyArray[n]);
+ }
+ }
+ System.out.println();
+ System.out.println();
+ }
+
+ // clear history when called
+ // return empty history
+ public static void clearHistory(Stack<String> history) {
+ history.clear();
+ }
+
+ // delete from history according to user input, starting either from most recent or oldest history
+ // return modified history
+ public static void deleteFromHistory(Scanner console, Stack<String> history) {
+ int size = history.size();
+ List<String> historyArrayList = new ArrayList<>();
+ if (!history.isEmpty()) {
+ for (int i = 0; i < size; i++) {
+ historyArrayList.add(history.pop());
+ }
+ }
+ System.out.println("A positive number will delete from recent history.");
+ System.out.println("A negative number will delete from the beginning of history.");
+ System.out.print("Enter number of songs to delete: ");
+ int numToDelete = Integer.parseInt(console.nextLine());
+ int absNum = Math.abs(numToDelete);
+ if (absNum > size) {
+ throw new IllegalArgumentException();
+ }
+ System.out.println();
+ if (size != 0 && absNum != 0) {
+ if (absNum == size) {
+ historyArrayList.clear();
+ } else if (numToDelete < 0) {
+ int lastIndex = size - 1;
+ for (int n = lastIndex; n >= (size - absNum); n--) {
+ historyArrayList.remove(n);
+ }
+ } else if (numToDelete > 0) {
+ for (int i = 0; i < numToDelete; i++) {
+ historyArrayList.remove(0);
+ }
+ }
+ }
+ for (int n = (historyArrayList.size() - 1); n >= 0; n--) {
+ history.push(historyArrayList.get(n));
+ }
+ }
+}
diff --git a/java/Review.java b/java/Review.java
new file mode 100644
index 0000000..b8376db
--- /dev/null
+++ b/java/Review.java
@@ -0,0 +1,30 @@
+import java.util.*;
+
+public class Review {
+ // Create a Map of students in section to their favorite word/movie/song/etc
+ // Then, edit and print out the Map
+
+ public static void main(String[] args) {
+ // Create the Map (do you want it to do ordered?)
+ Map<String, String> favorites = new TreeMap<>();
+ // Add key/value pairs to the Map
+ favorites.put("me", "キュ毛付きさぼたじ");
+ favorites.put("nik", "warp star");
+ favorites.put("andy", "none");
+ System.out.println(favorites);
+ // Delete one of the entries from the Map
+ favorites.remove("andy");
+ // Override one of the values
+ favorites.put("boner", "balls");
+ // Loop over the Map and print out all the values seperated by a comma and space
+ // Before printing - hypothesize what the output will look like!
+ for (String key : favorites.keySet()) {
+ System.out.print(favorites.get(key) + ", ");
+ }
+ // two ways to do this:
+ // 1st way - loop through keys then get values
+
+ // 2nd way - loop through values
+
+ }
+} \ No newline at end of file
diff --git a/java/Stonks.java b/java/Stonks.java
new file mode 100644
index 0000000..58fd703
--- /dev/null
+++ b/java/Stonks.java
@@ -0,0 +1,169 @@
+// Nik Johnson
+// 1-25-2024
+// TA: Andy Ruan
+// modified attempt for resubmission 1
+
+import java.util.*;
+import java.io.*;
+
+// stock simulator
+// stock market sandbox which reads fake market data from "stonks.tsv", and allows users to:
+// buy and sell stocks, and save their portfolio to a file.
+// upon quitting, the value of the users portfolio will be calculated and printed.
+
+public class Stonks {
+ public static final String STOCKS_FILE_NAME = "stonks.tsv";
+
+ public static void main(String[] args) throws FileNotFoundException {
+ Scanner console = new Scanner(System.in);
+ File stonks = new File(STOCKS_FILE_NAME);
+ Scanner stonklines = new Scanner(stonks);
+ int numStocks = Integer.parseInt(stonklines.nextLine());
+
+ String[] tickers = new String[numStocks];
+ Double[] prices = new Double[numStocks];
+ Double[] portfolio = new Double[numStocks];
+
+ // fill portfolio with zeros to prevent null exceptions
+ // without using Arrays.fill()
+ for (int n = 0; n < numStocks; n++) {
+ portfolio[n] = 0.0;
+ }
+
+ String info = stonklines.nextLine();
+
+ // this runs once.
+ // functionally decomposing it would add lines of code, but no functionality.
+ int index = 0;
+ while (stonklines.hasNextLine()) {
+ String nextline = stonklines.nextLine();
+ Scanner token = new Scanner(nextline);
+ tickers[index] = token.next();
+ prices[index] = token.nextDouble();
+ index++;
+ }
+
+ System.out.println("Welcome to the CSE 122 Stocks Simulator!");
+ System.out.println("There are " + numStocks + " stocks on the market:");
+
+ for (int i = 0; i < numStocks; i++) {
+ System.out.println(tickers[i] + ": " + prices[i]);
+
+ }
+
+ System.out.println();
+ String next = navigator(console);
+
+ while (!next.equals("quit")) {
+ if (next.equals("buy")) {
+ portfolio = buy(console, tickers, prices, portfolio);
+ } else if (next.equals("sell")) {
+ portfolio = sell(console, tickers, prices, portfolio);
+ } else if (next.equals("save")) {
+ save(console, tickers, portfolio);
+ } else {
+ System.out.println("Invalid choice: " + next);
+ System.out.println("Please try again");
+ System.out.println();
+ }
+ next = navigator(console);
+ }
+
+ Double value = quit(prices, portfolio);
+ System.out.println("Your portfolio is currently valued at: $" + value);
+ }
+
+ // menu method, called for inital prompt and then after using any other methods
+ // returns String which decides which function to use
+
+ public static String navigator(Scanner console) {
+ System.out.println("Menu: buy, sell, save, quit");
+ System.out.print("Enter your choice: ");
+ String destination = console.next();
+ return destination;
+ }
+
+ // add (buy) to portfolio according to user input, no lower than $5.0
+ // takes console scanner, and all data arrays defined in main lines 24-26
+ // return modified portfolio with MORE shares than before
+
+ public static Double[] buy(Scanner console, String[] tickers,
+ Double[] prices, Double[] portfolio) {
+ System.out.print("Enter the stock ticker: ");
+ String order = console.next();
+ System.out.print("Enter your budget: ");
+ Double budget = console.nextDouble();
+
+ if (budget >= 5.0) {
+ for (int i = 0; i < tickers.length; i++) {
+ if (order.equals(tickers[i])) {
+ portfolio[i] += (budget / prices[i]);
+ System.out.println("You successfully bought " + tickers[i] + ".");
+ System.out.println();
+ }
+ }
+ } else {
+ System.out.println("Budget must be at least $5");
+ System.out.println();
+ }
+ return portfolio;
+ }
+
+ // subtract (sell) from portfolio according to user input, if they have enough to sell
+ // takes console scanner, and all data arrays defined in main lines 24-26
+ // return modified portfolio with LESS shares than before
+
+ public static Double[] sell(Scanner console, String[] tickers,
+ Double[] prices, Double[] portfolio) {
+ System.out.print("Enter the stock ticker: ");
+ String ticker = console.next();
+ System.out.print("Enter the number of shares to sell: ");
+ Double sellAmount = console.nextDouble();
+ for (int i = 0; i < tickers.length; i++) {
+ if (ticker.equals(tickers[i])) {
+ if (sellAmount > portfolio[i]) {
+ System.out.println("You do not have enough shares of " + ticker + " to sell " + sellAmount + " shares.");
+ System.out.println();
+ } else if (sellAmount <= portfolio[i]) {
+ portfolio[i] = portfolio[i] - sellAmount;
+ System.out.println("You successfully sold " + sellAmount + " shares of " + ticker + ".");
+ System.out.println();
+ }
+ }
+ }
+ return portfolio;
+ }
+
+ // write current portfolio to file named according to user input
+ // takes console scanner, array of tickers and the users current portfolio
+
+ public static void save(Scanner console, String[] tickers, Double[] portfolio) throws FileNotFoundException {
+ System.out.print("Enter new portfolio file name: ");
+ String fileName = console.next();
+ System.out.println();
+ File saveData = new File(fileName);
+ PrintStream savePortfolio = new PrintStream(saveData);
+ for (int i = 0; i < tickers.length; i++) {
+ if (portfolio[i] != 0.0) {
+ savePortfolio.println(tickers[i] + " " + portfolio[i]);
+ }
+ }
+ }
+
+ // upon quitting, calculate total portfolio value using-
+ // -values in portfolio and current market prices
+ // return total value for printing
+
+ public static double quit(Double[] prices, Double[] portfolio) {
+ System.out.println();
+ Double portfolioValue = 0.0;
+ for (int i = 0; i < prices.length; i++) {
+ if (portfolio[i] != null) {
+ Double currValue = prices[i]*portfolio[i];
+ portfolioValue += currValue;
+ } else portfolioValue += 0;
+ }
+ return portfolioValue;
+ }
+
+} \ No newline at end of file
diff --git a/java/WordStats.java b/java/WordStats.java
new file mode 100644
index 0000000..22c3c76
--- /dev/null
+++ b/java/WordStats.java
@@ -0,0 +1,75 @@
+import java.util.*;
+
+public class WordStats {
+ public static void main(String[] args) {
+ Scanner console = new Scanner(System.in);
+
+ String[] words = getListOfWords(console);
+ System.out.println("Words: " + Arrays.toString(words));
+
+ String longestWord = getLongestWord(words);
+ System.out.println("Longest word: " + longestWord);
+
+ vowelSearch(words);
+ }
+
+
+ /*
+ * ask user for number of words to test, and store that (integer) as "number_of_Words"
+ * loop for that number of iterations, adding each word to array "words"
+ */
+ public static String[] getListOfWords(Scanner console) {
+ System.out.print("How many words? ");
+ int numbers_of_WORDS = console.nextInt();
+
+ String[] words = new String[numbers_of_WORDS];
+
+ for (int i = 0; i < numbers_of_WORDS; i++) {
+ System.out.print("Next word? ");
+ String Word = console.next();
+ words[i] = Word;
+ }
+
+ return words;
+ }
+
+ /*
+ * iterate through array "words"
+ * return longest word
+ */
+
+ public static String getLongestWord(String[] words) {
+ String longestWord = "";
+ for (int i = 0; i < words.length; i++) {
+ String curr = words[i];
+ if (curr.length() > longestWord.length()) {
+ longestWord = curr;
+ }
+ }
+ return longestWord;
+
+ }
+
+ /*
+ * test first character of each word in array "words"
+ * if first char is any defined in upper or lower case vowels, print it
+ */
+
+ public static void vowelSearch(String[] words) {
+ char[] uppercaseVowels = { 'A', 'E', 'I', 'O', 'U' };
+ char[] lowercaseVowels = { 'a', 'e', 'i', 'o', 'u' };
+ System.out.println("Words beginning with vowels: ");
+ for (int i = 0; i < words.length; i++) {
+ String curr = words[i];
+ char firstChar = curr.charAt(0);
+ for (int j = 0; j < uppercaseVowels.length; j++) {
+ if (firstChar == uppercaseVowels[j] || firstChar == lowercaseVowels[j]) {
+ System.out.println(" " + curr);
+ }
+ }
+ }
+
+ }
+
+
+}
diff --git a/java/absurdle/Absurdle.java b/java/absurdle/Absurdle.java
new file mode 100644
index 0000000..8353cbb
--- /dev/null
+++ b/java/absurdle/Absurdle.java
@@ -0,0 +1,188 @@
+// Nik Johnson
+// 2-11-2024
+// TA: Andy Ruan
+
+// TODO: Write your class comment here!
+
+import java.util.*;
+import java.io.*;
+
+public class Absurdle {
+ public static final String GREEN = "🟩";
+ public static final String YELLOW = "🟨";
+ public static final String GRAY = "⬜";
+
+ // [[ ALL OF MAIN PROVIDED ]]
+ public static void main(String[] args) throws FileNotFoundException {
+ Scanner console = new Scanner(System.in);
+ System.out.println("Welcome to the game of Absurdle.");
+
+ System.out.print("What dictionary would you like to use? ");
+ String dictName = console.next();
+
+ System.out.print("What length word would you like to guess? ");
+ int wordLength = console.nextInt();
+
+ List<String> contents = loadFile(new Scanner(new File(dictName)));
+ Set<String> words = pruneDictionary(contents, wordLength);
+
+ List<String> guessedPatterns = new ArrayList<>();
+ while (!isFinished(guessedPatterns)) {
+ System.out.print("> ");
+ String guess = console.next();
+ String pattern = record(guess, words, wordLength);
+ System.out.println(words);
+ guessedPatterns.add(pattern);
+ System.out.println(": " + pattern);
+ System.out.println();
+ }
+ System.out.println("Absurdle " + guessedPatterns.size() + "/∞");
+ System.out.println();
+ printPatterns(guessedPatterns);
+ }
+
+ // [[ PROVIDED ]]
+ // Prints out the given list of patterns.
+ // - List<String> patterns: list of patterns from the game
+ public static void printPatterns(List<String> patterns) {
+ for (String pattern : patterns) {
+ System.out.println(pattern);
+ }
+ }
+
+ // [[ PROVIDED ]]
+ // Returns true if the game is finished, meaning the user guessed the word. Returns
+ // false otherwise.
+ // - List<String> patterns: list of patterns from the game
+ public static boolean isFinished(List<String> patterns) {
+ if (patterns.isEmpty()) {
+ return false;
+ }
+ String lastPattern = patterns.get(patterns.size() - 1);
+ return !lastPattern.contains("⬜") && !lastPattern.contains("🟨");
+ }
+
+ // [[ PROVIDED ]]
+ // Loads the contents of a given file Scanner into a List<String> and returns it.
+ // - Scanner dictScan: contains file contents
+ public static List<String> loadFile(Scanner dictScan) {
+ List<String> contents = new ArrayList<>();
+ while (dictScan.hasNext()) {
+ contents.add(dictScan.next());
+ }
+ return contents;
+ }
+
+ public static Set<String> pruneDictionary(List<String> contents, int wordLength) {
+ if (wordLength < 1) throw new IllegalArgumentException();
+ Set<String> prunedDict = new HashSet<>();
+ for (String word : contents) {
+ if (word.length() == wordLength) {
+ prunedDict.add(word);
+ }
+ }
+ return prunedDict;
+ }
+
+ public static String record(String guess, Set<String> words, int wordLength) {
+ if (guess.length() != wordLength || words.isEmpty()) {
+ throw new IllegalArgumentException();
+ }
+ Map<String, Set<String>> wordMap = new TreeMap<>();
+ Iterator<String> iter = words.iterator();
+ while (iter.hasNext()) {
+ String word = iter.next();
+ String nextPattern = patternFor(word, guess);
+ if (!wordMap.containsKey(nextPattern)) {
+ wordMap.put(nextPattern, new HashSet<String>());
+ // iter.remove();
+ }
+ wordMap.get(nextPattern).add(word);
+ }
+
+ Boolean allSameSize = true;
+ int setSize1 = 0;
+
+ for (String pattern : wordMap.keySet()) {
+ int size = wordMap.get(pattern).size();
+ if (setSize1 == 0) setSize1 = size;
+ if (setSize1 != size) allSameSize = false;
+ }
+
+ if (allSameSize) {
+ for (String pattern : wordMap.keySet()) {
+ removeAllNonMatching(words, wordMap.get(pattern));
+ return pattern;
+ }
+ }
+
+ String pattern = "";
+ int setSize = 0;
+ for (String word : wordMap.keySet()) {
+ int nextSetSize = (wordMap.get(word)).size();
+ if (nextSetSize > setSize) {
+ setSize = nextSetSize;
+ pattern = word;
+ }
+ }
+ removeAllNonMatching(words, wordMap.get(pattern));
+ return pattern;
+ }
+
+ public static void removeAllNonMatching(Set<String> words, Set<String> match) {
+ Iterator<String> iter = words.iterator();
+ while(iter.hasNext()) {
+ if(!match.contains(iter.next())) {
+ iter.remove();
+ }
+ }
+ }
+
+ public static String patternFor(String word, String guess) {
+ List<String> splitGuess = new ArrayList<>();
+ String pattern = new String();
+ for (int i = 0;i < guess.length(); i++) {
+ String nextChar = Character.toString(guess.charAt(i));
+ splitGuess.add(nextChar);
+ }
+
+ Map<Character, Integer> wordMap = new HashMap<>();
+ for (int j = 0; j < word.length(); j++) {
+ char wordChar = word.charAt(j);
+ if (!wordMap.containsKey(wordChar)) {
+ wordMap.put(wordChar, 1);
+ } else {
+ wordMap.put(wordChar, (wordMap.get(wordChar) + 1));
+ }
+ }
+
+ for (int n = 0; n < splitGuess.size(); n++) {
+ if ((Character.toString(word.charAt(n))).equals(splitGuess.get(n))) {
+ int currCount = wordMap.get((splitGuess.get(n)).charAt(0));
+ wordMap.put(word.charAt(n), (currCount - 1));
+ splitGuess.set(n, GREEN);
+ }
+ }
+
+ for (int n = 0; n < splitGuess.size(); n++) {
+ char charToCheck = (splitGuess.get(n)).charAt(0);
+ if ((wordMap.keySet()).contains(charToCheck)
+ && wordMap.get(charToCheck) != 0) {
+ int currCount2 = wordMap.get(charToCheck);
+ wordMap.put(charToCheck, (currCount2 - 1));
+ splitGuess.set(n, YELLOW);
+ }
+ }
+
+ for (int n = 0; n < splitGuess.size(); n++) {
+ if ((splitGuess.get(n)).length() == 1) {
+ splitGuess.set(n, GRAY);
+ }
+ }
+
+ for (int i = 0; i < splitGuess.size(); i++) {
+ pattern += splitGuess.get(i);
+ }
+ return pattern;
+ }
+}
diff --git a/java/absurdle/small_dict.txt b/java/absurdle/small_dict.txt
new file mode 100644
index 0000000..32e2fed
--- /dev/null
+++ b/java/absurdle/small_dict.txt
@@ -0,0 +1,9 @@
+rgrasfally
+beta
+cool
+deal
+else
+flew
+good
+hope
+ibex
diff --git a/java/helloworld.java b/java/helloworld.java
new file mode 100644
index 0000000..f9f58c0
--- /dev/null
+++ b/java/helloworld.java
@@ -0,0 +1,8 @@
+// package src;
+// import src.printer;
+
+public class helloworld {
+ public static void main(String[] args) {
+ System.out.println("hellow");
+ }
+} \ No newline at end of file
diff --git a/java/printer.java b/java/printer.java
new file mode 100644
index 0000000..9392884
--- /dev/null
+++ b/java/printer.java
@@ -0,0 +1,7 @@
+public class printer {
+ public static void main(String[] args) {
+ System.out.println("hellow");
+ System.out.println("hello");
+ System.out.println("helw");
+ }
+} \ No newline at end of file
diff --git a/java/stonks.tsv b/java/stonks.tsv
new file mode 100644
index 0000000..16256ca
--- /dev/null
+++ b/java/stonks.tsv
@@ -0,0 +1,7 @@
+5
+Symbol Price P/E Ratio Dividend Yield
+AAPL 150.2 30.0 1.5%
+COST 673.58 45.8 1.5%
+GOOGL 2750.50 35.0 0.8%
+MSFT 310.75 28.5 1.2%
+AMZN 3400.25 40.0 0.5%
diff --git a/java/variables.java b/java/variables.java
new file mode 100644
index 0000000..5c1803c
--- /dev/null
+++ b/java/variables.java
@@ -0,0 +1,10 @@
+public class variables {
+ public static void main(String[] args) {
+ int x = 8;
+ int xy = 16;
+ int y = 32;
+ System.out.println((x+3)*200);
+ System.out.println(xy);
+ System.out.println(y);
+ }
+} \ No newline at end of file