• Nov 19, 2022 •CodeCatch
0 likes • 1 view
//sample code to write 100 random ints to a file, 1 per line import java.io.PrintStream; import java.io.IOException; import java.io.File; import java.util.Random; public class WriteToFile { public static void main(String[] args) { try { PrintStream writer = new PrintStream(new File("randInts.txt")); Random r = new Random(); final int LIMIT = 100; for (int i = 0; i < LIMIT; i++) { writer.println(r.nextInt()); } writer.close(); } catch (IOException e) { System.out.println("An error occured while trying to write to the file"); } } }
• Oct 15, 2022 •CodeCatch
0 likes • 8 views
public static String addBinary(){ // The two input Strings, containing the binary representation of the two values: String input0 = "1010"; String input1 = "10"; // Use as radix 2 because it's binary int number0 = Integer.parseInt(input0, 2); int number1 = Integer.parseInt(input1, 2); int sum = number0 + number1; return Integer.toBinaryString(sum); //returns the answer as a binary value; }
• Oct 7, 2023 •AustinLeath
0 likes • 5 views
import java.util.Arrays; public class LongestIncreasingSubsequence { public int lengthOfLIS(int[] nums) { if (nums.length == 0) { return 0; } int[] dp = new int[nums.length]; Arrays.fill(dp, 1); // Initialize all elements in dp array to 1, as each element itself is a valid subsequence of length 1. int maxLength = 1; // Initialize the maximum length to 1, considering a single element as the sequence. for (int i = 1; i < nums.length; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); // If nums[i] is greater than nums[j], update the longest subsequence length ending at i. } } maxLength = Math.max(maxLength, dp[i]); // Update the maximum length if needed. } return maxLength; } public static void main(String[] args) { LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence(); int[] nums = {10, 9, 2, 5, 3, 7, 101, 18}; int result = lis.lengthOfLIS(nums); System.out.println("Length of the Longest Increasing Subsequence: " + result); // Output: 4 } }
• May 15, 2025 •AustinLeath
0 likes • 2 views
import java.util.Arrays; public class Main { public static int findMinNonCreatableSum(int[] coins) { // Sort the array Arrays.sort(coins); // Initialize max creatable sum int maxCreatable = 0; // Iterate through coins for (int coin : coins) { // If current coin is greater than maxCreatable + 1, // we found our answer if (coin > maxCreatable + 1) { return maxCreatable + 1; } // Add current coin to maxCreatable maxCreatable += coin; } // If we can create all sums up to maxCreatable, // the answer is maxCreatable + 1 return maxCreatable + 1; } public static void main(String[] args) { int[] coins = {2, 9, 1, 2, 7}; int result = findMinNonCreatableSum(coins); System.out.println("Smallest sum that cannot be created: " + result); } }
• Jan 31, 2023 •AustinLeath
0 likes • 0 views
public class UserService { private UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public User getUserById(long id) { try { return userRepository.getUserById(id); } catch (Exception e) { // Log the exception here System.out.println("An error occurred while fetching the user: " + e.getMessage()); return null; } } } public class UserRepository { public User getUserById(long id) throws Exception { // Database query to fetch the user if (id < 0) { throw new Exception("Invalid user id"); } // Return the user object return new User(id, "John Doe"); } } public class User { private long id; private String name; public User(long id, String name) { this.id = id; this.name = name; } public long getId() { return id; } public String getName() { return name; } }
public class Factorial { public static void main(String[] args) { final int NUM_FACTS = 100; for(int i = 0; i < NUM_FACTS; i++) System.out.println( i + "! is " + factorial(i)); } public static int factorial(int n) { int result = 1; for(int i = 2; i <= n; i++) result *= i; return result; } }