Algorithms and OOD (CSC 207 2014F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [Learning Outcomes] [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Student-Curated Resources] [Java 8 API] [Java 8 Tutorials] [Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2014S (Rebelsky)] [CSC 207 2014F (Walker)] [CSC 207 2011S (Weinman)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Summary: In this laboratory, you will begin working with exceptions in Java.
Fork and clone the repository at
https://github.com/Grinnell-CSC207/lab-exceptions/.
(You don't really need to fork the repo, but if you don't you can't
save your work.)
Start Eclipse.
Import the repository into Eclipse.
Quickly skim the code to see what the purpose is.
Run ReadIntExpt1.java to verify that it works as you
might expect (except for very large positive or negative numbers).
Early in the main method for
ReadIntExpt1.java, you should see a line that
reads as follows.
PrintWriter pen = new PrintWriter(System.out, true);
PrintWriter provides a constructor that doesn't require
the boolean. Find out what happens if you just use
PrintWriter pen = new PrintWriter(System.out);
Then explain the difference in output (if you observe one).
a. Remove the throws Exception warning from the
main method and determine what, if any, error messages
you get. If Eclipse permits you to do so, try running the program.
After you finish exploring the effects of that removal, reinsert the
throws Exception warning.
b. Remove the throws Exception from the definition of
readInt and determine what, if any, error messages you
get. If Eclipse permits you to do so, try running the program with
that warning removed.
Do not reinsert the warning.
c. Within readInt, you should have a sequence of lines
(or perhaps a single line) that does something like the following:
String response = br.readLine(); return Integer.parseInt(response);
Enclose those lines in a try/catch clause that returns 0 if an exception is thrown, as in the following code.
try
{
String response = br.readLine();
return Integer.parseInt(response);
} // try to read and parse a string
catch (Exception e)
{
return 0;
} // Reading/parsing fails.
Verify that Eclipse is no longer concerned about errors in your code.
Determine what happens when the user enters an invalid value
(such as Hello) in response to a request for an integer.
To the MathUtils class, add a static
smallQuadraticRoot(double a, double b, double c)
method that computes the smaller of the two roots of a quadratic
expression.
Note that you can use the following formula to compute that root:
(-b - sqrt(b2 - 4ac))/2a
You will, of course, have to translate that mathematical expression into Java code.
b. Write a few simple unit tests for that procedure. Note that you are likely to find it easier of you choose quadratics for which you know the solution.
a. Write a new main class, QR, which
b. Use your class to compute a root of x2-x-2. (The roots of that quadratic are 2 and -1.)
a. Determine what happens if the user enters 0 for the coefficient of x2.
b. Determine what happens if user enters values for which there is no real root.
c. Determine what happens if the user enters values for which the function has only one root (e.g., x2-2x+1 has only one root)?
a. Extend smallerQuadraticRoot to indicate that it may
throw an exception. Note that you'll need to change the method signature
for smallerQuadraticRoot to something like the following
public static double smallerQuadraticRoot(double a, double b, double c)
throws Exception
b. Can you successfully compile your modified code? If not, make any changes necessary to permit you to compile it.
c. Can you successfully compile QR? If not, get help from
a mentor or faculty member.
d. What now happens if you enter the “erroneous” input described in the previous exercise?
a. Extend 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.");
} // if (a == 0)
b. Extend 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.
c. What now happens if you enter the “erroneous” input described above?
If you've written your main method using the recommended
template, you have the line throws Exception in
the head of that method. Remove that line.
a. What effect do you expect removing that line will have?
b. Verify your answer experimentally. Ask a mentor or teacher if you don't understand the results of your experiment.
c. Enclose your call to smallerQuadraticRoot in a
try/catch block. For example,
try
{
double root = f.smallerQuadraticRoot(a,b,c);
pen.println("The smaller root of the polynomial is: " + root);
pen.println("Experimentally, " + a + "*" + root + "*" + root
+ "+" + b + "*" + root + "+" + c + " = "
+ (a*root*root + b*root + c));
} // try to compute and print a root
catch (Exception e)
{
pen.println("Sorry, I could not compute a root.");
} // catch (Exception)
d. Determine what happens with the problematic inputs described above.
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 strategy described in the reading.
a. Create and compile a Java file for DivideByZeroException.
b. Verify that the previously-modified code now works.
a. Extend QR so that it has a catch clause for your
new DivideByZeroException before
the catch clause for the generic Exception. For example,
try
{
...
} // try
catch (DivideByZeroException dbze)
{
pen.println("Cannot compute a result because the coefficient of the quadratic term is 0.");
} // catch (DivideByZeroException)
catch (Exception e)
{
...
} // catch (Exception)
b. Determine what happens in each of the problematic cases.
c. What do your results for this problem suggest?