SlideShare a Scribd company logo
1 of 39
Chapter 2 Getting Started with C#
What is a variable? Variable:  imagine like a box to store one thing (data) Eg: int age;          age = 5;
What is a variable? Variable:  imagine like a box to store one thing (data) Eg: int age;          age = 5; int - Size of variable must be big enough to store Integer
What is a variable? Variable:  imagine like a box to store one thing (data) Eg: int age;          age = 5; int - Size of variable must be big enough to store Integer age - Name of variable age
What is a variable? Variable:  imagine like a box to store one thing (data) Eg: int age;          age = 5; 5 int - Size of variable must be big enough to store Integer age - Name of variable age age = 5 – Put a data 5 into the variable
Value Type Vs Reference Type Variables:  two types Value type (simple type like what you just saw) Only need to store one thing (5, 3.5, true/false, ‘C’ and “string”) Reference type (complex type for objects) Need to store more than one thing (age + height + run() + … )
Reference Type Reference type (complex type for objects) Eg: Human john;          john = new Human(); Compare this to  int age; age = 5;
Reference Type Reference type (complex type for objects) Eg: Human john;          john = new Human(); Human - Size of variable must be big enough to store an Address
john Reference Type Reference type (complex type for objects) Eg: Human john;          john = new Human(); Human - Size of variable must be big enough to store an Address john - Name of variable
Reference Type Reference type (complex type for objects) Eg: Human john;          john = new Human(); Human - Size of variable must be big enough to store an Address D403 john - Name of variable … age height john = new Human() – Get a house with enough space for john (age, height, etc) john
Non-Static Vs Static Class Non-Static: need New() to instantiate / create an object – like what you see just now Static: NO need to use New() to use, there is just one copy of the class.  This type of class basically to provide special functions for other objects   So if you see a class being used without New(): it is a static class EgMathclass    age = Math.Round(18.5);  // Math rounding
First Console Program A new project:
Understanding Program.cs using System; namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {         }     } }
Understanding Program.cs using System; namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {         }     } } What is a namespace? It is simply a logical collection of related classes.  Eg namespace System { publicstatic class Console     {         …. // with properties and methods     }     class xxxx     {          …. // with properties and methods     } }
Understanding Program.cs using System; namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {         }     } } What is a namespace? It is simply a logical collection of related classes.  Eg namespace System { publicstatic class Console     {         …. // with properties and methods     }     class xxxx     {          …. // with properties and methods     } } Access Modifiers – on class, its properties and methods Types: public, private, protected public: Access is not restricted. private: Access is limited to the containing type. protected: Access is limited to the containing class or types derived from the containing class.
Group Exercise public class Apple {     public OutputVariety() { … }     protected ReadColour()  { …. }     private ResetColour()  { …. } } class AnotherClass{    ….   } class DerivedClass: Apple {    ….   } Which class could access the 3 methods?
Understanding Program.cs using System; namespace ConsoleApplication1 {     class Program     { static void Main(string[] args)         {         }     } }
Understanding Program.cs using System; namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {         }     } } Inside a namespace, may contain internal namespace. namespace System {     namespace Data     {         …. // with classes & their properties and methods     } } OR  combine with a “.” Namespace System.Data {      …. // with classes & their properties and methods }
Group Exercise namespace ITE.MP.GDD {     public class 2W { public intClassSize;  } } namespace ITE.MP.EC {      public class 2W { public intClassSize;  } } How to access ClassSize for one ITE.MP.GDD.2W object?
Add code and run program Add following code into main() Console.WriteLine("Welcome!");  > View >output > Build > Build Solution > Debug > Start Without Debugging
Understanding Program.cs Demo: Remove all “using” statements and correct error - instead of Console.WriteLine(), change to System.Console.WriteLine()   Demo: Using refactor to rename “Program” to “HelloWorld” Demo: Right click on Console > Go To Definition- take a look at the Console class and its WriteLine method
Main(string[] args) string[] args  // string array  			      // with name args Getting inputs from commandline Console.WriteLine("Hello " + args[0]);
Setting Arguments Argument is what user key in after the program file Eg for Unreal Tournament, to use the editor, user type “ut.exe  editor” => editor is an argument In console program, there are two ways to key in arguments for running the program: 1. Using IDE    2. Use command prompt
Setting arguments using IDE In Solution Explorer, right click the project and select Properties:
Setting arguments using cmd Start > run > cmd Use cd, dir to move to the projet debug folder:  …. myFirstPrograminebug> > myFirstProgram.exe   joe
C# Application Programming Interface (API) C# API or .NET Framework Class Library Reference http://msdn.microsoft.com/en-us/library/ms229335.aspx
Recap Console program: Simple Procedural (from top to bottom) Inputs:   Arguments: How? Eg       Unreal Tournament Editor:                    > UT.exe  editor More useful to take inputs from Keyboard: How?
Guided Hands on Launch Visual Studio 2008 – C# Create a new console project Add the following line into main(..) Console.WriteLine("Hello " + args[0]); Add argument “James“ Build and run
Get input from keyboard static void Main()  {  string str; // A string variable to hold input Console.Write(“Your input:");  str = Console.ReadLine();  // Wait for inputs Console.WriteLine("You entered: " + str);  }
Exercise 2.1 Create your first console program Remove the unused namespaces Use refactor to rename your class to YourFullName   Refer to the following URL for Naming Convention for Class: http://alturl.com/6uzp http://alturl.com/o7fi What you see on screen  (blue by computer, red by user) Your Input: 3 Output: 3  3  3  3  3
Type conversion Need to tell computer to convert from string to integer/double string str;  // a string variable str = Console.Readline(); // get input intnumberInteger; // an integer variable // convert string type to integer type numberInteger = int.Parse(str); double numberDouble; // a decimal variable // convert string type to decimal type numberDouble = double.Parse(str)
Exercise 2.2    Write a program that asks the user to type 5 integers and writes the average of the 5 integers. This program can use only 2 variables.   Hint 1: Use decimal instead of integer eg:  doublemyNumber;     Hint 2:  conversion from string to double eg:  myNumber = double.Parse(str); => Next page for screen output
Exercise 2.2  What you see on screen  (blue by computer, red by user) Input1: 1 Input2: 4 Input3: 4 Input4: 2 Input5: 3 Average: 2.8
Exercise 2.3    Write a program that asks the user to type the width and the length of a rectangle and then outputs to the screen the area and the perimeter of that rectangle. 	Hint: 1) Assume that the width and length                are integers           2) eg: width = int.Parse(str); 	        3) Operator for multiplication: * eg: area = width * length; => Next page for screen output
Exercise 2.3  What you see on screen  (blue by computer, red by user) Width: 5 Length: 4 Area:20 and Perimeter:18
Exercise 2.4    Write a program that asks the user to type 2 integers A and B and exchange the value of A and B. Hint:1) To swop 2 variable, you need              another variable to store one of the              value        2) Eg for ascending order (small to Big)             if (n1 > n2)             {                 n3 = n2;                 n2 = n1;                 n1 = n3;              }   “then” processing
Exercise 2.4  What you see on screen  (blue by computer, red by user) Input1: 1 Input2: 4 Input1: 4  Input2: 1
Exercise 2.5 Prompt user to key in 3 integers. Sort the integers in ascending order. Print the 3 integers as you sort. Eg: Input1: 2 Input2: 1 Input3: 0 210 120 102 012 if (n1 > n2) { … } if (n2 > n3) { … } if (n1 > n2) { … }
Summary C# Language Fundamentals covered: int, double, string, Console.WriteLine(), Console.Write(), Console.ReadLine(), int.Parse(), double.Parse(), simple if then statement Problem solving skills covered: ,[object Object]

