home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
lab13b
loops: while, for-each practice
lab13b
In the previous lab,
wrote stringToPizzaServer,
which took in a String containing numbers and words,
and created a PizzaServer object.
(That method used a Scanner to read the data out of
the input String.)
In this lab, we will write a function which reads an entire
file full of such data, and creates a list of
PizzaServers.
Each line of the file will have the information for
exactly one PizzaServer.
-
Create test cases.
You can make files yourself, or
you can save-to-disk the test cases
lab13a-empty.txt,
lab13a-1server.txt,
lab13a-4servers.txt.
(You might want to open those files in Word or any other program,
so that you know what to expect as output.)
-
Write the signature and documentation for
fileToPizzaServers.
What modifiers should we use for the signature, out of
public,
private,
and
static?
Discuss with your partner about what each of these mean,
and which is appropriate for this method.
-
Ian -- should I provide unit tests?
We will create unit tests, before writing our method.
-
Write a stub function, so your program compiles.
- Create a new test-class, and start recording a test.
-
For each of our three test input files:
-
In the code pad, call your stub-function on
one of the three test-files.
This will return a list-of-PizzaServers.
Drag the tiny red result to the bench, and give it a name.
-
Still in the Code Pad, ask various questions:
What is the length of the list?
-
Start writing the method.
(Compare with the program from lecture,
which reads input from a web page, as long as there is more input to read.)
- Loop Initialization:
How will you read from a file?
Create a Scanner, which reads from the indicated file.
(Recall the lecture notes on
various constructors for Scanners.)
- Loop Initialization:
After having looked at (say) the first five lines of input of a file,
what information will you want to have accumulated?
Make and initialize a “so-far” variable to hold that.
- Loop body:
Write code fragment which reads a single line of the file,
and converts it into a PizzaServer.
How do you read a single line of input from a Scanner?
Call its nextLine() method.
How do you convert the resulting String into a PizzaServer?
Call .
-
Loop body:
How do you combine this current PizzaServer with our
“so-far” variable?
-
Loop control logic:
We want to this task as long as there are more lines in the file.
What will be the condition for a while loop?
1
Optional:
Additional problems:
-
Reading a file to get a list-of-Strings is a common task.
Re-structure the above into two methods:
One general-purpose function which takes a
filename and returns a list-of-Strings,
and one PizzaServer-specific method which takes a
list-of-Strings and returns a
list-of-PizzaServers.
What are some benefits and/or disadvantages of organizing
the code like this?
-
Using lessons learned in the above program, we can do something
similar for Treasures:
-
Make a Scanner which opens the gRUe wiki page,
http://ru-itec120.pbwiki.com/grue-data-2007spring
Look at the resulting Strings in that file.
We want to just get the text with our classmates' contributions,
and that is in the file, but unfortuantely we have to scan over
a bunch of html until we reach the special delimiter line
containing
“BEGIN GRUE DATA”.
-
Write loop which reads lines, looking for Treasure
declarations, and when it finds one it reads lines and calls the
Treasure constructor.
keep doing this until you read a line containing
“END GRUE DATA”.
1Note that we can't use a for-each loop;
we don't have a pre-existing list which we want to process.
↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs