nested loops, choosing loops, break, files

Download Report

Transcript nested loops, choosing loops, break, files

QMCS 230: Today in Class
•
•
•
•
•
•
Homework
Nested Loops
Other things
Group work if time
Start working with files?
Lab 8
March 2005
R. Smith - University of St Thomas - Minnesota
1/18
Homework
• Typical weekly assignments
– 1 “informal” lab
• Hand in source code ONLY
• Less credit than the “formal” lab
• Checked in general for content and form
– 1 “formal” lab
• E-mail source code ONLY
– Checked carefully
• Grading
–
–
–
–
–
March 2005
Check plus plus – a few labs – for special work – think A+
Check plus – almost no problems – think A
Check – Some problems – think of a B
Check Minus – many or major problems – think of a C
No Credit – major parts of the assignment missing
R. Smith - University of St Thomas - Minnesota
2/18
On the Exam
• Chapter 2, all sections except
– 2.6, 2.8
• Chapter 3, all sections except
– 3.8, 3.9
• Chapter 4, Sections 4.1-4.6
• Study the ‘Checkpoints’
March 2005
R. Smith - University of St Thomas - Minnesota
3/18
Reference Materials
• I will provide reference guides
– Java syntax basics
– Reserved words you might use
– Tables of details, if needed
• Closed book closed notes
March 2005
R. Smith - University of St Thomas - Minnesota
4/18
Nested Loops
• How would we do hh:mm:ss ?
• What does “nesting” mean?
– Nothing to do with eggs.
March 2005
R. Smith - University of St Thomas - Minnesota
5/18
Break and Continue (Sec 4.8)
• Loops do all statements in order
• Break and Continue change this.
– Cause ‘surprising’ behavior
• They exist, but we will NOT use them
– Makes programs harder to understand
– And harder for ME to read
March 2005
R. Smith - University of St Thomas - Minnesota
6/18
Picking a loop
• Always do it at least once?
• Maybe skip it all sometimes?
• A fixed number of times?
March 2005
R. Smith - University of St Thomas - Minnesota
7/18
Picking a loop
• Always do it at least once?
– Do - While
• Maybe skip it all sometimes?
– While
• A fixed number of times?
– For loop
March 2005
R. Smith - University of St Thomas - Minnesota
8/18
Group Work
• 8 groups - 4 problems
• ALL: average 4 numbers read from
keyboard
• Different problems
–
–
–
–
Use 4 separate statements
Use ‘for’ loop
Use ‘do while’ loop
Use ‘while’ loop
March 2005
R. Smith - University of St Thomas - Minnesota
9/18
Working with Files
• We use the same methods as
System.in/out
– println, print, printf, Scanner class
• We must “open” and “close” files
– “Open” when we create the file object
– “close()” method when done
March 2005
R. Smith - University of St Thomas - Minnesota
10/18
Output Files
• PrintWriter class
– “new” takes file name as an argument
– Creates that file
• Use the object with ‘print’ methods
• Write to a file
–
–
–
–
import java.io.*;
public static void main(String[] args) throws IOException
PrintWriter pwa;
pwa = new PrintWriter(“existingfile.txt”);
• Example: write student names
March 2005
R. Smith - University of St Thomas - Minnesota
11/18
Append to Output File
• Use an extra object for the existing file
• “FileWriter” object
• FileWriter fw;
• PrintWriter pwa;
// to define the existing file
// to open for print functions
• fw = new FileWriter(“existingfile.txt”, true);
• pwa = new PrintWriter(fw);
March 2005
R. Smith - University of St Thomas - Minnesota
12/18
Input Files
• Use Scanner class and File class
• import java.util.Scanner;
• import java.io.*;
• public static void main(String[] args) throws IOException
• fn;
// identify the file to read
• Scanner sc; // for reading the data
• fn = new File(“filename.txt”);
• sc = new Scanner(fn);
March 2005
R. Smith - University of St Thomas - Minnesota
13/18
That’s it.
• Questions?
March 2005
R. Smith - University of St Thomas - Minnesota
14/18
Creative Commons License
This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/
or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California,
94105, USA.
March 2005
R. Smith - University of St Thomas - Minnesota
15/18