Getting Started - Console Program and Problem Solving

1,118 views

Published on

C# Programming and Fundamentals - getting started with console program & problem solving

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
1,118
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Getting Started - Console Program and Problem Solving

  1. 1. Chapter 2<br />Getting Started with C#<br />
  2. 2. What is a variable?<br />Variable: imagine like a box to store one thing (data)<br />Eg: int age;<br /> age = 5;<br />
  3. 3. What is a variable?<br />Variable: imagine like a box to store one thing (data)<br />Eg: int age;<br /> age = 5;<br />int - Size of variable must be big enough to store Integer<br />
  4. 4. What is a variable?<br />Variable: imagine like a box to store one thing (data)<br />Eg: int age;<br /> age = 5;<br />int - Size of variable must be big enough to store Integer<br />age - Name of variable<br />age<br />
  5. 5. What is a variable?<br />Variable: imagine like a box to store one thing (data)<br />Eg: int age;<br /> age = 5;<br />5<br />int - Size of variable must be big enough to store Integer<br />age - Name of variable<br />age<br />age = 5 – Put a data 5 into the variable<br />
  6. 6. Value Type Vs Reference Type<br />Variables: two types<br />Value type (simple type like what you just saw)<br />Only need to store one thing (5, 3.5, true/false, ‘C’ and “string”)<br />Reference type (complex type for objects)<br />Need to store more than one thing (age + height + run() + … )<br />
  7. 7. Reference Type<br />Reference type (complex type for objects)<br />Eg: Human john;<br /> john = new Human();<br />Compare this to <br />int age;<br />age = 5;<br />
  8. 8. Reference Type<br />Reference type (complex type for objects)<br />Eg: Human john;<br /> john = new Human();<br />Human - Size of variable must be big enough to store an Address<br />
  9. 9. john<br />Reference Type<br />Reference type (complex type for objects)<br />Eg: Human john;<br /> john = new Human();<br />Human - Size of variable must be big enough to store an Address<br />john - Name of variable<br />
  10. 10. Reference Type<br />Reference type (complex type for objects)<br />Eg: Human john;<br /> john = new Human();<br />Human - Size of variable must be big enough to store an Address<br />D403<br />john - Name of variable<br />…<br />age<br />height<br />john = new Human() – Get a house with enough space for john (age, height, etc)<br />john<br />
  11. 11. Non-Static Vs Static Class<br />Non-Static: need New() to instantiate / create an object – like what you see just now<br />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 <br />So if you see a class being used without New(): it is a static class<br />EgMathclass<br /> age = Math.Round(18.5); // Math rounding<br />
  12. 12. First Console Program<br />A new project:<br />
  13. 13. 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 />
  14. 14. 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 />
  15. 15. 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 />
  16. 16. 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 3 methods?<br />
  17. 17. 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 />
  18. 18. 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 />
  19. 19. 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 />
  20. 20. Add code and run program<br />Add following code into main()<br />Console.WriteLine("Welcome!"); <br />> View >output<br />> Build > Build Solution<br />> Debug > Start Without Debugging<br />
  21. 21. Understanding 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 WriteLine method<br />
  22. 22. Main(string[] args)<br />string[] args // string array <br /> // with name args<br />Getting inputs from commandline<br />Console.WriteLine("Hello " + args[0]);<br />
  23. 23. Setting Arguments<br />Argument is what user key in after the program file<br />Eg for Unreal Tournament, to use the editor, user type “ut.exe editor” => editor is an argument<br />In console program, there are two ways to key in arguments for running the program:<br />1. Using IDE 2. Use command prompt<br />
  24. 24. Setting arguments using IDE<br />In Solution Explorer, right click the project and select Properties:<br />
  25. 25. Setting arguments using cmd<br />Start > run > cmd<br />Use cd, dir to move to the projet debug folder:<br /> …. myFirstPrograminDebug><br />> myFirstProgram.exe joe<br />
  26. 26. 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 />
  27. 27. 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 />
  28. 28. 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 />
  29. 29. Get input from keyboard<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 />
  30. 30. 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 />
  31. 31. 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 />
  32. 32. 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 />
  33. 33. 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 />
  34. 34. 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 />
  35. 35. 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 />
  36. 36. 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 />
  37. 37. 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 />
  38. 38. 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 />
  39. 39. 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
  40. 40. swopping of 2 variables
  41. 41. simple sorting</li>

×