More Related Content

What's hot

Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstractionIntro C# Book
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IEduardo Bergavera
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionSvetlin Nakov
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++jehan1987
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variablesIntro C# Book
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classesIntro C# Book
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++Hoang Nguyen
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm ComplexityIntro C# Book
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSVISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSSuraj Kumar
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Abou Bakr Ashraf
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 

What's hot (20)

Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONSVISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
 
Bc0037
Bc0037Bc0037
Bc0037
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Templates
TemplatesTemplates
Templates
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 

Viewers also liked

Social Media Report - Big 5 Canadian Banks September - October 2016
Social Media Report - Big 5 Canadian Banks September - October 2016Social Media Report - Big 5 Canadian Banks September - October 2016
Social Media Report - Big 5 Canadian Banks September - October 2016Unmetric
 
Ignite (10m) how to not burn out your monitoring team
Ignite (10m)   how to not burn out your monitoring teamIgnite (10m)   how to not burn out your monitoring team
Ignite (10m) how to not burn out your monitoring teamGil Zellner
 
Борьба с биологическими инвазиями.
Борьба с биологическими инвазиями.Борьба с биологическими инвазиями.
Борьба с биологическими инвазиями.kulibin
 
Classes inversées, classes "à l'envers" ? Et si c’était simplement Enseigner ...
Classes inversées, classes "à l'envers" ? Et si c’était simplement Enseigner ...Classes inversées, classes "à l'envers" ? Et si c’était simplement Enseigner ...
Classes inversées, classes "à l'envers" ? Et si c’était simplement Enseigner ...Marcel Lebrun
 
