info343/lectures/s5/ui/lab6-files-whitaker.shtml

<!--#include virtual="commontop.html" -->

      <title>CSE 142 (190) Lab 6</title>
   </head>

   <body>
      <div class="layout">
         <div id="controls"><!-- DO NOT EDIT --></div>
         <div id="currentSlide"><!-- DO NOT EDIT --></div>
         <div id="header"></div>
         <div id="footer">
            <h1>University of Washington, CSE 142 (190)</h1>
            <h2>Lab 6: File Processing</h2>
         </div>
      </div>

      <div class="presentation">
         <div class="slide">
            <h1>University of Washington, CSE 142 (190)</h1>
            <h2>Lab 6: File Processing</h2>

            <p class="license">
               Except where otherwise noted, the contents of this document are Copyright 2010 Stuart Reges and Marty Stepp.
            </p>

            <p class="license">
               <em>lab document created by Whitaker Brand and Marty Stepp</em>
            </p>

            <!--
            <div class="w3c">
               <a href="http://validator.w3.org/check/referer"><img src="images/w3c-xhtml11.png" alt="Valid XHTML 1.1" /></a>
               <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="images/w3c-css.png" alt="Valid CSS!" /></a>
            </div>
            -->
         </div>



         <div class="slide">
            <h1>Today's lab</h1>

            <p>Goals for today:</p>

            <ul>
               <li>
                  use <code>Scanner</code> and <code>File</code> objects to read input data from files
               </li>

               <li>
                  understand the way a <code>Scanner</code> breaks input into tokens
               </li>

               <li>
                  explore token-based and line-based input
               </li>

               <li>
                  Where you see this icon, you can click it to check the problem in Practice-It!

                  <span class="practiceitlink">
                     <img src="checkmark.png" alt="practice-it" width="26" height="26" />
                  </span>
               </li>
            </ul>
         </div>



         <div class="slide">
            <h1>Recall: <code>Scanner</code> Methods</h1>

            <p>
               To use these methods, you need a variable of type <code>Scanner</code>:
            </p>

            <pre>
Scanner fileScanner = new Scanner(new File("input.txt"));
String theNextString = <strong>fileScanner.next();</strong>
</pre>

            <table class="standard">
               <tr>
                  <th>
                     Method name
                  </th>

                  <th>
                     Outcome
                  </th>
               </tr>

               <tr>
                  <td style="white-space: nowrap">
                     <code>next()</code>
                  </td>

                  <td>
                     reads and returns the next token as a <code>String</code>
                  </td>
               </tr>

               <tr>
                  <td style="white-space: nowrap">
                     <code>nextLine()</code>
                  </td>

                  <td>
                     reads and returns as a <code>String</code> all the characters up to (but excluding) the next new line ('\n')
                  </td>
               </tr>

               <tr>
                  <td style="white-space: nowrap">
                     <code>nextInt()</code>
                  </td>

                  <td>
                     reads and returns the next token as an <code>int</code>, if possible.
                  </td>
               </tr>

               <tr>
                  <td style="white-space: nowrap">
                     <code>nextDouble()</code>
                  </td>

                  <td>
                     reads and returns the next token as <code>double</code>, if possible.
                  </td>
               </tr>

               <tr>
                  <td style="white-space: nowrap">
                     <code>hasNext()</code>
                  </td>

                  <td>
                     returns whether there is still a token in the <code>Scanner</code>
                  </td>
               </tr>

               <tr>
                  <td style="white-space: nowrap">
                     <code>hasNextLine()</code>
                  </td>

                  <td>
                     returns whether there is still at least one line left to be read in the <code>Scanner</code>
                  </td>
               </tr>

               <tr>
                  <td style="white-space: nowrap">
                     <code>hasNextInt()</code>
                  </td>

                  <td>
                     returns whether the next token can be read as an <code>int</code>
                  </td>
               </tr>

               <tr>
                  <td style="white-space: nowrap">
                     <code>hasNextDouble()</code>
                  </td>

                  <td>
                     returns whether the next token can be read as an <code>double</code>
                  </td>
               </tr>


            </table>
         </div>

         <div class="slide">
            <h1>Exercise <span class="exercisenumber"></span>: Token-izing</h1>

            <p>
               How many tokens are in the following <code>String</code>? <span class="expressionarea"><input type="text" value="" class="expressionanswer" size="6" spellcheck="true" /><span class="expressionsolution">3</span></span>
            </p>

            <pre>
