ENGR/CS 101 CS Session Lecture 3 Start Microsoft Visual Studio 2010

advertisement
ENGR/CS 101 CS Session
Lecture 3

Log into Windows/ACENET (reboot if in Linux)

Start Microsoft Visual Studio 2010



Lecture 3
Windows button -> All Programs ->
02 Programming -> Microsoft Visual Studio 2010 ->
Microsoft Visual Studio 2010
Choose C# as default environment, click Start
Visual Studio button
Wait for a long time... (next time should be faster)
ENGR/CS 101 Computer Science Session
1
Registration Session



The registration session has been moved to
next class, Wednesday, November 2 (instead
November 7 in the syllabus)
Students should try to pre-register for courses.
Guidebooks are available in EECS office.
Students will meet in groups based on major:

CS students - KC-267 with Dr. Hwang

CoE students - KC-137 with Dr. Mitchell

EE students - KC-136 with Dr. Howe
Lecture 3
ENGR/CS 101 Computer Science Session
2
Outline

Program specification

Program design

Programming languages

Using MS Visual Studio

C# programming language

Types and variables

Assignment and expressions

Input and output
Lecture 3
ENGR/CS 101 Computer Science Session
3
Program Specification

Work our way up to a GUI program to apply the
Vigenere cipher to a text file. Today's console
program will do the following:

Ask the user for an uppercase key letter (to
represent shift A-> key) and an uppercase letter to
encipher with this shift

Output the corresponding ciphertext letter

Example run (user input in bold):
Enter an uppercase key letter: I
Enter an uppercase letter to encipher: G
The corresponding ciphertext letter is: O
Lecture 3
ENGR/CS 101 Computer Science Session
4
Program Design


How will the program accomplish the
specifications?
Identify the data being used


shift key, plaintext letter, ciphertext letter
Write the steps of an algorithm
1. Get the shift key letter from the user
2. Get the plaintext letter from the user
3. Compute the ciphertext letter
4. Output the ciphertext letter to the screen
Lecture 3
ENGR/CS 101 Computer Science Session
5
Programming Languages

Syntax: What are the legal "sentences" in the
language?

Semantics: What do the "sentences" mean?

Compilers and interpreters enforce syntax.


Semantics determine whether the computation
is correct.
A program is not "working" if it gives the wrong
results!
Lecture 3
ENGR/CS 101 Computer Science Session
6
Compiling vs. Interpreting

Some languages are compiled with a program
called a compiler. Source code file is
translated into a machine code file.


Examples: C/C++, Java, Pascal, COBOL, Fortran
Other languages are interpreted. An
interpreter is a program that receives
programming language statements and
executes them directly.

Lecture 3
Examples: (original) BASIC, LISP, Prolog, LOGO
ENGR/CS 101 Computer Science Session
7
Source Code to Running Program

EDITOR -> programming language source file
-> COMPILER -> object file (+ libraries)
-> LINKER -> executable file
-> LOADER -> running program

Sometimes programs are run individually;
sometimes all work together in an Integrated
Development Environment (IDE)
Lecture 3
ENGR/CS 101 Computer Science Session
8
Microsoft Visual Studio



Microsoft Visual Studio is an IDE for developing
applications for Windows in multiple
programming languages, including C#.
Free Express versions of each individual
compiler are available. Link to the download
page on the CS session webpage.
EECS students can get the full version through
UE's MS Alliance program (see Jeff Cron).
Lecture 3
ENGR/CS 101 Computer Science Session
9
Creating a Console Project



All C# code belongs to a project. Start with
New -> Project
Each project produces a particular kind of
application. We will be creating a console
application.
After selecting the console application template,
set the Name box to "cs101console". Make
sure the Location is on your network drive,
then click OK. A large text window and a
Solution Explorer panel will appear.
Lecture 3
ENGR/CS 101 Computer Science Session
10
New Project Dialog
Make sure this is on
your network drive!
Lecture 3
ENGR/CS 101 Computer Science Session
11
C# Programs

The C# IDE tries to be helpful by creating the
parts of code that all C# program have. This
includes:



Lecture 3
using statements that cause (pre-defined) method
names in libraries like System to become known
namespace and class definition names based on
the project name given
a stub for the Main( ) method. The main program
code goes in this stub. It is the code that is
executed first when a program is run.
ENGR/CS 101 Computer Science Session
12
MS VS Project Window
Project code goes here!
Lecture 3
ENGR/CS 101 Computer Science Session
13
C# Programming Language

Developed by Microsoft for .NET framework

Syntax similar to C++ and Java

Semantics similar to Java

Object-oriented - won't cover in this class

Built-in support to make GUIs (Graphical User
Interfaces) - will look at this next class
Lecture 3
ENGR/CS 101 Computer Science Session
14
Types and Variables



A variable is a named memory location that
holds a value. All memory is in bits.
A variable has a type that determines how the
bits are interpreted into a value. Numbers are
in binary. Characters are mapped to binary
numbers. E.g., ASCII or Unicode.
C# types include



Lecture 3
int for integers (e.g., 5, -25, 0)
char for characters (e.g. 'A', 'b', '7', '%')
string (e.g. "Hello!")
ENGR/CS 101 Computer Science Session
15
Types and Variables

Variables are declared by giving type and name

