Prototype / Research / Study Projects

The following sections contain a list of “coding exercises” or “prototyping projects.” We recommend you use them selectively. If you know you’re weak in certain topics, feel free to jump to that section of the list first. If you want to do an overall assessment of your readiness, read through the list and try to honestly evaluate how difficult (or easy) it would be for you to complete the various projects. As you work through your coding projects, go out of your way to prove to yourself that you’re really getting the results you think you’re getting. If you can’t verify that your code is doing what you think it’s doing, then (with a few exceptions like garbage collection) your task isn’t done.

How about reference materials? As we mentioned earlier, don’t be too Draconian with yourself. Try to do the exercises without using reference materials. When you DO use reference material, make a note of it. Did you just need to check on a method signature, or did you completely forget a class’s capabilities? Adjust your study plans accordingly.

Finally, the following list probably doesn’t completely cover every topic you might find on the real exam. Use this list as a template to create your own coding projects that focus on areas you want to explore more thoroughly.

Section 1: Declarations, Initialization, Scoping

Image 1.a Create an interface that has methods and a constant. Create an abstract class with an abstract method, a non-abstract method, and an instance variable. Create a concrete class that uses both of the above.

Image 1.b Create a directory structure, and a couple of classes, in different packages, to demonstrate the differences between default and protected access rules.

Image 1.c Create a class, a subclass, and a sub-subclass. Create at least two constructors in each class and use super() and this() to access (one way or another) all of the constructors, based on invoking new() on the grandchild class.

Image 1.d Create a class that uses static initialization blocks, instance initialization blocks and a constructor, and prove the sequence in which they get called when the class is invoked and a new object is created.

Image 1.e Create a 2D array and a 3D array. Copy the 2D array into a part of the 3D array.

Image 1.f Create an enum that has a variable, a constructor, a method, and a constant class body. Create a class that exercises each of the enum’s members.

Image 1.g Create a class, a subclass, and a sub-subclass:

Image Make overridden versions of methods in those classes.

Image Make an array of instances of the three and loop through them polymorphically.

Image Do the same with a collection instead of an array.

Image 1.h Create a class that has a static inner class and a method local inner class. Make instances of each inner class. Create code that accesses the inner classes’ members from the outer class. Create code that accesses the outer class’s members from within the inner classes.

Image 1.i Write a program that has variables of all four scopes. Try to access them out of their scope.

Section 2: Flow Control

Image 2.a Write a method that includes an if-else that’s inside a while loop that’s inside a for loop that’s inside a do loop. Then add a labeled break and continue to the for loop.

Image 2.b Make a switch using an enum that contains a default case. Test the switch statement by iterating through the enum.

Image 2.c Write code that deliberately throws the following exceptions, without using the "throw" keyword:

Image ClassCastException

Image NullPointerException

Image ArrayIndexOutOfBoundsException

Image StackOverflowError

Image AssertionError

Image NumberFormatException

Image 2.d Catch all of the preceding exceptions and print some subsequent output outside of any error-handling code.

Image 2.e Write a try-catch-finally and demonstrate it doing a try-catch-finally and then a try-finally.

Image 2.f Write code that demonstrates how handle or declare rules are different for runtime exceptions, compile time exceptions, and errors.

Image 2.g Write code that demonstrates handling some exceptions, and declaring some others.

Image 2.h Create an assertion that passes or fails based on a command-line argument.

Image 2.i Attempt to compile working assertion code with old versions of Java.

Section 3: APIs

Image 3.a Create code that uses an xxxValue() method, a parseXxx() method, and a valueOf() method.

Image 3.b Create code that invokes String.concat() and “loses” the result, and then show an invocation of concat() that saves the result.

Image 3.c Create code that instantiates instances of two different numeric wrapper classes, adds the values of the two instances together, and then creates a third numeric wrapper instance whose value is the sum.

Image 3.d Create code that makes a new directory on your hard drive, adds a text file to that directory, writes several lines of text data to the file, closes the file, reopens the file, reads data from the file and prints out the data.

Image 3.e Create a program that instantiates a Date object whose date represents the two billionth second after the Java epoch (1/1/1970). Determine, based on your JVM’s locale, what day of the week (first, second…) the two billionth second occurs. Next, add 15 months to that date and print the new date.

Image 3.f Write a program that instantiates a NumberFormat object. Using methods available to the NumberFormat object, and given the String “345.67”, create two Number objects; one with the value 345.67 and one with the value 345.

Image 3.g Using classes from java.util.regex, and given the String a4 0x12 5b _x_ 056 092 0x5g, write a program that does the following: finds all occurrences of digits in the String, and all occurrences of word characters in the String. Next, write code that finds all occurrences of hexadecimal numbers in the String. Finally, write code that finds all occurrences of octal numbers in the String.

Image 3.h Use String.split() to tokenize the following String: 3.14, 0x17-5b, cat.dog.

Your program must tokenize using three different delimiters—the dot(.), the comma, and a space character—all of which must be handed to the program via a command-line argument.

