|
CIS162AD -- C Sharp (C#) -- Section 5832
Professor: Dave Hammer dwhammer@cox.net http://members.cox.net/dwhammer/ Student: Patrick Moss patmoss@patmoss.com http://www.patmoss.com/cis162ad/ Worksheet #8 Due Thursday, May 4, 2006
I. Definitions -- Please explain the following terms:
1. abstract method (p. 795)
A method that includes no implementation details (no method
body) in the declaration. Abstract method declarations are
only permitted in abstract classes.
2. single inheritance (p. 586)
All .NET languages only support single inheritance, which
means that a class can extend or derive from at most one
class. One way that languages such as C# and Java work
around this limitation is by implementing multiple
interfaces.
C++ permits multiple inheritances. A C++ class can extend
from more than one base class. This is not possible in C#,
Java, J#, or any of the Microsoft managed .NET languages.
3. exception (p. 799)
An unexpected condition in a computer program that occurs
infrequently and is usually associated with error conditions
that cause abnormal terminations if they are not handled.
4. stack trace (p. 806)
A listing of all the methods that are in an execution chain
when an exception is thrown.
This phrase derives from the fact that, when one method
calls (invokes) another method, the return address of the
calling method is "pushed" onto the "stack". Then when the
called method completes execution, the stack is "popped"
in order to return execution back to the "next instruction"
(i.e., to the return address) in the calling method.
The actual parameters values for each call are usually
stored on the stack, along with the return address.
So, by examining the "stack" at any point in time, it is
possible to view/determine the one (or more) methods that
have currently called other methods, and the order of those
calls, the corresponding actual parameter values, etc.
This procedure is especially revealing when methods are
called "recursively", but without proper termination, and
then a "stack overflow" occurs. Then a stack trace can
be very useful in analyzing and "debugging" the problem.
Recursive calls are used heavily in "language translation",
such as with computer language compiler software, which
"parse" source statements and build a "parse tree" that
represents the "meaning" of the source statements. For
example, see Chomsky's theory of language translation,
for Type 1 Context Sensitive Grammars (CSG), and Type 2
Context Free Grammars (CFG), such as at
www.patmoss.com/pcref/chomsky.htm.
5. finally block (p. 628-629)
C#, like C++ and Java, handles exceptions through try...
catch...finally blocks. Program code that may cause an
exception is placed inside the try block. The code to
deal with the problem (the exception handler) is placed
in the catch block(s). And the code that we want to be
executed whether or not an exception is thrown is placed
in the finally block.
6. exception handler (p. 799)
A block of code that is executed when an exception occurs.
where:
exception (p. 799)
See item 3. above.
II. Questions or short essay
1. What is the syntax of the try...catch block? (p. 628)
The C# try...catch...finally syntax is:
try
{
// Statements
}
catch
{
// Exception handler statements
}
finally
{
// Statements
}
2. If you have multiple catch blocks, why is the order
that you place them important? (p. 636-639)
Multiple catch blocks are executed in the order in
which they are placed in the source code. Therefore,
they should be placed from the most specific to the
most generic.
Because all exception classes derive from the Exception
class, if you are including the Exception class, it
should always be placed last.
Because the DivideByZeroException exception derives
from the ArithmeticException class, if both are
included, DivideByZeroException should be placed
before ArithmeticException.
3. What information does the message property of an
exception give you? (p. 632)
The message property returns a string describing
the exception. Since it is a property of the base
class, it can be used with any exception object.
The message property can be used inside a catch clause
to display a message describing the exception.
The object identifier e is used to name an object
of that class. The object e may now be used with any
of the properties or methods of the Exception class.
III. Programming Exercise
Create the following programs:
1. Create a Form that contains three Labels that hold famous
quotes of your choice. When the program starts, the
background color of the Form and each Label should be
black. When the user passes a mouse over a Label, change
its BackColor to white, revealing the text of the quote.
2. Design and implement an application that contains GUI
components and calculates an investment value. It should
have text boxes that allow the user to enter the amount
invested, the number of years for the investment, and the
annual interest rate. When the user presses the button,
it will accept the inputs and calculate the total value
of the investment after the specified number of years.
The answer should be written to a label.
Turn in your source code for these programs.
|