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
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) { … }