Class Notes - October 26
We have changed the order of topics around, and we will not cover arrays today. Instead we will go over the following topics. Also, we were considering a quiz for this coming Thursday, but it will most likely be sometime next week. We will keep you posted...
Topics for today:
Review of File Reading and Writing:
From Thursday's Class:
File Input Reader: File f =
new File("file.txt"); br.readLine will read a line of the file |
Traditional 'System.in'
reader: InputStreamReader isr = new
InputStreamReader(System.in);
br.readLine will read a line from the user |
File Writer: File fout = new
File ("outputFile.txt"); ps.println() will write a line of output to the file |
Traditional 'System.out'
writer: // don't need any extra code to set up System.out
System.out.println() will write a line of output to the screen |
Tips:
When opening a file, remember that it is risky code and needs to go in a try statement (FileNotFoundException)
In the above examples-
for file input, br.readLine() will read a line at a time from the input file;
for file output, ps.println() will write a line at a time to the output file.
Code Examples:
Files.java (reads lines of Strings from one file, saves them to another, and counts the lines)
FileTest.java (reads lines of Strings from a file and prints them out)
FileTest2.java (reads input from the keyboard and saves it to a file)
Alternative File Reader / Writer: FileReader and FileWriter
Code Example: NewFile.java (fixed)
Last Thursday's Way: New way using File Reader / File Writer:
Using FileInputStream: File f =
new File("file.txt"); br.readLine will read a line of the file |
FileReader:
File f = new File("file.txt");
|
Using FileOutputStream: File fout = new
File ("outputFile.txt");
|
FileWriter: File fout = new
File ("outputFile.txt"); pw.println() will write a line of output to the file
// at the end of your
program, do this to write the file |
Code Example: NestedFor.java
1. Loops (10 points each /20 points)
for (i = 1; i<10; i= i +3){ for (k=i; k>0; k= k-2){ System.out.println(k+i); } } |
What will the loop print?
|
Is this an infinite loop? YES NO |
int k = 3; while(k<5){ if(k%2==0){ System.out.println(k); } else { k = k +1; } br.readLine( ); }
|
What will the loop print?
|
2. Loops (10 points each /20 points)
for (i = 12; i<20; i= I-5){ for (k=i; k>0; k= k-i){ System.out.println(k+i);
} System.out.println("still in here"); }
|
What will the loop print?
|
Is this an infinite loop? YES NO |
int k= 4; while(k<5){ if(k%2==0){ System.out.println(k); } else { k = k +1; } }
|
What will the loop print?
|
Is this an infinite loop? YES NO |
for (i = 1; i<12; i= i +4){ for (k=7; k<i; k++){ System.out.println(k+i); } } |
What will the loop print?
|
Is this an infinite loop?
YES NO |
3. Loops (10 points each /20 points) This exam has a total of 90 points
int k =0; int i =9; for (i = 1; i<3; i= i ++){ for (k=5; k<8; k= k+2){ System.out.println(“adding “ + (k+i)); } }
|
What will the loop print?
|
Is this an infinite loop? YES NO |
Four ways to add two numbers together:
What are the differences in the four methods above? How would we call them from the main method?
Breaking down the structure (method signature) we get the following:
There are two things that make the four different types of 'add' methods above: what we pass in to the method, and what gets returned from the method. Try writing all four methods out to see what they look like.
Discussion of Lab 15 and Project 2:
http://www2.hawaii.edu/~tp_212/fall2004/lab15.html
http://www2.hawaii.edu/~tp_200/ics111/fall2004/project2.html (this isn't finalized yet- changes to the assignment may still be made)