Become a Learning Masterchef
Become a Learning MasterchefBecome a Learning Masterchef
Become a Learning MasterchefKineoPacific
 
How to Kick Ass on Google When You're All Out of Bubblegum
How to Kick Ass on Google When You're All Out of BubblegumHow to Kick Ass on Google When You're All Out of Bubblegum
How to Kick Ass on Google When You're All Out of BubblegumGreg Gifford
 
Stepping into Internet-Virtual Worlds and Future of Value Creation
Stepping into Internet-Virtual Worlds and Future of Value CreationStepping into Internet-Virtual Worlds and Future of Value Creation
Stepping into Internet-Virtual Worlds and Future of Value CreationRobin Teigland
 
Common Mistakes Travelers Make
Common Mistakes Travelers MakeCommon Mistakes Travelers Make
Common Mistakes Travelers MakeVisitorsCoverage
 
How to deal with bitter criticism
How to deal with bitter criticismHow to deal with bitter criticism
How to deal with bitter criticismXenia Y
 
SLA School Librarian of the Year 2012
SLA School Librarian of the Year 2012SLA School Librarian of the Year 2012
SLA School Librarian of the Year 2012Candy Gourlay
 
Présentation Xebicon15 - Akamis - Marketing & IT
Présentation Xebicon15 -  Akamis - Marketing & ITPrésentation Xebicon15 -  Akamis - Marketing & IT
Présentation Xebicon15 - Akamis - Marketing & ITAKAMIS
 
Accelerated Startup - #Idea-to-IPO
Accelerated Startup - #Idea-to-IPOAccelerated Startup - #Idea-to-IPO
Accelerated Startup - #Idea-to-IPOVitaly Golomb
 
Desempeño del ingeniero industrial en la empresa centrales1
Desempeño del ingeniero industrial en la empresa centrales1Desempeño del ingeniero industrial en la empresa centrales1
Desempeño del ingeniero industrial en la empresa centrales1Annabel Lee
 
The Content Marketing Platform: Why Kapost is Critical to Content Marketing S...
The Content Marketing Platform: Why Kapost is Critical to Content Marketing S...The Content Marketing Platform: Why Kapost is Critical to Content Marketing S...
The Content Marketing Platform: Why Kapost is Critical to Content Marketing S...Kapost
 