welcome...to the matrix.
</pre>

            <div class="mcquestion">
               <p>
                  What are the tokens that the <code>String</code> breaks up into?
               </p>
               <ol class="compressed">
                  <li>
                     <label>
                        <input type="radio" name="mcquestion1" value="answer1" class="mcincorrect" />
                        <code>"welcome", "to", "the", "matrix"</code>
                        <img class="mcresultimage" style="display: none" src="images/incorrect.gif" alt="incorrect" />
                     </label>
                  </li>

                  <li>
                     <label>
                        <input type="radio" name="mcquestion1" value="answer2" class="mcincorrect" />
                        <code>"welcome...to the matrix."</code>
                        <img class="mcresultimage" style="display: none" src="images/incorrect.gif" alt="incorrect" />
                     </label>
                  </li>

                  <li>
                     <label>
                        <input type="radio" name="mcquestion1" value="answer3" class="mccorrect" />
                        <code>"welcome...to", "the", "matrix."</code>
                        <img class="mcresultimage" style="display: none" src="images/correct.gif" alt="correct" />
                     </label>
                  </li>

               </ol>
            </div>
         </div>

         <div class="slide">
            <h1>Exercise <span class="exercisenumber"></span>: More tokenizing</h1>


            <p>
               How many tokens are in the following <code>String</code>? <span class="expressionarea"><input type="text" value="" class="expressionanswer" size="6" spellcheck="true" /><span class="expressionsolution">9</span></span>
            </p>

            <pre>
in fourteen-hundred 92\ncolumbus sailed the ocean blue :)
</pre>

            <div class="mcquestion">
               <p>
                  What are the tokens that the <code>String</code> breaks up into?
               </p>
               <ol class="compressed">
                  <li>
                     <label>
                        <input type="radio" name="mcquestion1" value="answer1" class="mcincorrect" />
                        <code>"in", "fourteen", "hundred", "92", "columbus", "sailed", "the", "ocean", "blue"</code>
                        <img class="mcresultimage" style="display: none" src="images/incorrect.gif" alt="incorrect" />
                     </label>
                  </li>

                  <li>
                     <label>
                        <input type="radio" name="mcquestion1" value="answer3" class="mccorrect" />
                        <code>"in", "fourteen-hundred", "92", "columbus", "sailed", "the", "ocean", "blue", ":)"</code>
                        <img class="mcresultimage" style="display: none" src="images/correct.gif" alt="correct" />
                     </label>
                  </li>

                  <li>
                     <label>
                        <input type="radio" name="mcquestion1" value="answer2" class="mcincorrect" />
                        <code>"in", "fourteen-hundred", "92\ncolumbus", "sailed", "the", "ocean", "blue :)"</code>
                        <img class="mcresultimage" style="display: none" src="images/incorrect.gif" alt="incorrect" />
                     </label>
                  </li>
               </ol>
            </div>
         </div>


         <div class="slide">
            <h1>Exercise <span class="exercisenumber"></span>:</h1>

            <p>
               For the next several questions, consider a file called <code>readme.txt</code> that has the following contents:
            </p>


            <pre>
6.7           This file has


several input lines.
 10 20
</pre>

            <p>
               What would be the output from the following code when it is run on the <code>readme.txt</code> file?
            </p>

            <pre>
