spf getting started with console program and problem solving

39
Chapter 2 Getting Started with C#

Upload: hock-leng-puah

Post on 09-Jun-2015

834 views

Category:

Education


2 download

DESCRIPTION

SPF Getting Started with Console Program and Problem Solving

TRANSCRIPT

Page 1: SPF Getting Started with Console Program and Problem Solving

Chapter 2Getting Started with C#

Page 2: SPF Getting Started with Console Program and Problem Solving

What is a variable?

Variable: imagine like a box to store one thing (data)

Eg: int age; age = 5;

Page 3: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

Reference Type

Reference type (complex type for objects)Eg: Human john; john = new Human();

Compare this to int age;age = 5;

Page 8: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

First Console Program

A new project:

Page 13: SPF Getting Started with Console Program and Problem Solving

Understanding Program.cs

using System;

namespace ConsoleApplication1{ class Program { static void Main(string[] args) { } }}

Page 14: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

Understanding Program.cs

using System;

namespace ConsoleApplication1{ class Program { static void Main(string[] args) { } }}

Page 18: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

Add code and run program

Add following code into main()

Console.WriteLine("Welcome!");

> View >output> Build > Build Solution> Debug > Start Without Debugging

Page 21: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

Setting arguments using IDE

In Solution Explorer, right click the project and select Properties:

Page 25: SPF Getting Started with Console Program and Problem Solving

Setting arguments using cmd

Start > run > cmdUse cd, dir to move to the projet debug

folder: …. myFirstProgram\bin\Debug>> myFirstProgram.exe joe

Page 26: SPF Getting Started with Console Program and Problem Solving

C# Application Programming Interface (API)

C# APIor.NET Framework Class Library Reference

http://msdn.microsoft.com/en-us/library/ms229335.aspx

Page 27: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

Exercise 2.2

What you see on screen (blue by computer, red by user)Input1: 1Input2: 4Input3: 4Input4: 2Input5: 3Average: 2.8

Page 34: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

Exercise 2.3

What you see on screen (blue by computer, red by user)Width: 5Length: 4Area:20 and Perimeter:18

Page 36: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

Exercise 2.4

What you see on screen (blue by computer, red by user)Input1: 1Input2: 4Input1: 4 Input2: 1

Page 38: SPF Getting Started with Console Program and Problem Solving

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: SPF Getting Started with Console Program and Problem Solving

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