You are on page 1of 17

Chapter 3: Methods & Input/Output

Methods & Input/Output


Quick In-Class Assignment

 Write a program that computes the average


of three exam scores. Each score should be
requested and retrieved from the console.
The program should then compute the
average, then display each score and that
average to the console.
Moving on

 input into and output from the computer.


– Console.WriteLine and Console.Write
– Console.ReadLine, Console.Read and Console.ReadKey
– These methods allow us to get and store character data
from/to the console
– These methods are included in a C# library called System.
But First…Let’s better understand
Methods

 A method is a group of code that (may or may not) accept


parameters and (may or may not) produce a result
 We have been writing a Method
static void Main(string[] args)method
which
– Accepts a parameter named args that is a string array
– Returns nothing
– Is a Static Method (more on that a little later)
– Is the starting place for our programs
More On Methods

 Methods can be programmed within our main


program, in another program or class files, or can be
stored in libraries
 When stored in libraries, to access the Method, that
library must be included at the beginning of the
program.
– What is really happening is the “using” is referencing that
library of functions to “show” the compiler the different ways
that the method can be called.
The Concept of Methods

 Methods are usually blocks of code that are


accessed multiple times in a program.
– So they allow for code to be reused
– They accept parameters so that their results are flexible.
 Methods with the same name can have different sets of
parameters and can return different types. This feature is
called overloading.
 Methods are members of Classes.
 Methods that return a value can be used as a
variable on the right side of an expression
The Concept of Methods
 The normal Method format is:
– Visibility [static] ReturnType Name (Parameters)
Visibility includes
public protected
internal protected internal private
[static] indicates that this method is a “utility” method and is not
dependant on the Class being first initialized to work. More on this
when we talk about classes.

ReturnType is any of the normal types of variables that we have learned


(int, double, string, etc.) or void. void indicates that nothing is
returned.

If there is a return type other than void, then the Method MUST have a
return clause in the code.
More on Methods

 Visibility [static] ReturnType Name (Parameters)


– The Name is usually a verb that describes what
the method does. It is usually in proper case.
(This means that normally named constants are
all upper case, variables are camel case and
methods are proper case)
– The Parameters are optional and include the type
of variable that will be passed in to the Method.
Let’s review the
Write and WriteLine Methods

 Simple format for a single variable:


– Console.WriteLine (Variable Name)
 Also
– Console.WriteLine (Format String, Variable List)
– The two different formats means that the WriteLine method is
Overloaded!!
 Write and WriteLine do the same thing
– The only difference is that the WriteLine method includes a CRLF
(Carriage Return/Line Feed) at the end of the variables.
 The Format String is any text (or other string variables) you
want to print out along with format codes
– {#,W:F}
– # = variable number, W = width (optional) F = Format
Format String Syntax
 The Format String is the first parameter to the
Write/WriteLine Method
 It is a single string. Each set of curly brackets
represents the format of one variable (left most
variable is {0}, then {1}, etc.)
 The string can also have regular text embedded
within in for context (similar to the C fprint command
– const int INCHES_PER_FOOT = 12;
– int nFeet = 3;
– int nInches = nFeet * INCHES_PER_FOOT;
– Console.WriteLine ("{0} Feet = {1} Inches",nFeet,nInches);
Console Write Statement Examples
Formatting Output
Formatting Output (continued)
Escape Sequences
 There are certain “things” that we need to send to the console that “can’t” be
directly entered into a string. To “fix” this
– \n = newline
– \t = tab
– \b = backspace
– \r = return (no line feed)
– \\ = just the backslash
– \’ = print a single quote (otherwise thinks it is a char)
– \" = print a double quote (otherwise thinks it is a string)

 These are active on string assignment statements unless the literal is preceded
by an @:
– String sString1 = “This \t is \t a \t String”;
– String sString2 = @” This \t is \t a \t String”;
There are Three Methods in
System.Console for Reading

 Console.Read() – Returns an integer with the


Unicode value of the next character entered
on the keyboard
 Console.ReadKey() – Returns a char with the
next character entered
 Console.ReadLine() – Returns whatever a
user enters up to a CR/LF into a String
 None of these Methods are Overloaded.
Once you get it in to ReadLine
Parse It

 Predefined static method (Utility. Doesn’t rely on any other


information from the class!)
 All numeric types have a Parse( ) method
– double.Parse("string number")
– int.Parse("string number")
– bool.Parse("string number")
 Expects string argument
– Argument must be a number inside a string
– Returns the number (or bool)
In-Class Assignment

 Modify your Average Grade Score program


so that the Average is computed using a
method instead of directly within the main
routine.
– The method should accept three parameters with
the grade for each test.
– The method should return a decimal number.

You might also like