Time is Life. Life is Time
Time is Life. Life is TimeTime is Life. Life is Time
Time is Life. Life is TimeShirley Williams
 
Centros De Responsabilidad Intra (ExposicióN)
Centros De Responsabilidad Intra (ExposicióN)Centros De Responsabilidad Intra (ExposicióN)
Centros De Responsabilidad Intra (ExposicióN)Julio Nuñez
 
The Science of Webinars
The Science of WebinarsThe Science of Webinars
The Science of WebinarsHubSpot
 
23 frases de liderazgo para inspirarte
23 frases de liderazgo para inspirarte23 frases de liderazgo para inspirarte
23 frases de liderazgo para inspirarteAna Sek
 

Viewers also liked (20)

Social Media Report - Big 5 Canadian Banks September - October 2016
Social Media Report - Big 5 Canadian Banks September - October 2016Social Media Report - Big 5 Canadian Banks September - October 2016
Social Media Report - Big 5 Canadian Banks September - October 2016
 
Ignite (10m) how to not burn out your monitoring team
Ignite (10m)   how to not burn out your monitoring teamIgnite (10m)   how to not burn out your monitoring team
Ignite (10m) how to not burn out your monitoring team
 
Борьба с биологическими инвазиями.
Борьба с биологическими инвазиями.Борьба с биологическими инвазиями.
Борьба с биологическими инвазиями.
 
Classes inversées, classes "à l'envers" ? Et si c’était simplement Enseigner ...
Classes inversées, classes "à l'envers" ? Et si c’était simplement Enseigner ...Classes inversées, classes "à l'envers" ? Et si c’était simplement Enseigner ...
Classes inversées, classes "à l'envers" ? Et si c’était simplement Enseigner ...
 
Become a Learning Masterchef
Become a Learning MasterchefBecome a Learning Masterchef
Become a Learning Masterchef
 
How to Kick Ass on Google When You're All Out of Bubblegum
How to Kick Ass on Google When You're All Out of BubblegumHow to Kick Ass on Google When You're All Out of Bubblegum
How to Kick Ass on Google When You're All Out of Bubblegum
 
Stepping into Internet-Virtual Worlds and Future of Value Creation
Stepping into Internet-Virtual Worlds and Future of Value CreationStepping into Internet-Virtual Worlds and Future of Value Creation
Stepping into Internet-Virtual Worlds and Future of Value Creation
 
Common Mistakes Travelers Make
Common Mistakes Travelers MakeCommon Mistakes Travelers Make
Common Mistakes Travelers Make
 
How to deal with bitter criticism
How to deal with bitter criticismHow to deal with bitter criticism
How to deal with bitter criticism
 
SLA School Librarian of the Year 2012
SLA School Librarian of the Year 2012SLA School Librarian of the Year 2012
SLA School Librarian of the Year 2012
 
Présentation Xebicon15 - Akamis - Marketing & IT
Présentation Xebicon15 -  Akamis - Marketing & ITPrésentation Xebicon15 -  Akamis - Marketing & IT
Présentation Xebicon15 - Akamis - Marketing & IT
 
Accelerated Startup - #Idea-to-IPO
Accelerated Startup - #Idea-to-IPOAccelerated Startup - #Idea-to-IPO
Accelerated Startup - #Idea-to-IPO
 
Desempeño del ingeniero industrial en la empresa centrales1
Desempeño del ingeniero industrial en la empresa centrales1Desempeño del ingeniero industrial en la empresa centrales1
Desempeño del ingeniero industrial en la empresa centrales1
 
The Content Marketing Platform: Why Kapost is Critical to Content Marketing S...
The Content Marketing Platform: Why Kapost is Critical to Content Marketing S...The Content Marketing Platform: Why Kapost is Critical to Content Marketing S...
The Content Marketing Platform: Why Kapost is Critical to Content Marketing S...
 
