![Page 1: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/1.jpg)
Chapter 2Getting Started with C#
![Page 2: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/2.jpg)
What is a variable?
Variable: imagine like a box to store one thing (data)
Eg: int age; age = 5;
![Page 3: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/3.jpg)
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
![Page 4: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/4.jpg)
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 Integerage - Name of variable
age
![Page 5: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/5.jpg)
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 Integerage - Name of variable
age = 5 – Put a data 5 into the variable
age
5
![Page 6: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/6.jpg)
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() + … )
![Page 7: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/7.jpg)
Reference Type
Reference type (complex type for objects)Eg: Human john; john = new Human();
Compare this to int age;age = 5;
![Page 8: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/8.jpg)
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
![Page 9: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/9.jpg)
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 Addressjohn - Name of variable
![Page 10: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/10.jpg)
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 Addressjohn - Name of variable
john = new Human() – Get a house with enough space for john (age, height, etc)
john
D403
age
height
…
![Page 11: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/11.jpg)
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
Eg Math class age = Math.Round(18.5); // Math rounding
![Page 12: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/12.jpg)
First Console Program
A new project:
![Page 13: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/13.jpg)
Understanding Program.cs
using System;
namespace ConsoleApplication1{ class Program { static void Main(string[] args) { } }}
![Page 14: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/14.jpg)
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. Egnamespace System{ public static class Console { …. // with properties and methods } class xxxx { …. // with properties and methods }}
![Page 15: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/15.jpg)
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. Egnamespace System{ public static class Console { …. // with properties and methods } class xxxx { …. // with properties and methods }}
Access Modifiers – on class, its properties and methodsTypes: 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.
![Page 16: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/16.jpg)
Group Exercise
public class Apple{ public OutputVariety() { … } protected ReadColour() { …. } private ResetColour() { …. }}class AnotherClass{ …. }class DerivedClass : Apple { …. }
Which class could access the 3 methods?
![Page 17: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/17.jpg)
Understanding Program.cs
using System;
namespace ConsoleApplication1{ class Program { static void Main(string[] args) { } }}
![Page 18: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/18.jpg)
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}
![Page 19: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/19.jpg)
Group Exercise
namespace ITE.MP.GDD{ public class 2W { public int ClassSize; }}namespace ITE.MP.EC{ public class 2W { public int ClassSize; }}
How to access ClassSize for one ITE.MP.GDD.2W object?
![Page 20: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/20.jpg)
Add code and run program
Add following code into main()
Console.WriteLine("Welcome!");
> View >output> Build > Build Solution> Debug > Start Without Debugging
![Page 21: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/21.jpg)
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
![Page 22: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/22.jpg)
Main(string[] args)
string[] args // string array // with name args
Getting inputs from commandline
Console.WriteLine("Hello " + args[0]);
•First argument
args[0]
•Second argument
args[1]•T
hird argument
args[2]
![Page 23: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/23.jpg)
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
![Page 24: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/24.jpg)
Setting arguments using IDE
In Solution Explorer, right click the project and select Properties:
![Page 25: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/25.jpg)
Setting arguments using cmd
Start > run > cmdUse cd, dir to move to the projet debug
folder: …. myFirstProgram\bin\Debug>> myFirstProgram.exe joe
![Page 26: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/26.jpg)
C# Application Programming Interface (API)
C# APIor.NET Framework Class Library Reference
http://msdn.microsoft.com/en-us/library/ms229335.aspx
![Page 27: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/27.jpg)
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?
![Page 28: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/28.jpg)
Guided Hands on
Launch Visual Studio 2008 – C#Create a new console projectAdd the following line into main(..)
Console.WriteLine("Hello " + args[0]);Add argument “James“Build and run
![Page 29: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/29.jpg)
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); }
![Page 30: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/30.jpg)
Exercise 2.1
Create your first console programRemove the unused namespacesUse 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: 3Output: 3 3 3 3 3
![Page 31: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/31.jpg)
Type conversion
Need to tell computer to convert fromstring to integer/double
string str; // a string variablestr = Console.Readline(); // get inputint numberInteger; // an integer variable// convert string type to integer typenumberInteger = int.Parse(str);double numberDouble; // a decimal variable// convert string type to decimal typenumberDouble = double.Parse(str)
![Page 32: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/32.jpg)
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: double myNumber; Hint 2: conversion from string to double eg: myNumber = double.Parse(str);
=> Next page for screen output
![Page 33: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/33.jpg)
Exercise 2.2
What you see on screen (blue by computer, red by user)Input1: 1Input2: 4Input3: 4Input4: 2Input5: 3Average: 2.8
![Page 34: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/34.jpg)
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
![Page 35: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/35.jpg)
Exercise 2.3
What you see on screen (blue by computer, red by user)Width: 5Length: 4Area:20 and Perimeter:18
![Page 36: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/36.jpg)
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
![Page 37: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/37.jpg)
Exercise 2.4
What you see on screen (blue by computer, red by user)Input1: 1Input2: 4Input1: 4 Input2: 1
![Page 38: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/38.jpg)
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: 2Input2: 1Input3: 0210120102012
if (n1 > n2) { … }
if (n2 > n3) { … }
if (n1 > n2) { … }
![Page 39: Getting Started - Console Program and Problem Solving](https://reader033.vdocument.in/reader033/viewer/2022052522/5538ecf44a79595b7a8b48ab/html5/thumbnails/39.jpg)
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:
repeating steps swopping of 2
variables simple sorting