• May 21, 2025 •AustinLeath
0 likes • 0 views
• Jan 31, 2023 •AustinLeath
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; } }
• Nov 19, 2022 •CodeCatch
import java.util.Arrays; public class QuickSortMain { private static int array[]; public static void sort(int[] arr) { if (arr == null || arr.length == 0) { return; } array = arr; quickSort(0, array.length-1); } private static void quickSort(int left, int right) { int i = left; int j = right; // find pivot number, we will take it as mid int pivot = array[left+(right-left)/2]; while (i <= j) { /** * In each iteration, we will increment left until we find element greater than pivot * We will decrement right until we find element less than pivot */ while (array[i] < pivot) { i++; } while (array[j] > pivot) { j--; } if (i <= j) { exchange(i, j); //move index to next position on both sides i++; j--; } } // call quickSort() method recursively if (left < j) quickSort(left, j); if (i < right) quickSort(i, right); } private static void exchange(int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } public static void main(String a[]){ int[] input = {33,21,45,64,55,34,11,8,3,5,1}; System.out.println("Before Sorting : "); System.out.println(Arrays.toString(input)); sort(input); System.out.println("=================="); System.out.println("After Sorting : "); System.out.println(Arrays.toString(array)); } }
• 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 } }
0 likes • 3 views
class main { public static boolean findDuplicates(int[] nums) { for(int i = 0; i < nums.length; i++) { for(int j = 0; j < i; j++) { if(nums[i] == nums[j]) { return true; } } } return false; } public static void main(String[] args) { int[] myArray = {1,3}; System.out.println(findDuplicates(myArray)); } }
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"); } } }