Вы находитесь на странице: 1из 5

CI5100

Software Development in Java Pair Programming Workshop Tutorial 1


Pair Programming Phase 1


Tutorial 2: Reading Data from a File

1. Introduction

Tutorial 1 covers creating a basic data class suitable for the core data for your
data viewer.

This tutorial describes a way of designing a data model class so that it can easily
create objects from a file of a known format and structure (in this case a csv file)
by using the constructors of the data model class.

The tutorial assumes that the starting point is a class with an implementation
along the lines of that shown in Figure W2.1.


Extended Data Class for the PetShop Application


Extended Data Class for the TalentAgency Application

CI5100 Software Development in Java Pair Programming Workshop Tutorial 1



Extended Data Class for the FootballTeam Application

Figure W2.1 Data Classes after Task W1.6 from Workshop 1

Depending upon your decisions as to what data types your application will use to
represent your attributes, your class design may look something like that in
Figure W2.1. part way through the implementation of User Story 1.

The ability to convert Strings to and from an Array is especially important if you
are using String[] in your attributes. However, knowing how to split a String up
into separate components at a delimiter is important for reading data from a
comma delimited file.

2. Learning Outcomes


The context of this tutorial is to present a strategy that for creating objects from
common delimited strings as present in your data files. The first step is to do
this in the main method of a test class using hard-coded data (String literal
values taken from the data file). The next step is to design and create a new class
to have the responsibility of reading in the data file and creating an Array of
objects from that file.

At the end of this Tutorial you should be able to:
Convert data to and from a delimited String and an array of Strings
Use a constructor to create an object from a line in your data file
Override the toString( ) method so that you can easily get a String
representation of your data objects
Create and manipulate an array of your data objects
Read data in from a file to create an array of your data objects

3. Tutorial 2 Core Tasks


All the core tasks that you need to do at this Stage are covered in the lectures and
in an associated Tutorial Video on StudySpace. The core tasks for this stage are
listed in TableW2.1 below.

CI5100 Software Development in Java Pair Programming Workshop Tutorial 1



Task
W2.1 Create a Parameterised
Constructor that will make a Data
Object out of a line from your data
file

Notes
[Basic Constructors are covered in
Supplementary Material: Writing
Classes..:CO1040 Lecture #7 part4]
The line to be read from your data file
should be a single String that is comma
delimited.
You can use the String Split method to split
the String up into an array of Strings.
Then you need to set the attributes of your
object from this array of Strings.
Test that this is working from your main
method
Important:
When you create a parameterised
constructor to avoid errors you should
also create an empty default constructor
for your class
E.g. For the Student class it would be:

public Student(){

}
W2.2 Override the toString( )
All Java Classes have a toString( ) method.
method of Java Object (see Java
This is invoked when you print out an
API docs) so that you can easily
object (see last week). The toString( )
print out a String representation
method should return a String. You should
of one of your data objects
format this to appear in a structured way

e.g. with tabs or spaces between attributes
[Note for flexibility with different or according to a fixed format.
types of formatted output you may Test that this is working in your main
want to design specific methods
method.
for this rather than use the
toString( ) method
W2.3 Create an array of your data [Arrays are covered in Supplementary
objects from the first few lines of
Material: Arrays..:CO1040 Lecture #10]
your data file.
In the test main method of your data class,

should create a small array of data objects
e.g. 3. For each of these create an object
from one of the lines in your file.
Afterwards print out each element of the
array to the output window.

Table W2.1 These are the core tasks you must complete for this Workshop
all are covered in the associated lecture and the tutorial videos that will
appear on Studyspace

4. Tutorail 2 Additional Tasks

We need an intermediate step that just reads from a file in the main test method

CI5100 Software Development in Java Pair Programming Workshop Tutorial 1



Only after this works should a special file reading method be created
Ideally we should not have core functions of our application, such as reading in
data, in our main method. We have no obvious place for this function as it
cannot be associated with the Student class it reads in many student objects
and not just one.
One option (there are others think of these) is to create a Utility classes which
can perform functions for our application that can be used by other classes.

W2.4 Create a new Utility Class for You may wish to put this class into a new
reading (and potentially writing)
package or put it into the same package as
data into your application
your data class.

Give the new class a sensible name e.g.
FileIO , DataIO , DataFile, FileReadWrite etc

W2.5 Add a method to your new
If you put your data file into your NetBeans
Utility Class that reads data in
Project directory then you only have to
from your data file and returns an refer to it by its filename and dont have to
array of data objects
use the full pathname of the file.

You need to create an array of empty data

objects that will be populated by the data
from your data file.
Unless you read through the file twice (once
to count the number of lines in the file, and
once to read in the data) then you will need
to decide how big your array of data
beforehand i.e. a maximum size
You will need to create a BufferedReader
from a FileReader in order to read data in a
line at a time. Your file operations will have
to be in a try{ } with a catch{ } to handle
exceptions and a finally { } to close your
data file.
Use the constructor from Task W2.1 to
create the data objects from each line of the
file and in the corresponding element of the
array of data objects
Finally test this new method by calling it
from the main method in your data class
and printing out all the elements of the
resulting array of data objects

Table W2.2 Designing a new Utility class with responsibility for reading
data files

5. Student Data Management Application

In Lecture 3 the Student Data Management Application is developed to a similar


stage as above.

CI5100 Software Development in Java Pair Programming Workshop Tutorial 1


Figure W2.2 Example taken from Lecture (similar to your application)



Although this will not be the same as your application it shows the new Utility
class which will is used to read in data from the Student data file and return an
array of Student[ ] created from the rows of the data file.

Вам также может понравиться