Lesson 6C – Loops 3 – Advanced File Processing By John B. Owen All rights reserved ©2011.

Slides:



Advertisements
Similar presentations
Lesson 6D – Loops and String Arrays – split process By John B. Owen All rights reserved ©2011.
Advertisements

Lesson 9 – Matrices By John B. Owen All rights reserved ©2011.
Lesson 8 – Passing Parameters By John B. Owen All rights reserved ©2011.
Lesson 6A – Loops By John B. Owen All rights reserved ©2011.
Introduction to arrays
1 Various Methods of Populating Arrays Randomly generated integers.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Logo Lesson 5 TBE Fall 2004 Farah Fisher. Prerequisites  Given a shape, use basic Logo commands and/or a procedure to draw the shape, with and.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Ch 2.1 – The Real Number Line
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Pairwise Alignment, Part I Constructing the Values and Directions Tables from 2 related DNA (or Protein) Sequences.
Introduction to Python
Lists in Python.
THE BIG PICTURE. How does JavaScript interact with the browser?
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 2 - Algorithms and Design
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
By the end of this session you should be able to...
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Objectives Set the margins of a document. Align text.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
G045 Lecture 08 DFD Level 1 Diagrams (Data Flow Diagrams Level 1)
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Wikispaces in Education Tutorial ESA, Region 2 Mary Teply Marge Hauser.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to Programming
Intro to PHP & Variables
Iterations Programming Condition Controlled Loops (WHILE Loop)
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
Introduction to Programming
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Introduction to Programming
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Switch Case Structures
Presentation transcript:

Lesson 6C – Loops 3 – Advanced File Processing By John B. Owen All rights reserved ©2011

Objectives File input setup/scenarios Scenario #1 – unknown quantity of values Scenario #2 – N data sets Warning – manage your white space! Lesson Summary / Labs Acknowledgement of use Table of Contents Lesson 6C – Loops 3 – Advanced File Processing2

In the previous lesson, you learned ways to use loops in problem solving that require repetitive actions, such as summation and counting. In this lesson you will learn about more file processing techniques in addition to what you have been using. Objectives Lesson 6C – Loops 3 – Advanced File Processing3

Finally, you will be given more practice in using the file input processes you have learned in this lesson. Objectives Lesson 6C – Loops 3 – Advanced File Processing4

As we have learned before, the setup steps required for file input are listed below: import java.io.*; //package that contains the File class import java.util.*; // package that contains the Scanner class throws IOException // added to the main method header Scanner f = new Scanner (new File(“fileName.in”)); //links program file to data file File input setup - reminder Lesson 6C – Loops 3 – Advanced File Processing5

An input text file can be arranged in several different ways. Three ways we will explore are: File Scenario #1 - An unknown quantity (one or more) of data items arranged vertically or horizontally (what we have been doing so far) File Scenario #2 - A single value N at the top of the file, followed by N sets of data, usually arranged vertically Three file input scenarios Lesson 6C – Loops 3 – Advanced File Processing6

File Scenario #3 - An unknown quantity of values all on one line separated by spaces, harvested using a special advanced technique called “split” (we’ll devote an entire lesson – 6D – for this process, which will require a brief introduction to arrays) Three file input scenarios Lesson 6C – Loops 3 – Advanced File Processing7

This first example is the one we have been using for a while - reading an unknown quantity of values input from a data file using the while(f.hasNext()) process. We will now study in detail how it actually works. To make it a bit more interesting, we’ll test these values to see if each is a prime number, using a method we will design called isPrime. This method requires the use of a loop to test for the factors of a number. See the example on the next slide. File input scenario #1 – Unknown quantity of values Lesson 6C – Loops 3 – Advanced File Processing8

File input scenario #1 – Unknown quantity of values Lesson 6C – Loops 3 – Advanced File Processing9 This is the file scenario we have been using exclusively in lessons so far: an unknown quantity of values, usually arranged vertically in the data file. A while(f.hasNext()) loop is required to process all of these.

File input scenario #1 – Unknown quantity of values Lesson 6C – Loops 3 – Advanced File Processing10 The hasNext() method belongs to the Scanner object f and simply returns a boolean value, indicating whether or not there is a “next” item in the data file. Let’s discuss how this works…

File input scenario #1 – Unknown quantity of values Lesson 6C – Loops 3 – Advanced File Processing11 When the Scanner object f is first constructed using the “new” command, and linked to the datafile, an invisible data pointer points to the first element in the data file. The hasNext() method “asks” the data pointer if it is pointing to anything. Do you have something for me???

File input scenario #1 – Unknown quantity of values Lesson 6C – Loops 3 – Advanced File Processing12 If the data pointer IS pointing to an element, the hasNext() method returns a true to the while loop, which then performs the action statements inside the loop. The “step” in this loop is the nextInt command, which does two things.

File input scenario #1 – Unknown quantity of values Lesson 6C – Loops 3 – Advanced File Processing13 The nextInt command retrieves the integer to which the data pointer is pointing, which is then assigned to the variable, and then “hops over” to the “next” integer, thus accomplishing the stepping action required for the loop.

File input scenario #1 – Unknown quantity of values Lesson 6C – Loops 3 – Advanced File Processing14 The output statement processes the input value and shows the results so far while the data pointer is ready with the next data element in the file.

File input scenario #1 – Unknown quantity of values Lesson 6C – Loops 3 – Advanced File Processing15 This two-part retrieval and stepping action continues as long as the data pointer still has something to point to.

