Computers and Programming Section 450. Lab #1 C# Basic. Student ID Name Signature

Similar documents
BASIC ELEMENTS OF A COMPUTER PROGRAM

Introduction to C# Applications

Lab #10 Multi-dimensional Arrays

Chapter 2: Using Data

6.096 Introduction to C++ January (IAP) 2009

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CS242 COMPUTER PROGRAMMING

Visual C# Instructor s Manual Table of Contents

3. Java - Language Constructs I

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

UNIT- 3 Introduction to C++

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Object Oriented Programming with Visual Basic.Net

QUIZ: What value is stored in a after this

Fundamentals of Programming CS-110. Lecture 2

A First Program - Greeting.cpp

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Differentiate Between Keywords and Identifiers

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

2 nd Week Lecture Notes

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Fundamentals of Programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Computer Programming : C++

Chapter 2: Basic Elements of C++

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Introduction to Programming EC-105. Lecture 2

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

The C++ Language. Arizona State University 1

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS313D: ADVANCED PROGRAMMING LANGUAGE

C++ Programming: From Problem Analysis to Program Design, Third Edition

COMP 202 Java in one week

Creating a C++ Program

Fundamental of Programming (C)

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Lecture 2 Tao Wang 1

Datatypes, Variables, and Operations

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

3. Java - Language Constructs I

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

IT 374 C# and Applications/ IT695 C# Data Structures

LECTURE 3 C++ Basics Part 2

Advanced Computer Programming

Objectives. In this chapter, you will:

Java Notes. 10th ICSE. Saravanan Ganesh

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Programming and Data Structures

Basics of Java Programming

Getting started with Java

ISRA University Faculty of IT. Textbook BARBARA DOYLE C# Programming: From Problem Analysis to Program Design 4 th Edition

DEPARTMENT OF MATHS, MJ COLLEGE

.Net Technologies. Components of.net Framework

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

REVIEW. The C++ Programming Language. CS 151 Review #2

Lab # 02. Basic Elements of C++ _ Part1

CSc Introduction to Computing

CSCE 110 PROGRAMMING FUNDAMENTALS

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Introduction to the C++ Programming Language

Computer Components. Software{ User Programs. Operating System. Hardware

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

3. Simple Types, Variables, and Constants

C# Fundamentals. built in data types. built in data types. C# is a strongly typed language

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Introduction to Programming Using Java (98-388)

CHAPTER 3 BASIC INSTRUCTION OF C++

Computational Expression

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Pointers, Dynamic Data, and Reference Types

ARG! Language Reference Manual

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

CS Programming I: Primitives and Expressions

Structured programming

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Integer Data Types. Data Type. Data Types. int, short int, long int

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

DigiPen Institute of Technology

C# INTRODUCTION. Object. Car. data. red. moves. action C# PROGRAMMERS GUIDE LESSON 1. CsGuideL1.doc. Date Started: May 8,2001

Lecture 3 Tao Wang 1

3 The Building Blocks: Data Types, Literals, and Variables

Reserved Words and Identifiers

LECTURE 02 INTRODUCTION TO C++

Variables. Data Types.

Basics of Programming

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Week 3 Lecture 2. Types Constants and Variables

Transcription:

Lab #1 C# Basic Sheet s Owner Student ID Name Signature Group partner 1. Identifier Naming Rules in C# A name must consist of only letters (A Z,a z), digits (0 9), or underscores ( ) The first character must be either a letter or an underscore A name can be at most 63 characters in length A name must not be identical to a reserved word such as class, namespace, int, void, static Exercise 1.1: Consider the following names and check if each of them is a valid name for a C# identifier Identifier Name Valid in C#? Reason XXX yes not violating the naming rules $$$ no consists of special characters $ Y string i j Student ID HelloWorld!! first-time null 204111Class Section3 section3 w*h do 2. Basic Structure of C# Programs Here is an example of C# program structure. namespace (A) { class (B) { static void Main () { (C) Lab #1 1 of 8

From the above, the positions (A), (B), and (C) have the following meanings: (A) is for the namespace s name (B) is for the class s name (C) is for statements telling the computer what to do For simple programs, the namespace part can even be omitted as follows. class (B) { static void Main () { (C) Exercise 2.1: Complete the following tasks: Write a program that can compile and run. Your program must meet these requirements: namespace has the name TestingNamespace class has the name TestingClass The program outputs no results 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

3. Data Types in C# C# provides a number of data types for storing different kinds of data. Some commonly used types are listed in the table below. Type char bool Description Single character Truth value true or false byte Unsigned integer between 0 and 255 int Signed integer between -2,147,483,648 and 2,147,483,647 uint Unsigned integer between 0 and 4,294,967,295 long Signed integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 ulong Unsigned integer between 0 and 18446744073709551615 float Real number double Double-precision real number string Sequence of characters 4. Variables and Constants A variable is an identifier used to represent data that can be changed while the program is running. In C#, a variable must be declared with a specific type as follows: DataType variablename ; where variablename is the name of the variable, and DataType specifies the type of data the variable can store. In addition, a variable can be declared along with its initial value. For this case, the declaration is of the form: DataType variablename = initialvalue ; where initialvalue indicates the initial value of the variable variablename. Example 4.1: Declare a variable distance to be of type uint without an initial value uint myvar ; Example 4.2: Declare a variable salary to be of type long with an initial value 30000 long myvar = 30000; Exercise 4.1: Declare a variable x to be of type float without an initial value Exercise 4.2: Declare a variable myname to be of type string with an initial value "Peter" Lab #1 3 of 8

A constant is an identifier used to represent data that can be used throughout the program. Unlike variables, the value of a constant is fixed and cannot be changed. Constants are useful when we want to use the same value over and over again, but we don t want to write out that value every time it is referred to. In C#, a constant must be declared with a specific 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 specifies the value of the constant. Example 4.3: Declare a constant myconst to be of type double and have a fixed value of 2.71828 const double myconst = 2.71828; Exercise 4.3: Declare a constant PI to be of type double and have a fixed value of 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 file 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

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 fix the errors? Exercise 4.5: Create a new file 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 fix the errors? Lab #1 5 of 8

Exercise 4.6: For each of the data item given below, choose an appropriate data type and write a C# statement to declare the specified variable Variable myage to store your own age Variable income to keep track of Peter s personal income Variable temp c to store temperature in degree Celcius Variable temp k to store temperature in Kelvin Variable name to store Aum s full name 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

Operator Symbol Type Example Value when x = 20 Add + Binary x+4 24 Subtract - Binary 32-x 12 Multiply * Binary x*2 40 Divide / Binary x/2 10 Modulo % Binary x/6 2 Negate - Unary -x -20 Group () N/A (x+2)*3 66 When more than one arithmatic operators are used within the same expression, C# evaluates the expression in specific 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 file 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 fill 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 Result Console.WriteLine(a); 10 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); Lab #1 7 of 8

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. You are {0 years old.", 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 filled in the blanks into this table. Position (a) Your answer (b) (c) (d) Lab #1 8 of 8