CS49J Lesson 21 – Command line arguments and Exceptions

Ch 11: 11.3-11.4

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Eclipse or your IDE
  4. Navigate to laughton.com/obrien/sjsu/
    cs49j/lessons and open this lesson

Questions

Concept - Command line

 

Activity - Command line arguments

Record your participation in Piazza clicker question Lesson21 Q1

Last lesson you had an activity where you read lines containing height of presidents from a hard-coded file name. Here was the problem statement

There is a theory that the being taller is an advantage in a U.S. presidential election. Download presidents.zip. It contains a text file with a list of all presidents and their heights, and also a starter program. The entries in the presidents_by_height.txt file have this format

Abraham Lincoln 6 ft 4 in 193  cm
Lyndon B. Johnson 6 ft 3.5 in 192 cm
Create a project with the files and complete the PresidentAverageHeight class to find the average height in centimeters of all US presidents to date (or 0 if the file is empty). Print with 2 decimal places. You will use nextLine, split, and Integer.parseInt()

Now modify the program to get the file name as a command line argument. Be sure there was an argument before you try to access it. Print a Usage message if there is no parameter.

 

Concept - Exception handling

Concept - Purpose of exception handling

Concept - try / catch

Activity - try/catch

Record your participation in Piazza clicker question Lesson21 Q2

Now in your PresidentAverageHeight.java class

Activity - try/catch

Record your participation in Piazza clicker question Lesson21 Q3

Below is the specificaion for the ReadAlice program

There is a file at http://www.laughton.com/obrien/sjsu/cs49j/lessons/lesson20/alice30.txt You are to read the file a character at a time and determine what percentage of the letters are letters, numbers, and punctuation (anything else except whitespace).

Do not copy the file into your project. Read it from the URL.

Use the static methods in the Character class to classify the characters. (page 526, section 11.2.3)

Approximately what percent are letters? (round to a whole number)

  1. 51
  2. 71
  3. 83
  4. something else

Here is the completed version: ReadAlice.java.

Modify the program to use try catch rather than declaring that the method throws exceptions.

 

Concept - When not to throw an exception

Do not throw an exception when a simple if statement will prevent the error

Activity - When not to throw an exception

Record your participation in Piazza clicker question Lesson21 Q4

Which of these exceptions should you generally not place in a try/catch block? Select all that apply

  1. FileNotFoundException
  2. ArrayIndexOutOfBoundsException
  3. NullPointerException
  4. MalformedURLException
  5. InputMismatchException

Concept - try-with-resource

 

Activity - try-with-resource

Record your participation in Piazza clicker question Lesson21 Q5

Now in your ReadFile.java class

Concept - Creating custom exceptions

In your own classes

 

-