Skip to content
This repository was archived by the owner on Apr 18, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion java-engineer-1/src/CountingThreading.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
22 changes: 19 additions & 3 deletions java-engineer-1/src/FizzBuzz.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
27 changes: 25 additions & 2 deletions java-engineer-1/src/Recursion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
45 changes: 43 additions & 2 deletions java-engineer-1/src/ReverseWords.java
Original file line number Diff line number Diff line change
@@ -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();
}
}