Vous êtes sur la page 1sur 2

4CCS1PRA

Programming Applications

Lab Solutions

Lab 1: OO and Programming Fundamentals


Exercise 1 Basic IDE usage
There is no sample solution to this task.

Exercise 2 Collections
The
solution
to
this
is
available
as
a
separate
zip
file
called
uk.ac.kcl.inf.4ccs1pra.lab1.basic.zip. It should be noted that this is one possible solution, but by no means the only one possible. So, as long as your solution does what is required, it is
valid! Notice that the zip file already contains the solution to Exercise 4 as well, so ignore class Main
for the moment.
A note on the ZIP files: These have been created by exporting the corresponding Eclipse projects.
From Eclipse, you should be able to view and test the sources by right-clicking in the package explorer and selecting 'Import'. In the dialogue window that opens, choose "General/Existing Project"
and click "Next". Select the "Archive File" radio button and use the browse button to find the ZIP file.
You should then be able to click on "Finish", which will create a new Java Project with the sources
from the ZIP file.
For other IDEs or for testing the code from the command line, please first unzip the ZIP file. You are
only interested in the contents of the src/ folder. If you use NetBeans or another IDE, copy the entire
contents of the src/ folder into the source folder of a new project. When running from the command
line, make sure to run your java compiler and virtual machine from the src/ folder (or, alternatively, to
include the src/ folder into your classpathfor example, by using the cp option).

Exercise 3 Debugging
There is no sample solution to this task.

Exercise 4 A simple user interface


The
solution
to
this
is
available
as
a
separate
zip
file
called
uk.ac.kcl.inf.4ccs1pra.lab1.basic.zip. It should be noted that this is one possible solution, but by no means the only one possible. So, as long as your solution does what is required, it is
valid!
Notice, in particular, how the main method creates an instance of class Main and invokes a new
method named run() on this new object. An alternate solution is in file
uk.ac.kcl.inf.4ccs1pra.lab1.basic.alternate.zip. Here, the main loop of the application has been written directly in the main() method rather than in the run() method defined in the
first solution. Notice how this has forced us to declare everything else in class Main as static, too!
While this solution works, it is slightly less flexible and reusable: It has become harder to reuse Main
independently of this specific application. In particular we cannot use two or more instances in the
same programme. While this doesn't, perhaps, matter so much for this exercise, it is an important
form of flexibility for more realistic applications. In general, you should avoid declaring methods or
attributes static as much as you can.

Exercise 5 Documenting your code


All sample code provided is fully documented.

Last changed: 07 January 2014

Exercise 6 Searching for records (advanced)


The
solution
to
this
is
available
as
a
separate
zip
file
called
uk.ac.kcl.inf.4ccs1pra.lab1.withMap.zip. Note that the List in TelephoneDirectory
has been replaced by a Map. Maps are data structures that work similar to arrays. However, instead
of only integer indexes, they can use any object as their index. In our case, we use a map that maps
Strings (the names of our contacts) to TelephoneRecords.
I hope you find the lecture material, handouts, exercises, SGTs, and labs useful. However, if you have
any comments (good or bad), please do not hesitate to contact me at steffen.zschaler@kcl.ac.uk or
come see me during my office hours.

Last changed: 07 January 2014

Vous aimerez peut-être aussi