Time is Life. Life is Time
Time is Life. Life is TimeTime is Life. Life is Time
Time is Life. Life is Time
 
Centros De Responsabilidad Intra (ExposicióN)
Centros De Responsabilidad Intra (ExposicióN)Centros De Responsabilidad Intra (ExposicióN)
Centros De Responsabilidad Intra (ExposicióN)
 
The Science of Webinars
The Science of WebinarsThe Science of Webinars
The Science of Webinars
 
KZero Radar chart Q1 2013
KZero Radar chart Q1 2013KZero Radar chart Q1 2013
KZero Radar chart Q1 2013
 
Disc
DiscDisc
Disc
 
23 frases de liderazgo para inspirarte
23 frases de liderazgo para inspirarte23 frases de liderazgo para inspirarte
23 frases de liderazgo para inspirarte
 

Similar to Getting Started - Console Program and Problem Solving

Similar to Getting Started - Console Program and Problem Solving (20)

Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
C#ppt
C#pptC#ppt
C#ppt
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
Csharp generics
Csharp genericsCsharp generics
Csharp generics
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
C# p3
C# p3C# p3
C# p3
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning material
 
Understanding C# in .NET
Understanding C# in .NETUnderstanding C# in .NET
Understanding C# in .NET
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
Csharp_mahesh
Csharp_maheshCsharp_mahesh
Csharp_mahesh
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
 
ptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdf
ptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdfptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdf
ptu3-harvey-m-deitel-paul-j-deitel-tem-r-nieto-contributor-paul-j-deitel.pdf
 

More from Hock Leng PUAH

ASP.net Image Slideshow
ASP.net Image SlideshowASP.net Image Slideshow
ASP.net Image SlideshowHock Leng PUAH
 
Visual basic asp.net programming introduction
Visual basic asp.net programming introductionVisual basic asp.net programming introduction
Visual basic asp.net programming introductionHock Leng PUAH
 
Using iMac Built-in Screen Sharing
Using iMac Built-in Screen SharingUsing iMac Built-in Screen Sharing
Using iMac Built-in Screen SharingHock Leng PUAH
 
Hosting SWF Flash file
Hosting SWF Flash fileHosting SWF Flash file
Hosting SWF Flash fileHock Leng PUAH
 
PHP built-in functions date( ) and mktime( ) to calculate age from date of birth
PHP built-in functions date( ) and mktime( ) to calculate age from date of birthPHP built-in functions date( ) and mktime( ) to calculate age from date of birth
PHP built-in functions date( ) and mktime( ) to calculate age from date of birthHock Leng PUAH
 
PHP built-in function mktime example
PHP built-in function mktime examplePHP built-in function mktime example
PHP built-in function mktime exampleHock Leng PUAH
 
A simple php exercise on date( ) function
A simple php exercise on date( ) functionA simple php exercise on date( ) function
A simple php exercise on date( ) functionHock Leng PUAH
 
Integrate jQuery PHP MySQL project to JOOMLA web site
Integrate jQuery PHP MySQL project to JOOMLA web siteIntegrate jQuery PHP MySQL project to JOOMLA web site
Integrate jQuery PHP MySQL project to JOOMLA web siteHock Leng PUAH
 
Step by step guide to use mac lion to make hidden folders visible
Step by step guide to use mac lion to make hidden folders visibleStep by step guide to use mac lion to make hidden folders visible
Step by step guide to use mac lion to make hidden folders visibleHock Leng PUAH
 
CSS Basic and Common Errors
CSS Basic and Common ErrorsCSS Basic and Common Errors
CSS Basic and Common ErrorsHock Leng PUAH
 
Connectivity Test for EES Logic Probe Project
Connectivity Test for EES Logic Probe ProjectConnectivity Test for EES Logic Probe Project
Connectivity Test for EES Logic Probe ProjectHock Leng PUAH
 
