Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class SnippyMain {
  4.  
  5. public static void main(String args[]){
  6.  
  7. System.out.println("Hello World");
  8. SnippyMain snippy = new SnippyMain();
  9.  
  10. String who = "";
  11.  
  12. while(true)
  13. {
  14. who = snippy.getName();
  15.  
  16. //exercise 1
  17. if(who != null && who.equals("Mary")){
  18. System.out.println("It's none other than Mary");
  19. break;
  20. }
  21. else{
  22. System.out.println("It's " + who + "!");
  23. }
  24. }
  25.  
  26. System.out.println("Should have found Mary");
  27.  
  28. int a = 0;
  29. int b;
  30.  
  31. a = 2;
  32. b = 3;
  33. //Exercise 2
  34. System.out.println("2 + 3 equals " + snippy.add(a,b).toString());
  35. System.out.println("Should be 5");
  36.  
  37. //Exercise 3
  38. System.out.println("My value is at " + snippy.findIndex(new int[]{0,1,2,3,4,5}, 5, 3));
  39. System.out.println("Should be 3");
  40.  
  41. //Exercise 4
  42. System.out.println("The largest value is" + snippy.getLargest(new int[]{4, 0, 20,2,3,4}, 5).toString());
  43. System.out.println("Should be 20");
  44. }
  45.  
  46. public String getName(){
  47.  
  48. String[] names = {null, "Joe", "Mary", "John", "David", null};
  49.  
  50. Random random = new Random();
  51.  
  52. return names[ Math.abs(random.nextInt()) % 4 ];
  53.  
  54. }
  55.  
  56. public Integer add(int a, int b){
  57. return a + b;
  58. }
  59.  
  60.  
  61. public int findIndex(int[] elements, int maxIndex, int target){
  62.  
  63. int i = 0;
  64.  
  65. while(elements[i] != target && i < maxIndex ){
  66. i++;
  67. }
  68.  
  69. return i;
  70. }
  71.  
  72. public Integer getLargest(int[] elements, int size){
  73.  
  74. int largest = -1;
  75.  
  76. for(int i = 0; i < size; i++){
  77. if(largest < elements[i]){
  78. largest = elements[i];
  79. }
  80. }
  81.  
  82. return largest;
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement