C# Programming: Basic Concept Part 2 Computer and Programming (204111)

Slides:



Advertisements
Similar presentations
2 nd Semester Selection Statement Computer and Programming (204111)
Advertisements

Computer Programming w/ Eng. Applications
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
True or false A variable of type char can hold the value 301. ( F )
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Basic Elements of C++ Chapter 2.
Thanachat Thanomkulabut
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Reading and Writing to the Console Svetlin Nakov Telerik Corporation
Introduction to C# Programming ณภัทร สักทอง Application Program Development.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
2440: 211 Interactive Web Programming Expressions & Operators.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
CSC Programming I Lecture 5 August 30, 2002.
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Basic Elements of C++ Chapter 1.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
1 Operations Making Things Happen (Chap. 3) Expressions.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Input & Output Statement
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
GUJARAT KNOWLEDGE VILLAGE Faculty Name:- Prof H.M.Patel Students Name:- SONI VISHAL THAKKER BHAVIN ZALA JAYDIP ZALA.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Input/Output and Mathematical Functions Computer and Programming Department of Computer Engineering Kasetsart University Cliparts are taken from.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
CompSci 230 S Programming Techniques
Arithmetic Expressions
Chapter Topics The Basics of a C++ Program Data Types
Using the Console.
Computing and Statistical Data Analysis Lecture 2
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Primitive and Reference Data Values
Computing with C# and the .NET Framework
Chapter 3: Understanding C# Language Fundamentals
Basic Elements of C++ Chapter 2.
OPERATORS (2) CSC 111.
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
C# Programming.
Expressions and Assignment
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Presentation transcript:

C# Programming: Basic Concept Part 2 Computer and Programming (204111)

Reminders account account Group arrangement Group arrangement Pair up with another person Pair up with another person

Review Some of important basic rules: Some of important basic rules: C# syntax is case-sensitive C# syntax is case-sensitive Every statement ends with a semicolon (;) Every statement ends with a semicolon (;) Program structure; namespaces, class and methods Program structure; namespaces, class and methods method1 method2 Namespace Class

Review (Cont’) Variables: declaration and assignment Variables: declaration and assignment Data Types: for example; int (integer), char (character), bool (Boolean) Data Types: for example; int (integer), char (character), bool (Boolean) Arithmetic expression: Arithmetic expression: +, -, *, /, % +, -, *, /, % Precedence rules: ( )  * / %  + -  left to right Precedence rules: ( )  * / %  + -  left to right int width, height; int area; width = 10; height = 20; area = width * height; int width, height; int area; width = 10; height = 20; area = width * height;

Lab Review Statements Statements Output Statement Output Statement System.Console.WriteLine() is used to write a string to the screen and then start a new line System.Console.WriteLine() is used to write a string to the screen and then start a new line System.Console.Write() is the same as WriteLine(), but it does not start a new line System.Console.Write() is the same as WriteLine(), but it does not start a new line int area=10; System.Console.WriteLine(“Hello”); System.Console.WriteLine(area); int area=10; System.Console.WriteLine(“Hello”); System.Console.WriteLine(area); System.Console.WriteLine(string);

