Ch 11: 11.3-11.4

java ProgramName parameter-list
if (args.length < 1)
{
System.out.println("Usage: java Classname input_file.txt ");
return; //can't do anything without the file
}
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 cmCreate 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.
try
{
//most of your code will go in here.
//put the code that throws the exception and any code that depends on it in the try block
}
catch(FileNotFoundException e )
{
System.out.println(e.getMessage());
}
catch(Exception e ) //some other exeption
{
System.out.println(e.getMessage());
}Record your participation in Piazza clicker question Lesson21 Q2
Now in your PresidentAverageHeight.java class
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)
- 51
- 71
- 83
- something else
Here is the completed version: ReadAlice.java.
Modify the program to use try catch rather than declaring that the method throws exceptions.
Do not throw an exception when a simple if statement will prevent the error
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
PrintWriter out = new PrintWriter(new File("output.txt"), "UTF-8");
//other statements might throw an exception
out.println("Number of even numbters: " + count);
//other statements might throw an exception
out.close(); //may not executetry (PrintWriter out = new PrintWriter(new File("output.txt"), "UTF-8"))
{
//other statements might throw an exception
out.println("Number of even numbters: " + count);
//other statements might throw an exception
out.close();
}
Record your participation in Piazza clicker question Lesson21 Q5
Now in your ReadFile.java class
In your own classes
public class InvalidDepositException extends RuntimeException
{
public InvalidDepositException()
{
super("The deposit amount is invalid");
}
public InvalidDepositException(String message)
{
super(message);
}
} public void deposit(double amount)
{
if (amount < 0)
{
throw new InvalidDepositException("Deposit amount can not be negative: " + amount);
}
}