• 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; } }
• Apr 15, 2025 •shivainsharma2124-fbae
0 likes • 2 views
public class Main { public static void main(String[] args) { System.out.println("Hello World"); } }
• Nov 19, 2022 •CodeCatch
0 likes • 1 view
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; } }
• May 15, 2025 •AustinLeath
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); } }
• May 21, 2025 •AustinLeath
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)); } }
• Feb 6, 2021 •Daedalus
public class Daedalus extends Athenian { private Story story; public ArrayList<Skill> skills = new ArrayList<Skill>(); public ArrayList<Athenian> children = new ArrayList<Athenian>(); public Daedalus(Story story, ArrayList<Skill> inherentSkills) { if(story != null) System.out.println("ERROR No one is created with a story."); skills.add(inherentSkills); } public Momento advanceStory(Scene s) { System.err.println("ERROR Don't know how to proceed..."); } } //██╗ ░█████╗░███╗░░░███╗ ██████╗░░█████╗░███████╗██████╗░░█████╗░██╗░░░░░██╗░░░██╗░██████╗ //██║ ██╔══██╗████╗░████║ ██╔══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗██║░░░░░██║░░░██║██╔════╝ //██║ ███████║██╔████╔██║ ██║░░██║███████║█████╗░░██║░░██║███████║██║░░░░░██║░░░██║╚█████╗░ //██║ ██╔══██║██║╚██╔╝██║ ██║░░██║██╔══██║██╔══╝░░██║░░██║██╔══██║██║░░░░░██║░░░██║░╚═══██╗ //██║ ██║░░██║██║░╚═╝░██║ ██████╔╝██║░░██║███████╗██████╔╝██║░░██║███████╗╚██████╔╝██████╔╝ //╚═╝ ╚═╝░░╚═╝╚═╝░░░░░╚═╝ ╚═════╝░╚═╝░░╚═╝╚══════╝╚═════╝░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═════╝░