Lab Review (Cont’) Consider the program to calculate the size of an area (20 x 10) Consider the program to calculate the size of an area (20 x 10) If different size, modify what? If different size, modify what? It’s not efficient to modify the program every time It’s not efficient to modify the program every time namespace Lect31 { class Lect31class { static void Main () { int width, height, area; width = 20; height = 10; System.Console.WriteLine(“Rectangle: H = {0}, W = {1}“, height, width); area = width*height; System.Console.WriteLine(“The rectangle area is {0}.”, area); } namespace Lect31 { class Lect31class { static void Main () { int width, height, area; width = 20; height = 10; System.Console.WriteLine(“Rectangle: H = {0}, W = {1}“, height, width); area = width*height; System.Console.WriteLine(“The rectangle area is {0}.”, area); }

Outline "using" keyword "using" keyword Input statements Input statements More on arithmetic operations More on arithmetic operations Type conversion Type conversion Calling methods Calling methods Math class Math class

" using " Keyword Write using statement at the beginning of a program Write using statement at the beginning of a program indicates that we are willing to refer to classes inside that namespace indicates that we are willing to refer to classes inside that namespace using System; class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } using System; class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } class Hello { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } class Hello { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); }

Input Statement The Console class has a method ReadLine which The Console class has a method ReadLine which Reads keyboard input until Enter is pressed Reads keyboard input until Enter is pressed Returns the whole input as a string Returns the whole input as a string Example: Example: string yourname; yourname = System.Console.ReadLine(); string yourname; yourname = System.Console.ReadLine();

Complete Example: ReadLine using System; class HelloYou { static void Main() static void Main() { string name; string name; Console.Write("What is your name: "); Console.Write("What is your name: "); name = Console.ReadLine(); name = Console.ReadLine(); Console.WriteLine("Hello, {0}", name); Console.WriteLine("Hello, {0}", name); }} using System; class HelloYou { static void Main() static void Main() { string name; string name; Console.Write("What is your name: "); Console.Write("What is your name: "); name = Console.ReadLine(); name = Console.ReadLine(); Console.WriteLine("Hello, {0}", name); Console.WriteLine("Hello, {0}", name); }} This program will ask the user for his/her name and say Hello This program will ask the user for his/her name and say Hello

String to Number Conversion As we have seen, Console.ReadLine returns a string As we have seen, Console.ReadLine returns a string Unfortunately, strings cannot be processed as numerical values (at least directly) Unfortunately, strings cannot be processed as numerical values (at least directly) What if we need a number input from user? What if we need a number input from user? Each numeric type has a Parse method which can be used to convert a string to number of that type Each numeric type has a Parse method which can be used to convert a string to number of that type string s = "12"; int x = s+1; // WRONG!!! string s = "12"; int x = s+1; // WRONG!!! string s = Console.ReadLine(); int x = int.Parse(s); double y = double.Parse(s); string s = Console.ReadLine(); int x = int.Parse(s); double y = double.Parse(s);

Example: Calculating Area using System; class AreaCalculation { static void Main() static void Main() { int width, height, area; int width, height, area; string input; string input; Console.Write("Enter width: "); Console.Write("Enter width: "); input = Console.ReadLine(); input = Console.ReadLine(); width = int.Parse(input); width = int.Parse(input); Console.Write("Enter height: "); Console.Write("Enter height: "); input = Console.ReadLine(); input = Console.ReadLine(); height = int.Parse(input); height = int.Parse(input); area = width * height; area = width * height; Console.WriteLine("Area = {0}x{1} = {2}", width, height, area); Console.WriteLine("Area = {0}x{1} = {2}", width, height, area); }} using System; class AreaCalculation { static void Main() static void Main() { int width, height, area; int width, height, area; string input; string input; Console.Write("Enter width: "); Console.Write("Enter width: "); input = Console.ReadLine(); input = Console.ReadLine(); width = int.Parse(input); width = int.Parse(input); Console.Write("Enter height: "); Console.Write("Enter height: "); input = Console.ReadLine(); input = Console.ReadLine(); height = int.Parse(input); height = int.Parse(input); area = width * height; area = width * height; Console.WriteLine("Area = {0}x{1} = {2}", width, height, area); Console.WriteLine("Area = {0}x{1} = {2}", width, height, area); }} Obtaining "width" Obtaining "height" area = width x height height width

Numeric Type Conversion Conversion from a smaller size variable to a larger size can be done implicitly Conversion from a smaller size variable to a larger size can be done implicitly (i.e., no loss of information possible) (i.e., no loss of information possible) If loss of information is possible, explicit type casting is required If loss of information is possible, explicit type casting is required doublefloatlongintshortsbyte ulonguintushortbyte char LargerSmaller int i = 50; double d = i; // OK int i = 50; double d = i; // OK double d = ; long x = d; // NOT OK long x = (long) d; // OK double d = ; long x = d; // NOT OK long x = (long) d; // OK

Modify-And-Assign Operations Arithmetic operators can be combined with to produce a modify-and-assign operation Arithmetic operators can be combined with "=" to produce a modify-and-assign operation Examples Examples StatementDescription var += expression Increment var by the value of expression var -= expression Decrement var by the value of expression var *= expression Multiply var by the value of expression, then store the result in var var /= expression Divide var by the value of expression, then store the result in var sum += x; // is equivalent to sum = sum + x prod *= 2.5; // is equivalent to prod = prod * 2.5 y -= 3+a; // is equivalent to y = y – (3+a)

Incrementing Operators Operators ++ and – can be used to increment and decrement a variable's value by 1, respectively Operators ++ and – can be used to increment and decrement a variable's value by 1, respectively Example: Example:StatementDescription x++ Add 1 to the variable x x-- Subtract 1 to the variable x int n = 0; n++; // is equivalent to n = n+1, or n += 1 n++; // n is now 2 n--; // n is now 1 int n = 0; n++; // is equivalent to n = n+1, or n += 1 n++; // n is now 2 n--; // n is now 1

Calling Methods Some classes provide static methods that can be called with the class name and method name Some classes provide static methods that can be called with the class name and method name General Form – calling the method inside the class which is inside the namespace General Form – calling the method "methodName" inside the class "className" which is inside the namespace "namespaceName" If the using statement is already used with "namespaceName", then the namespace part can be omitted If the using statement is already used with "namespaceName", then the namespace part can be omitted Well-known examples are and Well-known examples are System.Console.WriteLine() and System.Console.ReadLine() using namespaceName; : className. methodName ( optional-arguments ) namespaceName.className. methodName ( optional-arguments )

The Math Class The Math class inside the System namespace provides a large collection of math methods and constants, for example, The Math class inside the System namespace provides a large collection of math methods and constants, for example, Method/ Constant Value returned Example Call Result PI Value of Math.PI Max(x,y) Larger of the twoMath.Max(1,2)2 Abs(x) Absolute value of xMath.Abs(-1.3)1.3 Sqrt(x) Square-root of xMath.Sqrt(4.0)2.0 Round(x) Nearest integer to xMath.Round(0.8)1 Pow(x,y) xyxyMath.Pow(3,2)9.0 Log(x) Natural log of xMath.Log(10) Ceiling(x) Smallest integer greater than or equal to xMath.Ceiling(4.1)5 Cos(x) Cosine of x radiansMath.Cos(Math.PI)