Scanner input = new Scanner(new File("readme.txt"));
int count = 0;
while (input.hasNextLine()) {
    System.out.println("input: " + input.nextLine());
    count++;
}
System.out.println(count + " total");
</pre>
         </div>

         <div class="slide">
            <h1>Exercise <span class="exercisenumber"></span>: <code>runningSum</code>
               <a target="_blank" class="practiceitlink" title="view in Practice-It!" href="http://webster.cs.washington.edu:8080/practiceit/problem.jsp?category=142+Final+Exams&problem=practicefinal8-runningSum">
                  <img src="checkmark.png" width="26" height="26" alt="practice-it" />
               </a>
            </h1>

            <p>
               <strong>Write a static method</strong> called <code>runningSum</code> that accepts as a parameter a <code>Scanner</code> holding a sequence of real numbers and that outputs the running sum of the numbers followed by the maximum running sum.  For example if the Scanner contains the following data:
            </p>

            <pre>
3.25 4.5 -8.25 7.25 3.5 4.25 -6.5 5.25
</pre>

            <p>
               your method should produce the following output:
            </p>

            <pre>
running sum = 3.25 7.75 -0.5 6.75 10.25 14.5 8.0 13.25
max sum = 14.5
</pre>

            <p>
               Click on the check-mark above to try to solve this problem in Practice-it!
            </p>
         </div>

         <div class="slide">
            <h1>Exercise <span class="exercisenumber"></span>: <code>flipLines</code>
               <a target="_blank" class="practiceitlink" title="view in Practice-It!" href="http://webster.cs.washington.edu:8080/practiceit/problem.jsp?category=142+Final+Exams&problem=practicefinal2-flipLines">
                  <img src="checkmark.png" width="26" height="26" alt="practice-it" />
               </a>
            </h1>

            <p>
               <strong>Write a static method</strong> named <code>flipLines</code> that accepts as its parameter a <code>Scanner</code> for an input file and that writes to the console the same file's contents with successive pairs of lines reversed in order. For example, if the input file contains the following text:
            </p>

            <pre class="compressed">
Twas brillig and the slithy toves
did gyre and gimble in the wabe.
All mimsey were the borogroves,
and the mome raths outgrabe.

"Beware the Jabberwock, my son,
the jaws that bite, the claws that catch,
Beware the JubJub bird and shun
the frumious bandersnatch."
</pre>

            <p>
               your method should produce the following output:
            </p>

            <pre class="compressed">
did gyre and gimble in the wabe.
Twas brillig and the slithy toves
and the mome raths outgrabe.
All mimsey were the borogroves,
"Beware the Jabberwock, my son,

Beware the JubJub bird and shun
the jaws that bite, the claws that catch,
the frumious bandersnatch."
</pre>

            <p>
               Click on the check-mark above to try to solve this problem in Practice-it!
            </p>
         </div>

         <div class="slide">
            <h1>Exercise <span class="exercisenumber"></span>: <code>coinFlip</code>
               <a target="_blank" class="practiceitlink" title="view in Practice-It!" href="http://webster.cs.washington.edu:8080/practiceit/problem.jsp?category=142+Final+Exams&problem=practicefinal3-coinFlip">
                  <img src="checkmark.png" width="26" height="26" alt="practice-it" />
               </a>
            </h1>

            <p>
               <strong>Write a static method</strong> named <code>coinFlip</code> that accepts as its parameter a <code>Scanner</code> for an input file. Assume that the input file data represents results of sets of coin flips that are either heads (H) or tails (T).  Your method should consider each line to be a separate set of coin flips and should output to the console the number of heads and the percentage of heads in that line. If this percentage is more than 50%, you should print a "You win" message. For example, consider the following input file:
            </p>

            <pre class="compressed">
H T H H T
T t      t T h  H
       h
</pre>

            <p>
               For the input above, your method should produce the following output:
            </p>

            <pre class="compressed">
3 heads (60.0%)
You win!

2 heads (33.3%)

1 heads (100.0%)
You win!
</pre>

            <p>
               Click on the check-mark above to try to solve this problem in Practice-it!
            </p>
         </div>









<!--#include virtual="commonbottom.html" -->