Your SlideShare is downloading. ×
  • Like
SPF Getting Started - Console Program
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

SPF Getting Started - Console Program

  • 542 views
Published

C# console program and basic problem solving skills

C# console program and basic problem solving skills

Published in Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
542
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
6
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Chapter 2
    Getting Started with C#
  • 2. Visual C# IDE
    A new project:
  • 3. 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.
  • 4. Group Exercise
    public class Apple
    {
    public OutputVariety() { … }
    protected ReadColour() { …. }
    private ResetColour() { …. }
    }
    class AnotherClass{ …. }
    class DerivedClass: Apple { …. }
    Which class could access the methods?
  • 5. 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
    }
    }
    Non-static (default) vs static
    Non-static class must be instantiate to use.
    Eg
    class Enemy { …. }
    static class Random { …. }
    class Game1
    {
    Enemy usTroop = new Enemy();
    usTroop.method();
    int x = Random.Next(0,10);
    }
  • 6. 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
    }
  • 7. 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?
  • 8. Understanding Program.cs
    Add following code into main()
    Console.WriteLine("Welcome!");
    > View >output
    > Build > Build Solution
    > Debug > Start Without Debugging
  • 9. 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 WriteLinemethod
  • 10. Main(string[] args)
    string[] args // string array
    // with name args
    Getting inputs from commandline
    Console.WriteLine("Hello " + args[0]);
  • 11. Setting arguments in properties
    In Solution Explorer, right click the project and select Properties:
  • 12. Setting arguments - cmd
    Start > run > cmd
    Use cd, dir to move to the projet debug folder:
    …. myFirstPrograminDebug>
    > myFirstProgram.exe joe
  • 13. C# Application Programming Interface (API)
    C# API
    or
    .NET Framework Class Library Reference
    http://msdn.microsoft.com/en-us/library/ms229335.aspx
  • 14. 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?
  • 15. 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
  • 16. Get input from user
    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);
    }
  • 17. 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
  • 18. 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)
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. Exercise 2.3
    What you see on screen
    (blue by computer, red by user)
    Width: 5
    Length: 4
    Area:20 and Perimeter:18
  • 23. 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
  • 24. Exercise 2.4
    What you see on screen
    (blue by computer, red by user)
    Input1: 1
    Input2: 4
    Input1: 4
    Input2: 1
  • 25. 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) { … }
  • 26. 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
    • 27. swopping of 2 variables
    • 28. simple sorting