Lab 01
Lab 01
Section 450
Lab #1 C# Basic
Student ID Sheets Owner Group partner Name Signature
static
Exercise 1.1: Consider the following names and check if each of them is a valid name for a C# identier Identier Name XXX $$$ Y string ij Student ID HelloWorld!! first-time null 204111Class Section3 sECTION3 w*h do Valid in C#? yes no Reason not violating the naming rules consists of special characters $
Lab #1
1 of 8
Section 450
From the above, the positions (A), (B), and (C) have the following meanings: (A) is for the namespaces name (B) is for the classs name (C) is for statements telling the computer what to do For simple programs, the namespace part can even be omitted as follows.
Write another program that can also compile and run, but now it must meet these requirements: There is no namespace class has the name SayHello The program outputs a phrase Hello Section 3
Lab #1
2 of 8
Section 450
3. Data Types in C#
C# provides a number of data types for storing dierent kinds of data. Some commonly used types are listed in the table below. Type
char bool byte int uint long ulong float double string
Description Single character Truth value true or false Unsigned integer between 0 and 255 Signed integer between -2,147,483,648 and 2,147,483,647 Unsigned integer between 0 and 4,294,967,295 Signed integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 Unsigned integer between 0 and 18446744073709551615 Real number Double-precision real number Sequence of characters
uint myvar ;
Example 4.2: Declare a variable salary to be of type long with an initial value 30000
"Peter"
Exercise 4.2: Declare a variable myName to be of type string with an initial value
Lab #1
3 of 8
Section 450
A constant is an identier used to represent data that can be used throughout the program. Unlike variables, the value of a constant is xed and cannot be changed. Constants are useful when we want to use the same value over and over again, but we dont want to write out that value every time it is referred to. In C#, a constant must be declared with a specic type, an initial value, and a keyword const. DataType constantName = value ; where constantName is the name of the constant, DataType indicates the type of data the constant represents, and value species the value of the constant. Example 4.3: Declare a constant myconst to be of type double and have a xed value of 2.71828
3.1415926535
Example 4.4: More examples of variable and constant declaration. Note that multiple variables (or constants) of the same type can be declared at the same time by separating them with commas.
const int FreezingPoint = 32; int x , y ; int wd = 5 , ht = 8; const double PI = 3.1424; char ch = A ; string mynote = " Hello , Kitty " ; int j = 5;
Exercise 4.4: Create a new le in SharpDevelop and type the following source code
using System ; class CircleArea { static void Main () { const double PI = 3.1415926535;
radius = 12.5; area = PI * radius * radius ; Console . WriteLine ( " Circle area = {0} " , area ); } }
Lab #1
4 of 8
Section 450
Then try to compile the program. What errors are reported, and at which lines? What do you think is the cause?
Fix the above code so that it compiles and runs. How did you x the errors?
Exercise 4.5: Create a new le in SharpDevelop and type the following source code
using System ; class MoreExercise { static void Main () { int f = 1.5; char s = " Harry Potter " ; char c = A ;
Console . WriteLine ( " f value = {0} , s = {1} , c = {2} " , f , s , c ); } } Fix the above code by changing only data types in the variable declarations, so that it compiles and runs. How did you x the errors?
Lab #1
5 of 8
Section 450
Exercise 4.6: For each of the data item given below, choose an appropriate data type and write a C# statement to declare the specied variable Variable myAge to store your own age
5. Arithmetic Expressions
An expression is a single term or a combination of terms that can be evaluated to another value. Below are examples of single-term expressions: A number, such as 3000, 1.414 A string literal, such as "Hello, World" A boolean keyword, which can be true and false A single variable or constant, such as myName, salary In this lesson, we will mainly focus on those expressions that evaluate to numerical values. These expressions are called arithmetic expressions. C# provides a collection of arithmetic operators that allow us to build a more complex expressions from simpler ones. Arithmetic operators in C# are as follows:
Lab #1
6 of 8
Section 450
Symbol + * / % ()
When more than one arithmatic operators are used within the same expression, C# evaluates the expression in specic order, according to the following precedence rules: 1. ( ) 2. *, / and % 3. + and 4. For equal precedence, evaluate from left to right Exercise 5.1: Create a new le in SharpDevelop and type the following source code 1 using System ; 2 class Test { 3 static void Main () { 4 double x = 3.0 , y = 2.0; 5 int a = 10 , b = 2; 6 double tmp = a + b + x + y ; 7 Console . WriteLine ( tmp ); 8 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ 9 Console . ReadLine (); 10 } 11 } Notice that line 6 is blank. Now ll the blank with each of the statements listed in the table below, one at a time. Run the program and put the last result in the right column. Statement Console.WriteLine(a); Console.WriteLine(x+a); Console.WriteLine(a/b); Console.WriteLine(y/x); Console.WriteLine(y%x); Console.WriteLine((a+b)/b%a); Console.WriteLine(9.0/5.0*(a-x)); Console.WriteLine(x+y-x*y%x); Console.WriteLine(57%50/25); Result 10
Lab #1
7 of 8
Section 450
6. Output Statements
Exercise 6.1: (Basic usage of Console.Write and Console.WriteLine) Write a C# program that outputs the following result:
Live as if you were to die tomorrow. Learn as if you were to live forever. -Mahatma Gandhi Then copy the code you wrote in SharpDevelopinto the box below.
Exercise 6.2: (Advanced usage of Console.Write and Console.WriteLine) Consider the following incomplete code: 1 using System ; 2 3 class SayHi { 4 static void Main () { 5 string yourName = ___ ( a ) ___ ; 6 uint yourAge = ___ ( b ) ___ ; 7 Console . WriteLine ( " Hello {1}. 8 ___ ( c ) ___ , ___ ( d ) ___ ); 9 } 10 }
Fill in the blanks (1),..., (4) so that the program will say hello to you and print out your own age. For example, if your name is Arthur and you are 18 years old, the result should be: Hello Arthur. You are 18 years old.
Now copy what you lled in the blanks into this table. Position __(a)__ __(b)__ __(c)__ __(d)__ Your answer
Lab #1
8 of 8