Courses‎ > ‎AP Computer Science 2‎ > ‎konstantinovich‎ > ‎

2018-02-26

posted Feb 26, 2018, 9:31 AM by Konstantinovich Samuel

Trip forms:
 print and try to get them signed and handed in as the deadline is a hard deadline.

Quiz Wed - Not including today onward.

Goal: Reminder: Files are useful.

Do now: (on paper!)
1. What are some kinds of problems that you can solve using recursive backtracking?
2. List 2 advantages/ 2 limitations of recursive backtracking.




Checked vs. Unchecked exceptions.

Checked Exception : are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.
An example of a checked exception is the FileNotFoundException which requires a try/catch block (handle it) or that you have the method STATE that it will throw the exception.

You can also state that it throws a superclass of the exception:
e.g. A FileNotFoundException method can state that it throws:
FileNotFoundException
OR
IOException




import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile {
  public static void main(String args[]) throws FileNotFoundException {

        //instead of a try/catch, you can throw the FileNotFoundException.
        File text = new File("input.txt");// can be a path like: "/full/path/to/file.txt" 
        
        //inf stands for the input file
        Scanner inf = new Scanner(text);

        while(inf.hasNextLine()){
            String line = inf.nextLine();
            System.out.println(line);//hopefully you can do other things with the line
        }       
    }   
}

Place this maze into a text file, and read it into 
Maze1.txt
###################################
#   #         #   #   #     # # #E#
# # ### ######### # # # ### # # # #
# # #   #           #   #         #
# # # ### ######### ##### ### ### #
# # # # #   #     #   #     # # # #
### # # ### # # ### # # # ### # ###
#S            #   # # # # #   #   #
###################################

Read it into your program. Print it. 
Then try reading it into an array of characters, and print that.

Comments