File input scenario #1 – Unknown quantity of values Lesson 6C – Loops 3 – Advanced File Processing16 When the data pointer runs out of elements in the file and is now pointing to the eof (an invisible “end-of-file marker”), it causes the hasNext() command to return a false to the while loop, causing the loop to stop and the entire file input process to finish. eof

File input scenario #2 – N sets of data Lesson 6C – Loops 3 – Advanced File Processing17 To demonstrate this file input scenario, a common one used in programming competitions such as UIL and ACM, we’ll use a simple situation. The data file contains an initial value N, which indicates there are N sets of data to follow. A for loop is appropriate to use, but any loop will do. Let’s discuss this process…

File input scenario #2 – N sets of data Lesson 6C – Loops 3 – Advanced File Processing18 For simplicity’s sake, we’ll just focus on the file input process without any method calls or other processes for now. After the Scanner f object is constructed and linked, the initial integer N is input from the file. Instead of using the hasnext() feature as we did before, we’ll now just set up a for loop to input N sets of data. Again, any loop will do. Notice carefully that the loop control variable starts at zero, and the check is x<N. This achieves the correct number of input steps, the same as if you started at 1 and made the check x<=N.

File input scenario #2 – N sets of data Lesson 6C – Loops 3 – Advanced File Processing19 Since N has a value of 4, 4 sets of data are input, and four outputs are the result, as you can see below…very simple and straightforward.

Warning: manage the white space! Lesson 6C – Loops 3 – Advanced File Processing20 Here is a reminder and a warning: When a data file is arranged in this fashion with an initial integer N followed by several words or phrases requiring the nextLine() command to harvest, you must manage the whitespace character left behind by the nextInt() command. To do this, simply follow the nextInt() command with a nextLine() command, and all will be well. int N = f.nextInt(); f.nextLine();

In this lesson, you learned additional techniques for inputting data into your program from external data files, using several scenarios that are commonly used. Now it is time to practice with several examples. Lesson Summary Lesson 6C – Loops 3 – Advanced File Processing21

MyLoops3 will be the name of this series of labs. As you did before, create a separate folder and file called MyLoops3 and do your work there. The first method is done for you to help you get started. Labs Lesson 6C – Loops 3 – Advanced File Processing22

Lab 1 – Col to Row doubles Lesson 6C – Loops 3 – Advanced File Processing23 WAP in the main method to read and output each of the values in a data file using File Scenario #1 (unknown quantity of values). The values in the data file are arranged vertically. Output is to be in a single HORIZONTAL line with each value formatted to two decimal places and separated by single spaces. A solution is shown below.

Lab 2 – Odd/Even Lesson 6C – Loops 3 – Advanced File Processing24 WAP in the main method to read and output each of the values in a data file (again, use FS#1). The values in the data file are arranged horizontally. Output is to be in a column with each value labeled as ODD or EVEN. Define and use a static method called oddEven to receive an integer parameter and return the appropriate string, ODD or EVEN.

Lab 3 – String length Lesson 6C – Loops 3 – Advanced File Processing25 WAP to display the lengths of the words stored in a data file. The data file will be File Scenario #2, with an initial N value at the beginning, followed by N words or phrases. Output each word or phrase, labeled with its length. Hint: Remember to manage the white space!!!

Lab 4 – String length 2 Lesson 6C – Loops 3 – Advanced File Processing26 Repeat Lab 3, but this time align all length values right justified in column 25, as shown below.

Lab 5 – Character Type Lesson 6C – Loops 3 – Advanced File Processing27 WAP to read a single String from a data file and determine if each character in that String is a letter, a digit, or a symbol. Output each character along with the word LETTER, DIGIT, or SYMBOL as appropriate. Define and use the method charType to receive a single character and return a String with the correct “type” as indicated above. Hint: The Character class would come in handy here.

Lab 6 – Integer pairs Lesson 6C – Loops 3 – Advanced File Processing28 WAP to read in N pairs of integers and determine which is the larger and smaller value, or if they are equal, using the output format shown below. The entire resulting String should be calculated in a method called intPairs that receives the two integers and returns the appropriate String sentence. Hint: The Math class would be very helpful here.

Lab 7 – Social Security Lesson 6C – Loops 3 – Advanced File Processing29 WAP that will read an unknown quantity of nine-digit numbers representing Social Security numbers and output that number properly formatted with dashes. For example, the String would be formatted as

Lab 8 – Area and Circumference Lesson 6C – Loops 3 – Advanced File Processing30 WAP that will read N diameter values and output in table format the radius, area, and circumference of the circle. All output - headings, values, alignment and decimal formatting - must match EXACTLY as shown. The area and circumference must be calculated in return methods called area and circ, both of which receive a decimal value. Use Math.PI in your calculations.

Complete the documentation for all of the methods, run the JavaDoc utility, and print out and submit for a grade the API for your MyLoops2 class. Also print out and turn in the entire source code for this lab. JavaDoc Lesson 6C – Loops 3 – Advanced File Processing31

You now know how to use two different file processes to input data to your program. Lesson 6D will explore a third file input process that uses the String “split” command to create an array of Strings. CONGRATULATIONS! Lesson 6C – Loops 3 – Advanced File Processing32

Please copy, paste, and customize the brief acknowledgement shown below in an to indicating that you used this lesson. Feel free to include any comments you wish to offer, positive or negative. “I, (your name), from (educational institution) in (city, state, country) used Lesson 6C on (date), understand, respect and honor all of the rights claimed by the author, Mr. John Owen, and offer the following comments..._______________.” “THANK YOU!” Acknowledgement of use Lesson 6C – Loops 3 – Advanced File Processing33