Syntax is: <type> <var1>, <var2>, ..., <varn>;

Examples:
char shiftKey, // key letter plainLetter, // user input
cipherLetter;// result
int shiftNumber, // # of shift places
index; // of cipher letter

// marks the beginning of a comment to the
end of the line
Lecture 3
ENGR/CS 101 Computer Science Session
16
Assignment and Expressions



Assignment means to store a value into a
variable.
Syntax is: <var> = <expression>;
The expression is evaluated and the result is
stored in the variable. An expression can be:




Lecture 3
A literal. E.g., 12 or 'A'
A variable. E.g., shiftNumber
A function call. (More on this later.)
An expression of one or more literals, variables, or
function calls.
ENGR/CS 101 Computer Science Session
17
Assignment and Expressions

Examples:
shiftKey = 'I';
plainLetter = shiftKey;
shiftNumber = shiftKey ­ 'A';
shiftKey =
char.Parse(System.Console.ReadLine());
Lecture 3
ENGR/CS 101 Computer Science Session
18
Computing the Cipher Letter


Assume that variable shiftKey holds the key
letter and variable plainLetter holds the
letter to be enciphered.
Since the alphabetic characters have
sequential mapping (i.e., 'A' is first, followed by
'B', etc.), the number of places to shift is the
key letter minus 'A'. In C# code, this is:
shiftNumber = shiftKey ­ 'A';
Lecture 3
ENGR/CS 101 Computer Science Session
19
Computing the Cipher Letter


To find the cipher letter, we determine the
index of plaintext letter (i.e., where in the
alphabet it is when we start counting at 0) using
a similar method, then add the shift number.
This will be the index of the ciphertext letter,
except that the number may be greater than 26.
To make it circular, we compute the modulus
with respect to 26. In code, this is:
index = (plainLetter ­ 'A' + shiftNumber) % 26;

The modulus operator symbol is %
Lecture 3
ENGR/CS 101 Computer Science Session
20
Computing the Cipher Letter


Now we add this new index back to 'A' to find
the ciphertext letter.
However, C# is strict about types and is
unhappy that we are trying to add a number to
a character, so we have to tell the compiler to
treat 'A' as a number, then treat the result as a
character by casting. The code becomes:
cipherLetter = (char) ((int) 'A' + index);
Lecture 3
ENGR/CS 101 Computer Science Session
21
Output

C# output is done by calling a built-in function
that takes a string as an argument. There are
two forms:



System.Console.Write ( ) - displays string to screen
System.Console.WriteLine ( ) - displays string to
screen followed by a newline character
Strings can created by concatenating the items
to be displayed using +.
Lecture 3
ENGR/CS 101 Computer Science Session
22
Output

Examples:
System.Console.Write ("Enter an uppercase key letter: ");
System.Console.WriteLine ("The corresponding ciphertext letter is: " + cipherLetter); Lecture 3
ENGR/CS 101 Computer Science Session
23
Input

C# input is done by calling a built-in function
that has no arguments and returns a string that
contains what the user typed in.


System.Console.Read( )
Since all input is in the form of a string, it must
be converted to the appropriate type before
assignment. Each type has a Parse function
for this purpose. E.g.
shiftKey = char.Parse(System.Console.Read());
Lecture 3
ENGR/CS 101 Computer Science Session
24
Review: Program Design

Identify the data being used - what are their
types?


shift key (char), plaintext letter (char), ciphertext
letter (char), shift number (int), index (int)
Write the steps of an algorithm
1. Get the shift key letter from the user
2. Get the plaintext letter from the user
3. Compute the ciphertext letter - more details
- Compute shift number
- Compute index of ciphertext letter
- Compute the ciphertext letter
4. Output the ciphertext letter to the screen
Lecture 3
ENGR/CS 101 Computer Science Session
25
Putting the Code Together
// This code goes in the place indicated on Slide 12
// Variable declarations
char shiftKey, // key letter plainLetter, // user input
cipherLetter;// result
int shiftNumber, // # of shift places
index; // of cipher letter
// Get the key letter and a letter to encipher from user
System.Console.Write("Enter an uppercase key letter: ");
shiftKey = char.Parse(System.Console.ReadLine());
System.Console.Write
("Enter an uppercase letter to encipher: ");
plainLetter = char.Parse(System.Console.ReadLine());
Lecture 3
ENGR/CS 101 Computer Science Session
26
Putting the Code Together
// Compute the corresponding ciphertext letter
shiftNumber = shiftKey ­ 'A';
index = (plainLetter ­ 'A' + shiftNumber) % 26;
cipherLetter = (char)((int)'A' + index);
// Display the result
System.Console.WriteLine
("The corresponding ciphertext letter is: "
+ cipherLetter);
Lecture 3
ENGR/CS 101 Computer Science Session
27
Building a Program


To build (i.e., compile) the program do Build ->
Build Solution
If there are no syntax errors, great! If there are
syntax errors, they will be listed at the bottom of
the screen. Correct and build again.
Lecture 3
ENGR/CS 101 Computer Science Session
28
Running a Console Program


To run the
program, do
Debug -> Start
Without
Debugging.
This will start
the console
window and run
the program.
Lecture 3
ENGR/CS 101 Computer Science Session
29
Download