From b7f857e0ec93a226cc5714b9ca882e5ef073a281 Mon Sep 17 00:00:00 2001 From: eduardo2salas Date: Tue, 28 Nov 2017 17:37:10 +0000 Subject: [PATCH] Interview Submission Submitting implementation for the coding portion of the interview. --- java-engineer-1/src/CountingThreading.java | 42 +++++++++++++++++++- java-engineer-1/src/FizzBuzz.java | 22 +++++++++-- java-engineer-1/src/Recursion.java | 27 ++++++++++++- java-engineer-1/src/ReverseWords.java | 45 +++++++++++++++++++++- 4 files changed, 128 insertions(+), 8 deletions(-) diff --git a/java-engineer-1/src/CountingThreading.java b/java-engineer-1/src/CountingThreading.java index 066036d..e30c527 100644 --- a/java-engineer-1/src/CountingThreading.java +++ b/java-engineer-1/src/CountingThreading.java @@ -1,13 +1,53 @@ /** * Write a java code that takes input of a number N, and outputs counting from 1 to N in "N" seconds. Use Threads. * The user should be able to pass the number "N" to the application as a parameter. + * + * Author: Eduardo Salas + * Last Edited: 11/28/2017 */ public class CountingThreading { public static void main(String[] args) throws Exception{ - System.out.println("Hello from CountingThreading.java"); + //check for n if passed in, pass in through commandline argument + int n = 10; + try{ + n = Integer.parseInt(args[0]); + } catch(ArrayIndexOutOfBoundsException exception) {} + + //display num to count to + System.out.println("Number to count to: " + n); + + //initialize thread with n + Thread thread = new Thread(new CountingThreading().new Counter(n)); + + //start thread + thread.start(); } + class Counter extends Thread { + + private int num = 0; + + public Counter(int n) { + this.num = n; + } + + @Override + public void run() { + //go from 1 to n + for(int i=1; i <= num; i++) { + //sleep for 1 second + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + break; + } + + //print current number + System.out.println(i); + } + } + } } diff --git a/java-engineer-1/src/FizzBuzz.java b/java-engineer-1/src/FizzBuzz.java index 80bff61..8dae7e4 100644 --- a/java-engineer-1/src/FizzBuzz.java +++ b/java-engineer-1/src/FizzBuzz.java @@ -2,13 +2,29 @@ * Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" * instead of the number and for the multiples of five print "Buzz". * For numbers which are multiples of both three and five print "FizzBuzz". + * + * Author: Eduardo Salas + * Last Edited: 11/28/2017 */ public class FizzBuzz { + private static int n = 100; + public static void main(String[] args) throws Exception{ - - System.out.println("Hello from FizzBuzz.java"); + System.out.println("Hello from FizzBuzz.java"); - } + for(int i = 1; i <= n; i++){ + //add current number to result + String testResult = i + " "; + + //add Fizz if divisible by 3 + testResult += (i % 3 == 0) ? "Fizz" : ""; + //add Buzz if divisible by 5 + testResult += (i % 5 == 0) ? "Buzz" : ""; + + //show current number in loop with result + System.out.print(testResult + "\n"); + } + } } diff --git a/java-engineer-1/src/Recursion.java b/java-engineer-1/src/Recursion.java index 20e8bed..13a4c3b 100644 --- a/java-engineer-1/src/Recursion.java +++ b/java-engineer-1/src/Recursion.java @@ -2,13 +2,36 @@ * Write a recursive function that takes a string as input and returns the reversed string. * The user should be able to pass the input string as a paramter to the application. * + * Author: Eduardo Salas + * Last Edited: 11/28/2017 */ public class Recursion { public static void main(String[] args) throws Exception{ - - System.out.println("Hello from Recursion.java"); + System.out.println("Hello from Recursion.java"); + //check for n if passed in, pass in through commandline argument + String strToReverse = "dnuoF retemaraP oN"; + try{ + strToReverse = args[0]; + } catch(ArrayIndexOutOfBoundsException exception) {} + + //show original string + System.out.println("Normal String: "); + System.out.println(strToReverse + "\n"); + + //show result of reversing + System.out.println("Reversed String: "); + System.out.println(reverseString(strToReverse)); } + private static String reverseString(String str){ + //check for null, empty, or single character string; return itself + if ((str.length() <= 1) || (null == str)) { + return str; + } + + //recursively sent in the string without its first character, add the character to the end of the string and then return + return reverseString(str.substring(1)) + str.charAt(0); + } } diff --git a/java-engineer-1/src/ReverseWords.java b/java-engineer-1/src/ReverseWords.java index 0f90169..6089c32 100644 --- a/java-engineer-1/src/ReverseWords.java +++ b/java-engineer-1/src/ReverseWords.java @@ -1,13 +1,54 @@ /** * Given an input string, reverse the string word by word. * The user should be able to pass the input string as a paramter to the application. + * + * Author: Eduardo Salas + * Last Edited: 11/28/2017 */ public class ReverseWords { public static void main(String[] args) throws Exception{ - - System.out.println("Hello from ReverseWords.java"); + System.out.println("Hello from ReverseWords.java"); + //check for n if passed in, pass in through commandline argument + String strToReverse = "Found Parameter No"; + try{ + strToReverse = args[0]; + } catch(ArrayIndexOutOfBoundsException exception) {} + + //show normal string + System.out.println("Normal String: "); + System.out.println(strToReverse + "\n"); + + //show the result of reverseWords + System.out.println("Reversed Words: "); + System.out.println(reverseWords(strToReverse)); } + private static String reverseWords(String str) { + //check for null, empty, or single character string; return itself + if ((null == str) || (str.length() <= 1)) { + return str; + } + + //split string into an array of words + String[] word = str.split(" "); + + //construct string from going backwards in the array + StringBuilder builder = new StringBuilder(); + for (int i = word.length - 1; i >= 0; i--) { + if (!word[i].equals("")) { + //add word to string builder with an additional space + builder.append(word[i]).append(" "); + } + } + + //get rid of the extra space at the end + if(builder.length() > 0) { + builder.deleteCharAt(builder.length() - 1); + } + + //return the resulting string + return builder.toString(); + } }