Loading...
More Java Posts
import static java.lang.System.*;import java.util.Arrays;public class MergeSort{private static int passCount;public static void mergeSort(int[] list){passCount=0;mergeSort(list, 0, list.length);}private static void mergeSort(int[] list, int front, int back) //O( Log N ){int mid = (front+back)/2;if(mid==front) return;mergeSort(list, front, mid);mergeSort(list, mid, back);merge(list, front, back);}private static void merge(int[] list, int front, int back) //O(N){int dif = back-front;int[] temp = new int[dif];int beg = front, mid = (front+back)/2;int saveMid = mid;int spot = 0;while(beg < saveMid && mid < back) {if(list[beg] < list[mid]) {temp[spot++] = list[beg++];} else {temp[spot++] = list[mid++];}}while(beg < saveMid)temp[spot++] = list[beg++];while(mid < back)temp[spot++] = list[mid++];for(int i = 0; i < back-front; i++) {list[front+i] = temp[i];}System.out.println("pass " + passCount++ + " " + Arrays.toString(list) + "\n");}public static void main(String args[]){mergeSort(new Comparable[]{ 9, 5, 3, 2 });System.out.println("\n");mergeSort(new Comparable[]{ 19, 52, 3, 2, 7, 21 });System.out.println("\n");mergeSort(new Comparable[]{ 68, 66, 11, 2, 42, 31});System.out.println("\n");}}
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 hereSystem.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 userif (id < 0) {throw new Exception("Invalid user id");}// Return the user objectreturn 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 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 binaryint 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;}
//sample code to write 100 random ints to a file, 1 per lineimport 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");}}}
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;}}
import java.io.File;import java.io.IOException;import java.util.Map;import java.util.Scanner;import java.util.TreeMap;public class SimpleWordCounter {public static void main(String[] args) {try {File f = new File("ciaFactBook2008.txt");Scanner sc;sc = new Scanner(f);// sc.useDelimiter("[^a-zA-Z']+");Map<String, Integer> wordCount = new TreeMap<String, Integer>();while(sc.hasNext()) {String word = sc.next();if(!wordCount.containsKey(word))wordCount.put(word, 1);elsewordCount.put(word, wordCount.get(word) + 1);}// show resultsfor(String word : wordCount.keySet())System.out.println(word + " " + wordCount.get(word));System.out.println(wordCount.size());}catch(IOException e) {System.out.println("Unable to read from file.");}}}