OOP Part 2, Exercises


Create a program that uses an ArrayList without importing java.util.*. The topic is the import statement.


Create a class with public, protected, private and package-access fields and methods. Create an object of the class and try to call all fields and methods. The topic is the access modifiers on classes, i.e., private, protected, and public.


Create a class with a protected field. Create another class in the same file that access the protected field directly. Explain what is happening. The topic is the access modifier protected and access via other classes in the same file.


Create a class P such that instead of writing System.out.println(<message>) you write P.writeln. Add overloaded method for all basic types and java.lang.Object. It is a good idea to added the P class you create to your project code, it is very handy and will save you a lot of typing in the remaining part of the project. Note that the class should be added in the relevant package for example the cs.auc.dk.grp411.util package.


Create a Interval class by using java.util.Date as the start date and stop date, respectively. The Interval class should contain at least the following methods.

Extra Assignments


Design a class that can be used to measure the time it executes a piece of code. As an example.

Perf.start("Some code");
// complex code
// more complex code
Perf.showResult() // prints for example "Some code:" 100ms, "Code 2: " 102 ms

What should the interface look like? Which data structures be used internally?


Use the class from Exercise E1 to measure the overhead in calling a method for getting and setting a private integer compared to accessing a public integer directly.


Put the code for your project into a globally unique package name. Build a jar file with all your code. Add the jar file to your CLASSPATH. Create a new class that uses the code in your newly created jar file.


Make your project jar file to an executable jar file (read the documentation on how this is done). Add a class to your project that can be called directly.