Easter.java (4312B)
1 public class Easter 2 { 3 private static String USER1 = "Michael"; 4 private static String USER2 = "Stephen"; 5 private static String CONSTANT1 = "DFCHQ"; 6 private static String CONSTANT2 ="MAINCOREOFWEAS"; 7 /** 8 * Main entry point for Easter class. 9 * 10 * @param parameters is an array of string parameters of any size 11 * 'void' means it doesn't return a result value, like a function would 12 * don't worry about the 'static' word - it's mandatory for standalone programs 13 */ 14 public static void main(String[] parameters) 15 { 16 int i = 0; 17 String valuea = ""; 18 String valueb = ""; 19 String text = "Go and look here: "; 20 boolean variable1 = true, variable2 = false, variable3 = true; 21 if (parameters.length < 1) 22 { 23 System.out.println("Please put your name as the parameter to this program and try running it again"); 24 } 25 else if (USER1.equalsIgnoreCase(parameters[0]) || USER2.equalsIgnoreCase(parameters[0])) // || means 'OR' 26 { 27 System.out.println("Welcome, " + parameters[0]); // print out to the screen element 0 (the first element) in the array 28 if (USER1.equalsIgnoreCase(parameters[0])) // case-independent comparison, i.e. 'M' matches 'm' 29 { 30 valuea = CONSTANT1; 31 if (variable1 || variable2) 32 { 33 valuea = valuea + 'E'; 34 } 35 if (variable2 && variable3) // && means 'AND' 36 { 37 valuea = valuea + 'E'; 38 } 39 valueb = doSomething(valuea); 40 i = 0; 41 while (i < valueb.length()) 42 { 43 // add 1 to character at position i 44 char c = (char) (valueb.charAt(i) + 1); // in order to add 1 to a character it has to be of type 'char' rather than String - don't worry about it! 45 text = text + c; // charAt(i) gives you the character at position(i). NB first character is at position 0 46 i = i + 1; 47 } 48 } 49 else if (USER2.equalsIgnoreCase(parameters[0])) 50 { 51 valueb = CONSTANT2; 52 // charAt(i) gives you the character at position(i). NB first character is at position 0 53 // && means 'AND'; 0x in front of a number means that number is hexadecimal 54 if (0x11 > 15 && USER1.charAt(0) > valueb.charAt(0)) 55 { 56 valueb = valueb + "AV"; 57 } 58 else 59 { 60 valueb = valueb + "VA"; 61 } 62 if (Math.sqrt(121) > 8) // sqrt means 'square root' 63 { 64 valueb = valueb + 'E'; 65 } 66 else 67 { 68 valueb = valueb + 'A'; 69 } 70 if (variable1 && (variable2 || variable3)) // && means 'AND'; || means 'OR' 71 { 72 valueb = valueb + 'R'; 73 } 74 else 75 { 76 valueb = valueb + 'S'; 77 } 78 i = 0; 79 while (i < valueb.length()) 80 { 81 if (i % 2 == 0) // % is the modulo or 'remainder' function (i.e. remainder from dividing) and != means not equal to 82 { 83 text = text + (valueb.charAt(i)); // charAt(i) gives you the character at position(i). NB first character is at position 0 84 } 85 i = i + 1; 86 } 87 } 88 System.out.println(text); 89 } 90 else 91 { 92 System.out.println("Sorry, " + parameters[0] + " gets nothing"); 93 } 94 95 } 96 97 private static String doSomething(String input) 98 { 99 String result = ""; 100 if (input.length() > 1) 101 { 102 result = doSomething(input.substring(1, input.length())); // do something with the substring minus the first character (substring from element 1 to the end) 103 } 104 result = result + input.charAt(0); // charAt(0) means the first character 105 return result; 106 } 107 108 }