Outline Boolean Expressions The if Statement Comparing Data

Slides:



Advertisements
Similar presentations
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements 3.6 – Iterators – p
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS 1 Copyright: FALL 2014 Illinois Institute of Technology_ George Koutsogiannakis.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 14. © 2007 Pearson Addison-Wesley. All rights reserved5-2 Agenda Day 14 Problem set 3 posted  10 problems from chapters 5 & 6  Due in 11.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Chapter Day 12. © 2007 Pearson Addison-Wesley. All rights reserved5-2 Agenda Day 12 Problem set corrected  1 A, 2 B’s and 1 D Starting Late and not turning.
Chapter 4 Conditionals and Loops. Chapter Scope Flow of control Boolean expressions if and switch statements Comparing data while, do, and for loops Iterators.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2014 Pearson Education, Inc. Chapter 5 Conditionals and Loops Java Software Solutions Foundations of Program Design 8 th Edition John Lewis.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
1 Review of Java Basic Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – conditional statements –Flow of Control.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 Conditionals and Loops
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
COS 312 DAY 5 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 2 Posted – Due Feb 22 prior to class – Any issues? Assignment 3 will be posted soon Capstones.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
OBJECT ORIENTED PROGRAMMING I LECTURE 10 GEORGE KOUTSOGIANNAKIS
Introduction to programming in java
Project 1.
OBJECT ORIENTED PROGRAMMING II LECTURE 2 GEORGE KOUTSOGIANNAKIS
CSC 1051 – Data Structures and Algorithms I
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
Loop Structures.
COMPUTER 2430 Object Oriented Programming and Data Structures I
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Outline Boolean Expressions The if Statement Comparing Data
Conditionals and Loops
Exercise on Java Basics
Flow of Control Unless specified otherwise, the order of statement execution through a method is linear: one after another Some programming statements.
Chapter 6 More Conditionals and Loops
Chapter 5 Conditionals and Loops
Introduction to Classes and Methods
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Iterator.
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
Outline Boolean Expressions The if Statement Comparing Data
OBJECT ORIENTED PROGRAMMING II LECTURE 13_1 GEORGE KOUTSOGIANNAKIS
Chapter 5 Conditionals and Loops
The Random Class The Random class is part of the java.util package
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Outline Software Development Activities
Object-Oriented Design Part 2
Outline Boolean Expressions The if Statement Comparing Data
Presentation transcript:

Outline Boolean Expressions The if Statement Comparing Data The while Statement Iterators The ArrayList Class Determining Event Sources Check Boxes and Radio Buttons Copyright © 2012 Pearson Education, Inc.

Iterators An iterator is an object that allows you to process a collection of items one at a time It lets you step through each item in turn and process it as needed An iterator has a hasNext method that returns true if there is at least one more item to process The next method returns the next item Iterator objects are defined using the Iterator interface, which is discussed further in Chapter 7 Copyright © 2012 Pearson Education, Inc.

Iterators Several classes in the Java standard class library are iterators The Scanner class is an iterator the hasNext method returns true if there is more data to be scanned the next method returns the next scanned token as a string The Scanner class also has variations on the hasNext method for specific data types (such as hasNextInt) Copyright © 2012 Pearson Education, Inc.

Iterators The fact that a Scanner is an iterator is particularly helpful when reading input from a file Suppose we wanted to read and process a list of URLs stored in a file One scanner can be set up to read each line of the input until the end of the file is encountered Another scanner can be set up for each URL to process each part of the path See URLDissector.java Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // URLDissector.java Author: Lewis/Loftus // // Demonstrates the use of Scanner to read file input and parse it // using alternative delimiters. import java.util.Scanner; import java.io.*; public class URLDissector { //----------------------------------------------------------------- // Reads urls from a file and prints their path components. public static void main (String[] args) throws IOException String url; Scanner fileScan, urlScan; fileScan = new Scanner (new File("urls.inp")); continue Copyright © 2012 Pearson Education, Inc.

// Read and process each line of the file while (fileScan.hasNext()) { continue // Read and process each line of the file while (fileScan.hasNext()) { url = fileScan.nextLine(); System.out.println ("URL: " + url); urlScan = new Scanner (url); urlScan.useDelimiter("/"); // Print each part of the url while (urlScan.hasNext()) System.out.println (" " + urlScan.next()); System.out.println(); } Copyright © 2012 Pearson Education, Inc.

Sample Run URL: www.google.com www.google.com URL: www.linux.org/info/gnu.html www.linux.org info gnu.html URL: thelyric.com/calendar/ thelyric.com calendar URL: www.cs.vt.edu/undergraduate/about www.cs.vt.edu undergraduate about URL: youtube.com/watch?v=EHCRimwRGLs youtube.com watch?v=EHCRimwRGLs continue // Read and process each line of the file while (fileScan.hasNext()) { url = fileScan.nextLine(); System.out.println ("URL: " + url); urlScan = new Scanner (url); urlScan.useDelimiter("/"); // Print each part of the url while (urlScan.hasNext()) System.out.println (" " + urlScan.next()); System.out.println(); } Copyright © 2012 Pearson Education, Inc.