Ohm's law, resistors in series or in parallel
Ohm's law, resistors in series or in parallelOhm's law, resistors in series or in parallel
Ohm's law, resistors in series or in parallelHock Leng PUAH
 
Connections Exercises Guide
Connections Exercises GuideConnections Exercises Guide
Connections Exercises GuideHock Leng PUAH
 
Design to circuit connection
Design to circuit connectionDesign to circuit connection
Design to circuit connectionHock Leng PUAH
 
NMS Media Services Jobshet 1 to 5 Summary
NMS Media Services Jobshet 1 to 5 SummaryNMS Media Services Jobshet 1 to 5 Summary
NMS Media Services Jobshet 1 to 5 SummaryHock Leng PUAH
 
Virtualbox step by step guide
Virtualbox step by step guideVirtualbox step by step guide
Virtualbox step by step guideHock Leng PUAH
 

More from Hock Leng PUAH (20)

ASP.net Image Slideshow
ASP.net Image SlideshowASP.net Image Slideshow
ASP.net Image Slideshow
 
Visual basic asp.net programming introduction
Visual basic asp.net programming introductionVisual basic asp.net programming introduction
Visual basic asp.net programming introduction
 
Using iMac Built-in Screen Sharing
Using iMac Built-in Screen SharingUsing iMac Built-in Screen Sharing
Using iMac Built-in Screen Sharing
 
Hosting SWF Flash file
Hosting SWF Flash fileHosting SWF Flash file
Hosting SWF Flash file
 
PHP built-in functions date( ) and mktime( ) to calculate age from date of birth
PHP built-in functions date( ) and mktime( ) to calculate age from date of birthPHP built-in functions date( ) and mktime( ) to calculate age from date of birth
PHP built-in functions date( ) and mktime( ) to calculate age from date of birth
 
PHP built-in function mktime example
PHP built-in function mktime examplePHP built-in function mktime example
PHP built-in function mktime example
 
A simple php exercise on date( ) function
A simple php exercise on date( ) functionA simple php exercise on date( ) function
A simple php exercise on date( ) function
 
Integrate jQuery PHP MySQL project to JOOMLA web site
Integrate jQuery PHP MySQL project to JOOMLA web siteIntegrate jQuery PHP MySQL project to JOOMLA web site
Integrate jQuery PHP MySQL project to JOOMLA web site
 
Responsive design
Responsive designResponsive design
Responsive design
 
Step by step guide to use mac lion to make hidden folders visible
Step by step guide to use mac lion to make hidden folders visibleStep by step guide to use mac lion to make hidden folders visible
Step by step guide to use mac lion to make hidden folders visible
 
Beautiful web pages
Beautiful web pagesBeautiful web pages
Beautiful web pages
 
CSS Basic and Common Errors
CSS Basic and Common ErrorsCSS Basic and Common Errors
CSS Basic and Common Errors
 
Connectivity Test for EES Logic Probe Project
Connectivity Test for EES Logic Probe ProjectConnectivity Test for EES Logic Probe Project
Connectivity Test for EES Logic Probe Project
 
Logic gate lab intro
Logic gate lab introLogic gate lab intro
Logic gate lab intro
 
Ohm's law, resistors in series or in parallel
Ohm's law, resistors in series or in parallelOhm's law, resistors in series or in parallel
Ohm's law, resistors in series or in parallel
 
Connections Exercises Guide
Connections Exercises GuideConnections Exercises Guide
Connections Exercises Guide
 
Design to circuit connection
Design to circuit connectionDesign to circuit connection
Design to circuit connection
 
NMS Media Services Jobshet 1 to 5 Summary
NMS Media Services Jobshet 1 to 5 SummaryNMS Media Services Jobshet 1 to 5 Summary
NMS Media Services Jobshet 1 to 5 Summary
 
Virtualbox step by step guide
Virtualbox step by step guideVirtualbox step by step guide
Virtualbox step by step guide
 
Nms chapter 01
Nms chapter 01Nms chapter 01
Nms chapter 01
 

