RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.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.

  1. 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.)
  2. 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.
  3. Ian -- should I provide unit tests? We will create unit tests, before writing our method.
  4. 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.)
    1. 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.)
    2. 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.
    3. 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                 .
    4. Loop body: How do you combine this current PizzaServer with our “so-far” variable?
    5. 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:


1Note that we can't use a for-each loop; we don't have a pre-existing list which we want to process.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Aug.27 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme