Java practice: predict, run, explain
Prerequisites: Java basics, control flow and classes. Time: 45–60 minutes. These exercises adapt the sequence in the supplied 1257 Parallel Universe Programming 2026–2027, pages 6–7 and 11; their code and answers below were written for this course.
- Define an integer player level and a boolean
hasKey. Print"open"only when the level is at least 3 and the player has the key. Test(3, true),(2, true),(3, false): expected open, closed, closed. - Store three names in
String[] names = {"Ari", "Bo", "Cy"};and print each using an enhanced for loop. Expected: each name once, in order. Then useArrayList<Integer>for a changing list of levels. Java generic type arguments are reference types: useInteger, notint. - Define an abstract
Mechanismwith abstractString describe(), then subclassesIntakeandArmreturning distinct descriptions. Put both in aMechanism[], iterate and calldescribe(). Expected: the implementation chosen depends on the actual object. This practices polymorphism without implying a physical robot implementation.
Compile each complete program with javac FileName.java, run with java FileName, and compare output. If compilation fails, check the public class and filename match, punctuation, and generic types. Ask a mentor to review code and results; the supplied textbook's examples are reference material and should be checked against Java documentation rather than copied uncritically. Oracle classes.