OOP Part 2, Exercises

A

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

B

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.

C

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.

D

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.

E

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

E1

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
Perf.measure();
// more complex code
Perf.measure();
Perf.showResult() // prints for example "Some code:" 100ms, "Code 2: " 102 ms
Perf.clean();

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

E2

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.

E3

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.

E4

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.