Courses‎ > ‎APCS - Term 1‎ > ‎Konstantinovich‎ > ‎

2019-02-26 Amazing Foundations

posted Feb 26, 2019, 5:49 AM by Konstantinovich Samuel   [ updated Feb 26, 2019, 8:32 AM ]
Trip forms: print and try to get them signed and handed in as the deadline is a hard deadline.

Goal: Reminder: Files are useful.

Do now: (on paper!)
List some advantages and limitations of recursive backtracking?
What kind of problems can it be used for?


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 can have the method declare that it will throw the exception (as in the following example)

You can also state that it throws a superclass of the exception:
e.g. A method that doesn't catch a FileNotFoundException method can state that:

throws FileNotFoundException
throws IOException
throws Exception


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.
        //This is generally bad behavior
 
        File text = new File("input.txt");
        // can be a path like: "/full/path/to/file.txt" or "../data/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
        }       
    }   
}

It is FAR better to catch the exception and handle it. 
You almost never want to have your main throw anything.
You DO sometimes want your other methods to throw checked
exceptions so that a different method can catch them.


Classwork:

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.


While you do this, think about how we could solve a maze... what algorithms* might you try. 
*(Not computer, just general steps to take to solve it)
Comments