CSU Banner

CS 150, Spring 2018

Lab 15 - More Practice with Loops and File Input/Output

Wednesday - May 2, 2018


Lab 14 Regrade: If you need a regrade for the week 14 lab, please open Lab 14 requirements and show one of the TAs your completed work within the first 5 minutes of class. The TAs will not accept regrades after this time. Additionally, if you forgot to take the attendance quiz last week for the single attendance point, your maximum possible grade for the previous lab is now 3 out of 4.

Objectives of this Lab

  1. While and for loop review, and
  2. additional practice with reading files and writing to a file.

Getting Started

Create a new Java Project named Lab15, and make a class named Lab15. Import two text files into your Lab15 by right-clicking here and here. Choose either 'Save as' or 'Save link as' or 'Download link as'. Then save the file to your Lab15 project directory. Do not save to the src or bin folder. After saving, refresh your project in Eclipse to view the files.


Today's Assignment

    Part One
  1. Copy and paste the following code into your main method.
  2.   try {
    	// Program variables.
    	String lineToAnalyze = "", lineToWrite = "";
    	int numZeros = 0, numOnes = 0, numFileLines = 0, qCounter = 0, zCounter = 0;
    	Character q = new Character('q');
    	Character z = new Character('z');
    	Character zero = new Character('0');
    	Character one = new Character ('1');
    	
    	// While loop to use with contents of BriefHistoryOfCS.txt.
    			
    	// Write number of q's and z's to output file.
    			
    	// Assign numFileLines the integer value in Binary.txt
    			
    	// For loop to use with contents of Binary.txt.
    			
    	// Write number of zeros and ones to output file.
    			
    	// Close Scanners and PrintWriter.
    			
      } catch (Exception e) {
    	e.printStackTrace();
      } // end try-catch
      
  3. Add two Scanners to the listed program variables - one to source from the file name at args[0] and the other sourced from the file name at args[1].
  4. Set up your program arguments to read: BriefHistoryOfCS.txt Binary.txt
  5. Instantiate a PrintWriter to write to the file Output.txt. For this lab, the argument for your File object should be the string literal "Output.txt", not an element in args[].

  6. Part Two
  7. Write a while loop using your Scanner object that references the BriefHistoryOfCS.txt file.
  8. First include your while loop, assign lineToAnalyze the next line of the file using your Scanner.
  9. Next write a for loop that iterates over the String lineToAnalyze.
  10. Using a Character wrapper class method, if a character of lineToAnalyze is lower case, change it to upper case and concatenate the resulting character to lineToWrite.
  11. Otherwise, concatenate the character from lineToAnalyze unchanged to lineToWrite.
  12. Additionally, inside the for loop you should check if each character of lineToAnalyze is a 'q' or 'z' using your Character q and z objects and if so, increment qCounter or zCounter.
  13. Before exiting the while, but after the for loop, use your PrintWriter object and write lineToWrite to the output file.
  14. After this (still inside the while loop), reassign lineToWrite to the empty String.
  15. After the while loop, print a blank line to the output file.
  16. Then print the results of number of lower case q's followed on another line by the number of lower case z's.
  17. Print another blank line to the output file after these two results.

  18. Part Three
  19. Using your Scanner that references the Binary.txt file, read the integer into numFileLines.
  20. Then, with the same Scanner make a nextLine() call.
  21. Now write a for loop using numFileLines as part of the loop's boolean expression.
  22. First in the for loop, read a line into lineToAnalyze.
  23. Then iterate over lineToAnalyze using an inner for loop and determine the number of zeros and ones in the line. Write two conditionals utilizing the zero and one Character objects and increment numZeros and numOnes appropriately.
  24. Outside of your for loops, write the results of number of zeros and number of ones to the output file.
  25. Lastly, close your Scanners and PrintWriter.
  26. See below for the expected contents of the output file.

Output File Contents

A BRIEF HISTORY OF COMPUTER SCIENCE - WORLD SCIENCE FESTIVAL

2700-2300 B.C. THE SUMERIAN ABACUS FIRST APPEARED.
87 B.C. THE ANTIKYTHERA MECHANISM, THE EARLIEST KNOWN ANALOG COMPUTER IS DEVELOPED IN ANCIENT GREECE.
1703 A.D. GERMAN MATHEMATICIAN GOTTFRIED WILHELM LIEBNITZ INTRODUCES THE BINARY NUMBER SYSTEM.
1837 CHARLES BABBAGE INVENTS THE ANALYTICAL ENGINE.
1843 ADA LOVELACE DEVELOPS THE FIRST COMPUTER ALGORITHM.
1936 ALAN TURING INVENTS THE TURING MACHINE.
1941 KONRAD ZUSE INVENTS Z3, THE FIRST PROGRAMMABLE DIGITAL COMPUTER.
1947 THE TRANSISTOR IS INVENTED AT BELL LABS.
1963 DOUGLAS ENGELBART INVENTS THE COMPUTER MOUSE.
1968 DARPA CREATES ARPANET, THE FIRST OPERATIONAL COMPUTER NETWORK AND ANCESTOR OF THE INTERNET. 
1971 KENBAK-1, THE FIRST PERSONAL COMPUTER IS SOLD IN THE USA.
1976 STEVE WOZNIAK DESIGNED THE APPLE-1, A SINGLE-BOARD COMPUTER FOR HOBBYISTS.
1981 IBM INTRODUCES ITS FIRST MS-DOS COMPUTER. 
1985 MICROSOFT RELEASES WINDOWS 1.0.
1990 SIR TIM BERNERS-LEE OF CERN INVENTS THE WORLDWIDEWEB.
1993 MARC ANDRESSON CREATED MOSAIC, LATER KNOWN AS NETSCAPE, THE FIRST POPULAR, USER-FRIENDLY WEB BROWSER.
2008 IBM SUPERCOMPUTER "ROADRUNNER" REACHES THE SPEED OF 1 PETAFLOPS.
2010 RESEARCHERS ARE ABLE TO MANIPULATE "QUBITS", QUANTUM COMPUTING INFORMATION UNITS.
2012 SCIENTISTS CULTIVATED BRAIN CELLS OF A MICROCHIP, ADVANCING NEUROCHIP TECHNOLOGY.

Number of lower case q's: 2
Number of lower case z's: 2

Number of zeros: 1049
Number of ones: 887




© 2018 CS150 Colorado State University. All Rights Reserved.
CS Banner
CS Building