• 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; } }
• 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; } }
• Oct 15, 2022 •CodeCatch
public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head = new ListNode(0); ListNode handler = head; while(l1 != null && l2 != null) { if (l1.val <= l2.val) { handler.next = l1; l1 = l1.next; } else { handler.next = l2; l2 = l2.next; } handler = handler.next; } if (l1 != null) { handler.next = l1; } else if (l2 != null) { handler.next = l2; } return head.next; } }
//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"); } } }
• Feb 6, 2021 •Daedalus
import java.util.Scanner; /* In Heaven, the seed is tall and the oak small / It's input vital * "The al-Madinatian," verse 236-37 * tinyurl.com/17snqkfv * email answer to [email protected] **/ public class Main { public static void main(String args[]) { int x = 0; int a, c, m = 10000; Scanner in = new Scanner(System.in); System.out.println("Please enter a seed, maximum 3000000:"); x = in.nextInt(); System.out.println("Please input a number, maximum 29:"); a = in.nextInt(); System.out.println("Please input a number, maximum 999999:"); c = in.nextInt(); char menu = 'y'; while(menu != 'n') { x = (a*x + c) % m; System.out.println("The next number is " + x + "\nWould you like another? (y/n)"); menu = in.next().charAt(0); } } } //██╗ ░█████╗░███╗░░░███╗ ██████╗░░█████╗░███████╗██████╗░░█████╗░██╗░░░░░██╗░░░██╗░██████╗ //██║ ██╔══██╗████╗░████║ ██╔══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗██║░░░░░██║░░░██║██╔════╝ //██║ ███████║██╔████╔██║ ██║░░██║███████║█████╗░░██║░░██║███████║██║░░░░░██║░░░██║╚█████╗░ //██║ ██╔══██║██║╚██╔╝██║ ██║░░██║██╔══██║██╔══╝░░██║░░██║██╔══██║██║░░░░░██║░░░██║░╚═══██╗ //██║ ██║░░██║██║░╚═╝░██║ ██████╔╝██║░░██║███████╗██████╔╝██║░░██║███████╗╚██████╔╝██████╔╝ //╚═╝ ╚═╝░░╚═╝╚═╝░░░░░╚═╝ ╚═════╝░╚═╝░░╚═╝╚══════╝╚═════╝░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═════╝░
• May 21, 2025 •AustinLeath