import java.util.Scanner; public class InterpretingAliens { private static Scanner in; private static int next; private static void getNext() { next = in.nextInt(); } public static void main(String[] args) { in = new Scanner(System.in); String assignment = in.nextLine(); if (assignment.trim().equals("Part 1")) { part1(); } else if (assignment.trim().equals("Part 2")) { part2(); } else if (assignment.trim().equals("Part 3")) { part3(); } } /****************************************** * PART 1 ******************************************/ // We almost mechanically transform the language description into code. // Whenever we're done with a number, we call 'getNext();' to get the // next integer. We do this as early as possible, which works nicely. private static void part1() { getNext(); switch (next) { // One might argue that these are constants and should get their own // private static final variable, but as these constants are only used once, // this solution is just as maintainable and saves you some code. case 0: // Give (us resources) getNext(); give(); break; case 1: // Examine (some celestial object) getNext(); examine(); break; case 2: // Celebrate (a festival) getNext(); celebrate(); break; } System.out.println("."); } private static void give() { System.out.print("Give us "); quantity(); resource(); } private static void quantity() { if (next == 0) { System.out.print("all your "); } else { System.out.print(next); System.out.print(" "); } getNext(); } private static void resource() { switch (next) { case 0: System.out.print("watches"); break; case 1: System.out.print("breadsticks"); break; case 2: System.out.print("chewing gums"); break; case 3: System.out.print("stones"); break; case 4: System.out.print("sulfur"); break; } getNext(); } private static void examine() { System.out.print("Examine "); if (next == 0) { getNext(); quantity(); celestialObject(); } else { name(); } if (next != -1) { System.out.print(" and look for "); resource(); } } private static void name() { switch (next) { case 1: System.out.print("Mercurius"); break; case 2: System.out.print("Venus"); break; case 3: System.out.print("Mars"); break; case 4: System.out.print("Jupiter"); break; case 5: System.out.print("Saturnus"); break; case 6: System.out.print("Uranus"); break; case 7: System.out.print("Neptune"); break; } getNext(); } private static void celestialObject() { System.out.print(getCelestialObject(next)); getNext(); } // This function returns a String, for use in part 3. private static String getCelestialObject(int celestialObject) { switch (celestialObject) { case 0: return "comets"; case 1: return "asteroids"; case 2: return "moons"; case 3: return "rings"; default: return ""; } } private static void celebrate() { System.out.print("Celebrate "); festival(); if (next != -1) { System.out.print(" and offer "); quantity(); resource(); System.out.print(" to "); name(); } } private static void festival() { switch (next) { case 0: System.out.print("the festival of Aah"); break; case 1: System.out.print("Beeish Chistmas"); break; case 2: System.out.print("the Cee festival of the universe"); break; } getNext(); } /****************************************** * PART 2 ******************************************/ private static void part2() { getNext(); System.out.print("Build a "); container(); destination(); System.out.println("."); } private static void container() { type(); System.out.print(" "); shape(); if (next == 4) { getNext(); } else if (next == 5) { getNext(); System.out.print(" containing a "); container(); System.out.print(" and a "); container(); } else { System.out.print(" containing a "); container(); } } private static void type() { System.out.print(getType(next)); getNext(); } // This function returns a String, for use in part 3. private static String getType(int type) { switch (type) { case 0: return "cardboard"; case 1: return "steel"; case 2: return "plastic"; case 3: return "ceramic"; default: return ""; } } private static void shape() { switch (next) { case 0: System.out.print("box"); break; case 1: System.out.print("ball"); break; case 2: System.out.print("pyramid"); break; case 3: System.out.print("dodecahedron"); break; } getNext(); } private static void destination() { if (next != -1) { System.out.print(" and send one to "); name(); destination(); } } /****************************************** * PART 3 ******************************************/ // For an efficient solution, string builders should be used. private static void part3() { getNext(); System.out.print("Build a concrete base"); System.out.print(tower()); System.out.println("."); } private static String tower() { if (next == 4) { getNext(); return ""; } else { String typeString = getType(next); getNext(); String inner = tower(); if (next == -1) { return " on top of which is a 2x2x2 " + typeString + " cube" + inner; } else { String celestialObject = getCelestialObject(next); getNext(); return " on top of which is a 1x1x1 " + typeString + " cube" + inner + " with " + celestialObject + " painted on the sides"; } } } }