Pair Programming Phase 1 Tutorial 2: Reading Data From A File
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
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
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.
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
We need an intermediate step that just reads from a file in the main test method