| CSC 207 | Algorithms and Object Oriented Design | Spring 2009 |
Summary: In this lab, you will explore the basics of file input and output in Java, as well as dealing with exceptions.
cd somewhere/CSC207/labs/intro
cp ~weinman/public_html/courses/CSC207/2009S/labs/code/morejava/IntReader.java .
Greet.java. In this file, write (and
test) a program that prompts the user for a name, reads a name
from the keyboard, and prints a greeting to the screen. Your
output should resemble the following:
What is your name? Sam Have a nice day Sam!
DumpInfo that
prints the following three lines to the screen:
DumpInfo so that it instead creates a file and
dumps the information to that file.
DumpInfo.java
write to a file that already exists. (For example, if you run it
a second time.)
DumpInfo.java
to write to a file in a classmate's directory.
ShowInfo that reads a file
of three lines named info and prints each line preceeded
by a category (Name, Email, Color). For example, if the file contains
J. Doe doej@gmail.com plaid
the output on the screen should be
Name: J. Doe Email: doej@gmail.com Fave Color: plaid
info and verify that ShowInfo
continues to work correctly.
ShowInfo
if info contains only two lines (e.g., doesn't
contain a color)?
ShowInfo
if info does not exist?
ShowInfo
using a file in a classmate's directory?
IntReader, which you copied in the preparations
for this lab, has a method, readInt, that reads an
integer, and maintests that method.
throws Exception from
the main method and determine what, if any, error
messages you get. If it compiles, try running the program.
After you finish exploring the effects of that removal, reinsert
the throws warning.
throws Exception from the definition of
readInt and determine what, if any, error messages you
get. If it compiles, try running the program with that warning
removed.
Do not reinsert the warning.
readInt, enclose the two lines in a try/catch
clause that returns 0 if an exception is thrown, as in
try {
String str = br.readLine();
return Integer.parseInt(str);
}
catch (Exception e) {
return 0;
}
Compile to verify that Java is no longer concerned about any errors in your code.
Hello) in response to a request for an integer.
QuadraticRoot.java.
public static double smallQuadraticRoot(double a, double b, double c)
that computes the smaller of the two roots of a quadratic.
Note that you can use the following formula to compute that root:
(-b - sqrt(b2 - 4ac))/2aYou will, of course, have to translate that mathematical expression i into Java code.
testQR that computes the roots for a
variety of quadratics. Note that it is easiest if you choose
quadratics for which you know the solution. For example,
smallerQuadraticRoot to indicate that it may
throw an exception. Note that you'll need to change the method
head for smallerQuadraticRoot to something like the
following
public static double smallerQuadraticRoot(double a, double b, double c)
throws Exception
erroneousinput described in the previous exercise?
smallerQuadraticRoot so that it throws an exception if
a is 0. For example,
if (a == 0) {
throw new Exception("Cannot compute quadratic roots of linear functions.");
}
smallerQuadraticRoot so that it throws an
exception if the root is not real (i.e., if it has an imaginary
component). Note that the root is not real if the thing you're taking
a square root of is negative.
erroneousinput described above?
main method using the template
presented earlier, you have the line throws Exception in
the head of that method. Remove that line.
smallerQuadraticRoot in a try/catch block.
For example,
try {
double root = f.smallerQuadraticRoot(a,b,c);
System.out.println("The smaller root of the polynomial is: " + root);
System.out.println("Experimentally, " + a + "*" + root + "*" + root
+ "+" + b + "*" + root + "+" + c + " = "
+ (a*root*root + b*root + c));
}
catch (Exception e) {
System.err.println("Sorry, I could not compute a root.");
}
a. Update smallerQuadraticRoot so that it tries to throw a
DivideByZeroException if the coefficient of the
quadratic term is 0. You can still throw a generic exception
if the result includes an imaginary component.
b. What do you expect to happen when you try to compile the revised program?
c. Verify your answer experimentally.
As you should have determined in the previous exercise, Java does not know by default what
a DivideByZeroException is. Hence, you'll need to create your
own Exception. You do so using the template for user-defined exceptions.
a. Create and compile a Java file for DivideByZeroException.
That is, begin with the template, replace the package name by
your current package name, replace YourException by
DivideByZeroException, and fill in the default message.
b. Verify that the previously-modified code now works.
a. Extend your code that it has a catch clause for your new
DivideByZeroException before the catch clause for
the generic Exception. For example,
try {
...
}
catch (DivideByZeroException dbze) {
pen.println("Cannot compute a result because the coefficient of the quadratic term is 0.");
}
catch (Exception e) {
...
}
b. Determine what happens in each of the problematic cases.
c. What do your results for this problem suggest?