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");
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);

br.readLine will read a line of the file
 

Traditional 'System.in' reader:

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

 

br.readLine will read a line from the user

 

File Writer:

File fout = new File ("outputFile.txt");
FileOutputStream fos = new FileOutputStream (fout);
PrintStream ps = new PrintStream(fos);

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:

  1. When opening a file, remember that it is risky code and needs to go in a try statement (FileNotFoundException)

  2. 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");
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);

br.readLine will read a line of the file
 

FileReader:

File f = new File("file.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);


br.readLine will read a line from the user

 

Using FileOutputStream:

File fout = new File ("outputFile.txt");
FileOutputStream fos = new FileOutputStream (fout);
PrintStream ps = new PrintStream(fos);


ps.println() will write a line of output to the file
 

FileWriter:

File fout = new File ("outputFile.txt");
FileWriter fw = new FileWriter(fout);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);

pw.println() will write a line of output to the file

// at the end of your program, do this to write the file
// this is the step that we forgot to do earlier...
pw.close();  // to close the PrintWriter and write the file

 


Nested Loops:

Code Example:  NestedFor.java


Review of Loops:

 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

 


Review of Writing a Method:

Four ways to add two numbers together:

  1. public static void add ( ) {
      ......  // main passes nothing, so need to ask for numbers here
      ......  // since we return nothing, we should output our answer here
    }
  2. public static int add ( ) {
      ......  // main passes nothing, so need to ask for numbers here
      ......  // since we return our result, we can print it in back in the main method
    }
  3. public static void add (int x, int y) {
      ......  // main passes the two numbers to add
      ......  // since we return nothing, we should output our answer here
    }
  4. public static int add (int x, int y) {
      ......  // main passes the two numbers to add
      ......  // since we return our result, we can print it in back in the main method
    }

What are the differences in the four methods above?  How would we call them from the main method?

  1. add( );
  2. int sum = add( );
  3. add(numA, numB);
  4. int sum = add(numA, numB);

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)