\documentclass[11pt]{article}

\newif\ifpdf
\ifx\pdfoutput\undefined
\pdffalse % we are not running PDFLaTeX
\else
\pdfoutput=1 % we are running PDFLaTeX
\pdftrue
\fi

\ifpdf
\usepackage[pdftex]{graphicx}
\else
\usepackage{graphicx}
\fi

\textwidth = 6.5 in
\textheight = 9 in
\oddsidemargin = 0.0 in
\evensidemargin = 0.0 in
\topmargin = 0.0 in
\headheight = 0.0 in
\headsep = 0.0 in
\parskip = 0.2in
\parindent = 0.0in

\title{Lab 9:  Exceptions, Cloning, Copying, and Equality}
\author{Dale Vaillancourt}
\begin{document}

\ifpdf
\DeclareGraphicsExtensions{.pdf, .jpg, .tif}
\else
\DeclareGraphicsExtensions{.eps, .jpg}
\fi

\maketitle

\section{Exceptions}
What is the value of \verb|3 / 0|?  What happens when you ask Java to compute it for you?  Find out by running the provided \verb|DivideByZero()| test.  See that error message printed in the console?  The JPT designers thought ahead about what happens when errors such as these happen, and they catch them in order to give you useful feedback.  It's important to know how to catch these errors in order to make your applications handle failure in an appropriate manner.

We're going to learn the basics in just a moment, but first here's some vocabulary that every respectable computer scientist and software engineer should be familiar with:
\begin{itemize}
\item Exception - a type of value that indicates an error condition.  For example, division by zero causes an ArithmeticException.
\item Throw an exception - to signal programmatically that an error condition is present. (Some languages use the keyword \verb|raise| instead of \verb|throw|.)
\item Catch an exception - to trap a thrown exception and perform some appropriate action.  (Some languages use \verb|handle| instead of \verb|catch|.)
\end{itemize}

Okay, let's get to it.  In Java, exception handling looks like this:
\begin{verbatim}... some program text ...
try {
 ... statements that might cause errors go in here...
 } catch (Exception e) {
 ... statements that handle the Exception go here ...
 }
 ... more program text ...
 \end{verbatim}
Let's build a concrete example.
\begin{enumerate}
\item Open up \verb|TestSuite.java|.
\item Find the \verb|DivideByZero()| method.
\item Place the the offending statement inside a \verb|try|-block.
\item Write a \verb|catch|-block which prints a helpful error message to the console.
\end{enumerate}

Note that by using \verb|Exception e| in our \verb|catch|-block, we state that we will handle any kind of error.  This is analogous to saying \verb|Object x = new ...;|.  \verb|x| can hold any kind of \verb|Object|, and we don't really care what kind it is.  In future courses you'll learn all about the different kinds of exceptions, and you'll be able to set up \verb|catch|-blocks to handle certain kinds of exceptions and ignore others.

As a final note, uncaught exceptions will cause your program to crash.  Remember the last time you stayed up late trying to finish that paper, and your XYZ brand whiz-bang word processor crashed before you could save (or worse, while you were trying to save)?  That's an uncaught exception.  Some programmer said to him- or herself ``Nah, that'll never happen.'' and ignored a possible error condition.  Needless to say, there is a lot of bad karma associated with such decisions.

\section{Cloning}
You've got an object, and you'd like to perform some computation on it.  Unfortunately that computation involves destructive updates, and you want to keep the original object around when you're done with the computation.  In this situation you need to make a clone of your object before you run the destructive computation on it.  Say I've got a couple of objects:

\verb|Balloon b1 = new Balloon(10, 5, 10, Color.blue);|\\
\verb|Balloon b2 = b1;|

And I then want to change \verb|b2|'s color:

\verb|b2.color = Color.red;|

The problem is that \verb|b1| also sees this change; \verb|b2| does not contain a copy (or a {\it clone}) of \verb|b1|, it actually references the same object.  In order to clone objects, your class must implement the interface \verb|Cloneable|.  This interface requires that your class implement one method:  \verb|public Object clone();|.  Study the \verb|clone()| method in the \verb|Balloon| class; the process of writing the method is entirely mechanical.  It makes a copy of each field and creates a new object with the copies.

To do:
\begin{enumerate}
\item Try the \verb|TestBalloonView()| test.  Try moving each of the Balloons.  What's happening with the red one?
\item Look at the \verb|TestBalloonView()| code.  Fix it to actually create two separate copies of the red balloon using the \verb|clone| method.
\end{enumerate}


\section{Equality}
Sometimes you've got two distinct objects, and you'd like to know if they are equal.  That is to say, you want to know if the two objects you've got represent the same piece of information.  For example, say I've defined these three \verb|String|s:

\verb|String s1 = "Viera";|\\
\verb|String s2 = "John";|\\
\verb|String s3 = "Viera";|

Obviously \verb|s1| and \verb|s3| are equal (even though they are two distinct objects), \verb|s1| and \verb|s2| are not equal, and \verb|s2| and \verb|s3| are not equal.  Wouldn't it be nice to be able to determine this programmatically?  Java's \verb|String| class allows  you to compare two \verb|String|s for equality with the \verb|equals| method.  For example:

\verb|s1.equals(s3);| evaluates to \verb|true|\\
\verb|s1.equals(s2);| evaluates to \verb|false|

In fact, Java's class \verb|Object| (which {\it every} class implicitly extends) provides a default \verb|equals| method whose signature looks like this:  \verb|boolean equals(Object o);|, but its default implementation does not do what you want!  The default implementation will only return \verb|true| when this object and the given object are actually the {\it same object}.  The \verb|String| class is able to compare two different objects and tell you the Right Thing (TM) because it provides a smarter implementation of the \verb|equals| method.  To compare \verb|String|s for equality, you just compare them character by character.  If they all match, the two \verb|String|s are equal.  Let's use this idea to compare Balloons for equality!

Your job is to implement \verb|equals| for Balloons:
\begin{enumerate}
\item Run \verb|TestBalloonEqual()| and look at the output.  Notice what's going on in the third test case.  If you don't understand what's going on in this case, ask a TA or a tutor!
\item Open the file \verb|Balloon.java|
\item Define the \verb|equals| method with the signature given above.
\item Use \verb|TestBalloonEqual()| to test your code.  The output in the third case should make sense now.
\end{enumerate}

 \end{document}
 \end
