Lecture 12 Slides

Download Report

Transcript Lecture 12 Slides

Programming for Beginners
Lecture 12: Programming Project
Martin Nelson
Elizabeth FitzGerald
Revision of session 11

Java has in-built techniques for handling errors:

Place ‘dangerous’ code inside a try statement.

If this code generates an error, it throws an exception.


Immediately after the try statement, we have a catch statement,
which contains instructions for how to handle various types of
exception.
File I/O can be done using the class RandomAccessFile

Mode can be “r” (read-only) or “rw” (read-write).

All file I/O must be error-trapped, i.e. inside a try statement.

Useful method for reading data: readLine

Useful method for writing data: writeBytes
Session 12 – aims and objectives

Programming project!

Using all the concepts which we’ve covered in the course
so far:

Software design – pen & paper!!

Pseudocode.

Coding, bit by bit.

Software testing.

Successive refinement.
Programming Project

Task: Create an address book program, capable of
storing the contact details of a number of your friends.

Make a class called “Person”, with variables to store
name, address, phone number and email address.

Your program should store a number of friends in an
array.

Ask the user to choose between adding new friends,
updating old friends’ information, or search for a friend by
name.

Store the contact info in a text file.
Programming Project

This task will take some time – there is a lot to do!

Break the task down into smaller chunks...

Create the Person class.

Store some people in an array manually.

Add User input.

Add methods to manipulate data.

Add File I/O.

Remember: Pen & paper first!

Pseudocode!

Test the code after every step!
How do I exit?

You may like to loop your code, repeatedly asking the
user to add friends, update contacts, search for people
etc. until they get bored...

Can use a perpetual loop to do this...
while(true){ do_stuff };

If you do this, make sure you give the user the choice to
quit the program.

To end the program neatly:
System.exit(0);

Don’t worry about this until everything else in your code
works properly!
The File Class – 1

You may find that a convenient way to update your
contacts file is to delete the old version, and re-populate
the list from scratch.

The File class contains a number of methods for
manipulating files. For example, to delete the file
contacts.txt:
File myFile = new File(“contacts.txt”);
myFile.delete();

The File class also contains methods for testing read/write
access to a given file/directory, and much more. See the
documentation for more details.
The File Class – 2

Do not confuse File with RandomAccessFile!

File class:


manipulating files and directories

testing for existence

testing read/write access

creating/deleting files/directories

extracting file properties.
RandomAccessFile class:

reading data from files

writing data to files.
Coming up in Sessions 13-15


Some new languages, other than Java!

Session 13: A brief introduction to C++

Session 14: A brief introduction to scripting languages.
Session 15: Overview of some more-advanced concepts

Some ideas that we should be aware of for efficient coding.

Some more interesting java tasks, e.g. graphics!

Where to go next? Future C++ and Java programming courses.