Recently uploaded

Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 

Recently uploaded (20)

Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 

Getting Started - Console Program and Problem Solving

  • 1. Chapter 2 Getting Started with C#
  • 2. What is a variable? Variable: imagine like a box to store one thing (data) Eg: int age; age = 5;
  • 3. What is a variable? Variable: imagine like a box to store one thing (data) Eg: int age; age = 5; int - Size of variable must be big enough to store Integer
  • 4. What is a variable? Variable: imagine like a box to store one thing (data) Eg: int age; age = 5; int - Size of variable must be big enough to store Integer age - Name of variable age
  • 5. What is a variable? Variable: imagine like a box to store one thing (data) Eg: int age; age = 5; 5 int - Size of variable must be big enough to store Integer age - Name of variable age age = 5 – Put a data 5 into the variable
  • 6. Value Type Vs Reference Type Variables: two types Value type (simple type like what you just saw) Only need to store one thing (5, 3.5, true/false, ‘C’ and “string”) Reference type (complex type for objects) Need to store more than one thing (age + height + run() + … )
  • 7. Reference Type Reference type (complex type for objects) Eg: Human john; john = new Human(); Compare this to int age; age = 5;
  • 8. Reference Type Reference type (complex type for objects) Eg: Human john; john = new Human(); Human - Size of variable must be big enough to store an Address
  • 9. john Reference Type Reference type (complex type for objects) Eg: Human john; john = new Human(); Human - Size of variable must be big enough to store an Address john - Name of variable
  • 10. Reference Type Reference type (complex type for objects) Eg: Human john; john = new Human(); Human - Size of variable must be big enough to store an Address D403 john - Name of variable … age height john = new Human() – Get a house with enough space for john (age, height, etc) john
  • 11. Non-Static Vs Static Class Non-Static: need New() to instantiate / create an object – like what you see just now Static: NO need to use New() to use, there is just one copy of the class. This type of class basically to provide special functions for other objects So if you see a class being used without New(): it is a static class EgMathclass age = Math.Round(18.5); // Math rounding
  • 12. First Console Program A new project:
  • 13. Understanding Program.cs using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } }
  • 14. Understanding Program.cs using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } } What is a namespace? It is simply a logical collection of related classes. Eg namespace System { publicstatic class Console { …. // with properties and methods } class xxxx { …. // with properties and methods } }
  • 15. Understanding Program.cs using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } } What is a namespace? It is simply a logical collection of related classes. Eg namespace System { publicstatic class Console { …. // with properties and methods } class xxxx { …. // with properties and methods } } Access Modifiers – on class, its properties and methods Types: public, private, protected public: Access is not restricted. private: Access is limited to the containing type. protected: Access is limited to the containing class or types derived from the containing class.
  • 16. Group Exercise public class Apple { public OutputVariety() { … } protected ReadColour() { …. } private ResetColour() { …. } } class AnotherClass{ …. } class DerivedClass: Apple { …. } Which class could access the 3 methods?
  • 17. Understanding Program.cs using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } }
  • 18. Understanding Program.cs using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } } Inside a namespace, may contain internal namespace. namespace System { namespace Data { …. // with classes & their properties and methods } } OR combine with a “.” Namespace System.Data { …. // with classes & their properties and methods }
  • 19. Group Exercise namespace ITE.MP.GDD { public class 2W { public intClassSize; } } namespace ITE.MP.EC { public class 2W { public intClassSize; } } How to access ClassSize for one ITE.MP.GDD.2W object?
  • 20. Add code and run program Add following code into main() Console.WriteLine("Welcome!"); > View >output > Build > Build Solution > Debug > Start Without Debugging
  • 21. Understanding Program.cs Demo: Remove all “using” statements and correct error - instead of Console.WriteLine(), change to System.Console.WriteLine() Demo: Using refactor to rename “Program” to “HelloWorld” Demo: Right click on Console > Go To Definition- take a look at the Console class and its WriteLine method
  • 22. Main(string[] args) string[] args // string array // with name args Getting inputs from commandline Console.WriteLine("Hello " + args[0]);
  • 23. Setting Arguments Argument is what user key in after the program file Eg for Unreal Tournament, to use the editor, user type “ut.exe editor” => editor is an argument In console program, there are two ways to key in arguments for running the program: 1. Using IDE 2. Use command prompt
  • 24. Setting arguments using IDE In Solution Explorer, right click the project and select Properties:
  • 25. Setting arguments using cmd Start > run > cmd Use cd, dir to move to the projet debug folder: …. myFirstPrograminebug> > myFirstProgram.exe joe
  • 26. C# Application Programming Interface (API) C# API or .NET Framework Class Library Reference http://msdn.microsoft.com/en-us/library/ms229335.aspx
  • 27. Recap Console program: Simple Procedural (from top to bottom) Inputs: Arguments: How? Eg Unreal Tournament Editor: > UT.exe editor More useful to take inputs from Keyboard: How?
  • 28. Guided Hands on Launch Visual Studio 2008 – C# Create a new console project Add the following line into main(..) Console.WriteLine("Hello " + args[0]); Add argument “James“ Build and run
  • 29. Get input from keyboard static void Main() { string str; // A string variable to hold input Console.Write(“Your input:"); str = Console.ReadLine(); // Wait for inputs Console.WriteLine("You entered: " + str); }
  • 30. Exercise 2.1 Create your first console program Remove the unused namespaces Use refactor to rename your class to YourFullName Refer to the following URL for Naming Convention for Class: http://alturl.com/6uzp http://alturl.com/o7fi What you see on screen (blue by computer, red by user) Your Input: 3 Output: 3 3 3 3 3
  • 31. Type conversion Need to tell computer to convert from string to integer/double string str; // a string variable str = Console.Readline(); // get input intnumberInteger; // an integer variable // convert string type to integer type numberInteger = int.Parse(str); double numberDouble; // a decimal variable // convert string type to decimal type numberDouble = double.Parse(str)
  • 32. Exercise 2.2 Write a program that asks the user to type 5 integers and writes the average of the 5 integers. This program can use only 2 variables. Hint 1: Use decimal instead of integer eg: doublemyNumber; Hint 2: conversion from string to double eg: myNumber = double.Parse(str); => Next page for screen output
  • 33. Exercise 2.2 What you see on screen (blue by computer, red by user) Input1: 1 Input2: 4 Input3: 4 Input4: 2 Input5: 3 Average: 2.8
  • 34. Exercise 2.3 Write a program that asks the user to type the width and the length of a rectangle and then outputs to the screen the area and the perimeter of that rectangle. Hint: 1) Assume that the width and length are integers 2) eg: width = int.Parse(str); 3) Operator for multiplication: * eg: area = width * length; => Next page for screen output
  • 35. Exercise 2.3 What you see on screen (blue by computer, red by user) Width: 5 Length: 4 Area:20 and Perimeter:18
  • 36. Exercise 2.4 Write a program that asks the user to type 2 integers A and B and exchange the value of A and B. Hint:1) To swop 2 variable, you need another variable to store one of the value 2) Eg for ascending order (small to Big) if (n1 > n2) { n3 = n2; n2 = n1; n1 = n3; } “then” processing
  • 37. Exercise 2.4 What you see on screen (blue by computer, red by user) Input1: 1 Input2: 4 Input1: 4 Input2: 1
  • 38. Exercise 2.5 Prompt user to key in 3 integers. Sort the integers in ascending order. Print the 3 integers as you sort. Eg: Input1: 2 Input2: 1 Input3: 0 210 120 102 012 if (n1 > n2) { … } if (n2 > n3) { … } if (n1 > n2) { … }
  • 39.
  • 40. swopping of 2 variables