Image 3.i Using classes from java.util.regex, write a program that returns whether a given String contains either all alphabetic characters, or all numeric characters, or whether the String is mixed.

Section 4: Concurrency

Image 4.a Create a class that starts one thread from an initialization block, that starts another thread from an inner class, and that starts a third thread from a constructor.

Image 4.b Create code that starts threads using java.lang.Thread and java.lang.Runnable.

Image 4.c Create code that demonstrates how sleep(), join(), setPriority(), and interrupt() can affect how threads interact with each other.

Image 4.d Create code that has a method named atomic(). Demonstrate in code how two threads can, sometimes, invoke atomic() concurrently. Next, make a version of the code in which the two threads CANNOT invoke atomic() concurrently.

Image 4.e Create code in which two threads sometimes deadlock.

Image 4.f Create code that demonstrates that when two threads invoke the same synchronized method—but against different objects—the threads runs concurrently.

Section 5: OO Concepts

Image 5.a Write code in which overloaded methods try to “box then widen.” Then try to “widen then box.”

Image 5.b Create a class and a subclass and perform legal reference variable upcasting and downcasting. Next, write code that won’t compile due to casting errors. Finally, write code that will throw an exception when attempting a cast.

Image 5.c Create legal overriding methods that change the following aspects of the method they override: the return type, the access modifier, exceptions thrown.

Image 5.d Write code that accesses constructors with all four access modifiers.

Image 5.e Write a singleton class.

Image 5.f Write a superclass that requires any of its subclasses to create a no-arg constructor. Write a subclass for this superclass.

Image 5.g Write two classes that interact with each other’s “state” and maintain loose coupling.

Image 5.h Write several classes such that there is an “is-a” relationship and a separate “has-a” relationship.

Section 6: Collections/Generics

Image 6.a Create an "Items" class that has a String name, and an int value. Note that you will use this class in several of this section’s exercises, and you may need to enhance this class as you go along. For all of 6.a, use classes in java.util whenever possible:

Image Write a class that makes a collection of Items that guarantees no duplicates.

Image Write a class that makes a collection of Items that can be accessed in the order in which they were added to the collection.

Image Write a class that creates a collection in which a given instance of Items can be retrieved using a different associated String called "buyer".

Image Write a program that uses some combination of java.util.Comparator and java.lang.Comparable to sort a collection of Items either by name or by value.

Image 6.b Create an array of Strings and use method(s) in the Arrays class to search the array for the location of specific Strings. Demonstrate an unsuccessful search. Next, sort the array in reverse order and perform another search.

Image 6.c Create a collection of Strings and use method(s) in the Collections class to search the collection. Demonstrate successful and unsuccessful searches.

Image 6.d Create a List of Longs. Create an array that “backs” the List. Change a value in the List and display the contents of the array. Change a value in the array and display the contents of the List. Attempt to add a new entry to the List.

Image 6.e Create code that uses an Iterator to print the contents of a collection.

Image 6.f Create a Set such that only Items can be added. Prove it.

Image 6.g Create a Map such that only Strings can be keys and only Items can be values. Prove it.

Image 6.h Create a non-generic collection and retrieve elements from it.

Image 6.i Make a subclass of Items. Create a method that takes only Items or the Items subclass. Prove it.

Image 6.j Write code such that the compiler issues a warning when you mix generic and non-generic collections.

Image 6.k Write a method that takes an instance of any type, makes an ArrayList of that type, and then adds the original instance to the ArrayList.

Section 7: Fundamentals

Image 7.a Create a compiler error concerning a final primitive. Create an error concerning a final reference variable. Attempt to change the state of an instance whose reference variable is final.

Image 7.b Write code that proves a short-circuit operator is short-circuiting, and that a non-short-circuit operator isn’t short-circuiting.

Image 7.c Write code that determines whether the integer value of args[0] is evenly divisible by the integer value of args[1].

Image 7.d Write code that uses a ternary operator to populate a String with either “success” or “failure” based on whether args[0] can be parsed to an int.

Image 7.e Write code that demonstrates how postfix operators affect loop operations differently than prefix operators.

Image 7.f Write a program that adds two name-value pairs to java.util.Properties: one pair via a command-line option and one via a method in Properties. Next, print all the system’s properties to verify that the two new properties were added.

Image 7.g Although difficult to prove, write code that creates instances of a class that are referred to by other instances, and yet are still eligible for garbage collection.

Image 7.h Create a class that has a method such that the first time the garbage collector attempts to collect a given instance, this method will keep the garbage collector from collecting that instance at that point.

Image 7.i Create code that passes primitives and reference variables into methods, demonstrating how “pass by value” works for primitives and reference variables.

Image 7.j Although not strictly on the exam, create a class and put it in a JAR file. Create a second class that uses the class in the JAR file.

Image 7.k Write a class that uses a static import.

Image 7.l Write a class that lives in a package. Write another class in another package that successfully uses methods from the first class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset