Chapter 2<br />Getting Started with C#<br />
Visual C# IDE<br />A new project:<br />
Understanding Program.cs<br />using System;<br />namespace ConsoleApplication1<br />{<br />    class Program<br />    {<br...
Group Exercise<br />public class Apple<br />{<br />    public OutputVariety() { … }<br />    protected ReadColour()  { …. ...
Understanding Program.cs<br />using System;<br />namespace ConsoleApplication1<br />{<br />    class Program<br />    {<br...
Understanding Program.cs<br />using System;<br />namespace ConsoleApplication1<br />{<br />    class Program<br />    {<br...
Group Exercise<br />namespace ITE.MP.GDD<br />{<br />    public class 2W { public intClassSize;  }<br />}<br />namespace I...
Understanding Program.cs<br />Add following code into main()<br />Console.WriteLine("Welcome!"); <br />> View >output<br /...
Program.cs<br />Demo: Remove all “using” statements and correct error - instead of Console.WriteLine(), change to System.C...
Main(string[] args)<br />string[] args  // string array <br />			      // with name args<br />Getting inputs from commandl...
Setting arguments in properties<br />In Solution Explorer, right click the project and select Properties:<br />
Setting arguments - cmd<br />Start > run > cmd<br />Use cd, dir to move to the projet debug folder:<br /> …. myFirstProgra...
C# Application Programming Interface (API)<br />C# API<br />or<br />.NET Framework Class Library Reference<br />http://msd...
Recap<br />Console program:<br />Simple<br />Procedural (from top to bottom)<br />Inputs:  <br />Arguments: How?<br />Eg<b...
Guided Hands on<br />Launch Visual Studio 2008 – C#<br />Create a new console project<br />Add the following line into mai...
Get input from user<br />static void Main() <br />{ <br />string str; // A string variable to hold input<br />Console.Writ...
Exercise 2.1<br />Create your first console program<br />Remove the unused namespaces<br />Use refactor to rename your cla...
Type conversion<br />Need to tell computer to convert from<br />string to integer/double<br />string str;  // a string var...
Exercise 2.2 <br />  Write a program that asks the user to type 5 integers and writes the average of the 5 integers. This ...
Exercise 2.2 <br />What you see on screen <br />(blue by computer, red by user)<br />Input1: 1<br />Input2: 4<br />Input3:...
Exercise 2.3 <br />  Write a program that asks the user to type the width and the length of a rectangle and then outputs t...
Exercise 2.3 <br />What you see on screen <br />(blue by computer, red by user)<br />Width: 5<br />Length: 4<br />Area:20 ...
Exercise 2.4 <br />  Write a program that asks the user to type 2 integers A and B and exchange the value of A and B.<br /...
Exercise 2.4 <br />What you see on screen <br />(blue by computer, red by user)<br />Input1: 1<br />Input2: 4<br />Input1:...
Exercise 2.5<br />Prompt user to key in 3 integers.<br />Sort the integers in ascending order.<br />Print the 3 integers a...
Summary<br />C# Language Fundamentals covered:<br />int, double, string, Console.WriteLine(), Console.Write(), Console.Rea...
swopping of 2 variables
simple sorting</li>
SPF Getting Started - Console Program
SPF Getting Started - Console Program
SPF Getting Started - Console Program
SPF Getting Started - Console Program
SPF Getting Started - Console Program
SPF Getting Started - Console Program
SPF Getting Started - Console Program
SPF Getting Started - Console Program
SPF Getting Started - Console Program
SPF Getting Started - Console Program
SPF Getting Started - Console Program
Upcoming SlideShare
Loading in …5
×

SPF Getting Started - Console Program

780 views

Published on

C# console program and basic problem solving skills

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
780
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SPF Getting Started - Console Program

  1. 1. Chapter 2<br />Getting Started with C#<br />
  2. 2. Visual C# IDE<br />A new project:<br />
  3. 3. Understanding Program.cs<br />using System;<br />namespace ConsoleApplication1<br />{<br /> class Program<br /> {<br /> static void Main(string[] args)<br /> {<br /> }<br /> }<br />}<br />What is a namespace?<br />It is simply a logical collection of related classes. Eg<br />namespace System<br />{<br />publicstatic class Console<br /> {<br /> …. // with properties and methods<br /> }<br /> class xxxx<br /> {<br /> …. // with properties and methods<br /> }<br />}<br />Access Modifiers – on class, its properties and methods<br />Types: public, private, protected<br />public: Access is not restricted.<br />private: Access is limited to the containing type.<br />protected: Access is limited to the containing class or types derived from the containing class.<br />
  4. 4. Group Exercise<br />public class Apple<br />{<br /> public OutputVariety() { … }<br /> protected ReadColour() { …. }<br /> private ResetColour() { …. }<br />}<br />class AnotherClass{ …. }<br />class DerivedClass: Apple { …. }<br />Which class could access the methods?<br />
  5. 5. Understanding Program.cs<br />using System;<br />namespace ConsoleApplication1<br />{<br /> class Program<br /> {<br /> static void Main(string[] args)<br /> {<br /> }<br /> }<br />}<br />What is a namespace?<br />It is simply a logical collection of related classes. Eg<br />namespace System<br />{<br />publicstatic class Console<br /> {<br /> …. // with properties and methods<br /> }<br /> class xxxx<br /> {<br /> …. // with properties and methods<br /> }<br />}<br />Non-static (default) vs static<br />Non-static class must be instantiate to use. <br />Eg<br />class Enemy { …. }<br />static class Random { …. }<br />class Game1 <br />{<br /> Enemy usTroop = new Enemy();<br />usTroop.method();<br />int x = Random.Next(0,10);<br />}<br />
  6. 6. Understanding Program.cs<br />using System;<br />namespace ConsoleApplication1<br />{<br /> class Program<br /> {<br /> static void Main(string[] args)<br /> {<br /> }<br /> }<br />}<br />Inside a namespace, may contain internal namespace.<br />namespace System<br />{<br /> namespace Data<br /> {<br /> …. // with classes & their properties and methods<br /> }<br />}<br />OR combine with a “.”<br />Namespace System.Data<br />{<br /> …. // with classes & their properties and methods<br />}<br />
  7. 7. Group Exercise<br />namespace ITE.MP.GDD<br />{<br /> public class 2W { public intClassSize; }<br />}<br />namespace ITE.MP.EC<br />{<br /> public class 2W { public intClassSize; }<br />}<br />How to access ClassSize for one ITE.MP.GDD.2W object?<br />
  8. 8. Understanding Program.cs<br />Add following code into main()<br />Console.WriteLine("Welcome!"); <br />> View >output<br />> Build > Build Solution<br />> Debug > Start Without Debugging<br />
  9. 9. Program.cs<br />Demo: Remove all “using” statements and correct error - instead of Console.WriteLine(), change to System.Console.WriteLine() <br />Demo: Using refactor to rename “Program” to “HelloWorld”<br />Demo: Right click on Console > Go To Definition- take a look at the Console class and its WriteLinemethod<br />
  10. 10. Main(string[] args)<br />string[] args // string array <br /> // with name args<br />Getting inputs from commandline<br />Console.WriteLine("Hello " + args[0]);<br />
  11. 11. Setting arguments in properties<br />In Solution Explorer, right click the project and select Properties:<br />
  12. 12. Setting arguments - cmd<br />Start > run > cmd<br />Use cd, dir to move to the projet debug folder:<br /> …. myFirstPrograminDebug><br />> myFirstProgram.exe joe<br />
  13. 13. C# Application Programming Interface (API)<br />C# API<br />or<br />.NET Framework Class Library Reference<br />http://msdn.microsoft.com/en-us/library/ms229335.aspx<br />
  14. 14. Recap<br />Console program:<br />Simple<br />Procedural (from top to bottom)<br />Inputs: <br />Arguments: How?<br />Eg<br /> Unreal Tournament Editor: <br /> > UT.exe editor<br />More useful to take inputs from Keyboard: How?<br />
  15. 15. Guided Hands on<br />Launch Visual Studio 2008 – C#<br />Create a new console project<br />Add the following line into main(..)<br />Console.WriteLine("Hello " + args[0]);<br />Add argument “James“<br />Build and run<br />
  16. 16. Get input from user<br />static void Main() <br />{ <br />string str; // A string variable to hold input<br />Console.Write(“Your input:"); <br />str = Console.ReadLine(); // Wait for inputs<br />Console.WriteLine("You entered: " + str); <br />}<br />
  17. 17. Exercise 2.1<br />Create your first console program<br />Remove the unused namespaces<br />Use refactor to rename your class to YourFullName<br /> Refer to the following URL for Naming Convention for Class:<br />http://alturl.com/6uzp<br />http://alturl.com/o7fi<br />What you see on screen <br />(blue by computer, red by user)<br />Your Input: 3<br />Output: 3 3 3 3 3<br />
  18. 18. Type conversion<br />Need to tell computer to convert from<br />string to integer/double<br />string str; // a string variable<br />str = Console.Readline(); // get input<br />intnumberInteger; // an integer variable<br />// convert string type to integer type<br />numberInteger = int.Parse(str);<br />double numberDouble; // a decimal variable<br />// convert string type to decimal type<br />numberDouble = double.Parse(str)<br />
  19. 19. Exercise 2.2 <br /> 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.<br /> Hint 1: Use decimal instead of integer<br />eg: doublemyNumber; <br />Hint 2: conversion from string to double<br />eg: myNumber= double.Parse(str);<br />=> Next page for screen output<br />
  20. 20. Exercise 2.2 <br />What you see on screen <br />(blue by computer, red by user)<br />Input1: 1<br />Input2: 4<br />Input3: 4<br />Input4: 2<br />Input5: 3<br />Average: 2.8<br />
  21. 21. Exercise 2.3 <br /> 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.<br /> Hint: 1) Assume that the width and length <br /> are integers<br /> 2) eg: width = int.Parse(str);<br /> 3) Operator for multiplication: *<br />eg: area = width * length;<br />=> Next page for screen output<br />
  22. 22. Exercise 2.3 <br />What you see on screen <br />(blue by computer, red by user)<br />Width: 5<br />Length: 4<br />Area:20 and Perimeter:18<br />
  23. 23. Exercise 2.4 <br /> Write a program that asks the user to type 2 integers A and B and exchange the value of A and B.<br />Hint:1) To swop 2 variable, you need <br />another variable to store one of the <br />value<br /> 2) Eg for ascending order (small to Big)<br /> if (n1 > n2)<br /> {<br /> n3 = n2;<br /> n2 = n1;<br /> n1 = n3;<br /> } <br />“then” processing<br />
  24. 24. Exercise 2.4 <br />What you see on screen <br />(blue by computer, red by user)<br />Input1: 1<br />Input2: 4<br />Input1: 4 <br />Input2: 1 <br />
  25. 25. Exercise 2.5<br />Prompt user to key in 3 integers.<br />Sort the integers in ascending order.<br />Print the 3 integers as you sort.<br />Eg:<br />Input1: 2<br />Input2: 1<br />Input3: 0<br />210<br />120<br />102<br />012<br />if (n1 > n2) { … }<br />if (n2 > n3) { … }<br />if (n1 > n2) { … }<br />
  26. 26. Summary<br />C# Language Fundamentals covered:<br />int, double, string, Console.WriteLine(), Console.Write(), Console.ReadLine(), int.Parse(), double.Parse(), simple if then statement<br />Problem solving skills covered:<br /><ul><li>repeating steps
  27. 27. swopping of 2 variables
  28. 28. simple sorting</li>

×