Created
February 3, 2015 22:36
-
-
Save fernandozamoraj/9dcb7cb1801c99a2220a to your computer and use it in GitHub Desktop.
smallest_universal_kata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class SnippyMain { | |
public static void main(String args[]){ | |
System.out.println("Hello World"); | |
SnippyMain snippy = new SnippyMain(); | |
String who = ""; | |
while(true) | |
{ | |
who = snippy.getName(); | |
//exercise 1 | |
if(who != null && who.equals("Mary")){ | |
System.out.println("It's none other than Mary"); | |
break; | |
} | |
else{ | |
System.out.println("It's " + who + "!"); | |
} | |
} | |
System.out.println("Should have found Mary"); | |
int a = 0; | |
int b; | |
a = 2; | |
b = 3; | |
//Exercise 2 | |
System.out.println("2 + 3 equals " + snippy.add(a,b).toString()); | |
System.out.println("Should be 5"); | |
//Exercise 3 | |
System.out.println("My value is at " + snippy.findIndex(new int[]{0,1,2,3,4,5}, 5, 3)); | |
System.out.println("Should be 3"); | |
//Exercise 4 | |
System.out.println("The largest value is" + snippy.getLargest(new int[]{4, 0, 20,2,3,4}, 5).toString()); | |
System.out.println("Should be 20"); | |
} | |
public String getName(){ | |
String[] names = {null, "Joe", "Mary", "John", "David", null}; | |
Random random = new Random(); | |
return names[ Math.abs(random.nextInt()) % 4 ]; | |
} | |
public Integer add(int a, int b){ | |
return a + b; | |
} | |
public int findIndex(int[] elements, int maxIndex, int target){ | |
int i = 0; | |
while(elements[i] != target && i < maxIndex ){ | |
i++; | |
} | |
return i; | |
} | |
public Integer getLargest(int[] elements, int size){ | |
int largest = -1; | |
for(int i = 0; i < size; i++){ | |
if(largest < elements[i]){ | |
largest = elements[i]; | |
} | |
} | |
return largest; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment