|
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 #2 Due Thursday, February 9, 2006
I. Definitions
1. software (p. 806)
Computer programs or instructions that perform a task or
manage functions of a computer.
2. literal (p. 801)
The numbers, characters, and combinations of characters
used in a program. For example, 5 is a numeric literal,
and 'A' is a character literal.
3. object
In OOP, we create classes. Then we instantiate an object
from a class. The class is a template. The object is an
executable implementation of the class.
4. algorithm (p. 795)
A clear unambiguous, step-by-step process for solving a
problem. These steps are expressed completely and precisely
so that all details are included.
5. string (p. 94, 357-361)
The string type represents a series of Unicode characters.
Objects of the string class store an immutable series of
characters. Each character can be accessed by an array index
of 0 to n-1, where the string contains n characters. The
C# language provides some 25 methods and properties for the
string class.
6. assignment statement (p. 796)
For example: x = y + 5;
The value of the expression on the right side is evaluated,
and then that resulting value is assigned to the variable
on the left side of the "=" sign.
7. preincrement operator (p. 101)
There are four forms of the unary increment/decrement operations
as follows:
a. preincrement ++x;
b. predecrement --x;
c. postincrement x++;
d. postdecrement x--;
When placed before an operand, the increment or decrement is
performed before using it in an expression. When placed after
an operand, the increment or decrement is performed after using
it in an expression.
8. const (p. 94 and 797)
When you add the const keyword to a declaration, it becomes a
constant. Then the data item is defined and initialized to keep
the same value throughout the life of the program.
9. operator precedence (p. 106-107, 796)
The order in which the individual operators are evaluated when
an expression contains multiple operators. For example, given
x = a + b / c - d; the division is performed first, and then the
addition is performed second, and the subtraction is performed
last.
10. casting (p. 109, 796)
Casting makes a variable or expression temporarily behave as
if it were a different type.
II. Questions or short essay
1. What are the differences between syntax errors, logic errors,
and runtime errors?
a. Syntax errors: When we write a statement, we must obey all
rules of the language.
b. Logic errors: When we write software statements to implement
an algorithm, the resulting algorithm must produce results
identical with the program specifications.
c. Runtime errors: When we execute the program, we must not
violate the capabilities of the hardware platform. For example,
a "divide by zero" error will exceed the precision of the
ALU, since the hardware cannot represent an "infinite" answer.
2. Why does C# require that you declare variables?
This is an arbitrary decision made by the C# implementators.
In order to create a "strongly typed" language, we must adhere to
this conservative approach. And C# is a strongly typed language.
The two implementation possibilities include:
a. Define and initialize a variable automatically with its first
use. For example, if we write the statement x = 10; then the
compiler could create a default value type variable x, such as
a 32-bit int, and store the integer value 10 into that variable.
b. Always define a variable, and its data type, and optionally,
initialize it, before it is used.
The implementors of C# chose to enforce the strongly typed and
conservative model b.
Note: A variable must be initialized, either explicly by program
code, or implicitly by the compiler or by an object
constructor, before its value is used. For example,
the statement x = y + 10; requires that we initialize
the value type variable y to a known value before the
statement is executed. Failure to do so will result in
a logic error, and probably also a runtime error that
will cause a program exception condition to occur.
3. Why is it necessary to include a Main() method in your program?
An OOP language must have a preexisting object to receive control
when the program begins runtime execution. And the Main() method
provides this capability. As we recall, a Java "applet" is an
apparent exception, but for that case, an executing browser has
already received control via its Main() method (or equivalent).
Ref.: Main() (p. 128)
4. What does the compound operator += do?
A statement of the form x = x + 5; can be written as x += 5;
The latter is just an equivalent "shorthand" notation for the
former. This notation is valid for the operators +, -, *, /,
and %. We can also use this notation when the increment size
is 1, but for this case the unary notation x++; is preferred.
Ref.: increment/decrement (p. 100-106)
5. What is the difference between the data types int and double?
For an int declaration, the compiler creates a 32-bit integer
value type variable, and allocates a 32-bit memory storage area
to hold its value, and generates 32-bit integer instructions
to manipulate this variable.
For a double declaration, the compiler creates a 64-bit floating
point value type variable, and allocates a 64-bit memory storage
area to hold its value, and generates 64-bit floating point
instructions to manipulate this variable.
Ref.: C# data types (p. 89-94)
6. How do you output a value to print in currency format?
Given a numeric value such as x = 12345.678, we can print the
value, rounded to two decimal places, as follows:
Console.WriteLine("The value as currency is: {0:c}", x);
To print the value, to two decimal places, without the leading
dollar sign, we can use the notation:
Console.WriteLine("The value to two places is: {0:F2}", x);
or
Console.WriteLine("The value to two places is: {0:N2}", x);
Note that we can use upper or lower case for the formatting
characters C/c, F/f, and N/n. The "N/n" option also provides
comma insertion so that the value is more readable.
Ref.: Formatting strings (p. 118, 129)
III. Programming Exercise
Write a program that calculates and prints the take-home pay
for a commissioned sales employee. Initialize and store the
name of employee Jessica Oakley in a variable called
employeeName. Jessica received 7% of her total sales. Her
federal tax rate is 18%. She contributes 10% to a retirement
fund and 6% to Social Security. Input the sales for the week.
Produce a formatted report showing the amount for each of the
computed items. Select appropriate constants to use in the
calculations. Turn in your source code for this program.
|