ENGR/CS 101 CS Session Lecture 2 Log into Windows/ACENET (reboot if - - PowerPoint PPT Presentation

engr cs 101 cs session lecture 2
SMART_READER_LITE
LIVE PREVIEW

ENGR/CS 101 CS Session Lecture 2 Log into Windows/ACENET (reboot if - - PowerPoint PPT Presentation

ENGR/CS 101 CS Session Lecture 2 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 Windows button -> All Programs -> 02-Programming -> Microsoft Visual Studio 2010 Choose C# as default


slide-1
SLIDE 1

Lecture 2 ENGR/CS 101 Computer Science Session 1

ENGR/CS 101 CS Session Lecture 2

 Log into Windows/ACENET (reboot if in Linux)  Start Microsoft Visual Studio 2010

 Windows button -> All Programs ->

02-Programming -> Microsoft Visual Studio 2010

 Choose C# as default environment, click Start

Visual Studio button

 Wait for a long time... (next time should be faster)

slide-2
SLIDE 2

Lecture 2 ENGR/CS 101 Computer Science Session 2

Outline

 Program specification  Program design  Programming languages  C# programming language

 Types and variables  Assignment and expressions  Input and output

 Using MS Visual Studio

slide-3
SLIDE 3

Lecture 2 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

slide-4
SLIDE 4

Lecture 2 ENGR/CS 101 Computer Science Session 4

Program Design

 How will the program accomplish the

specifications?

 Identify the data being used

 shiftKey, plainLetter, cipherLetter

 Write the steps an algorithm

  • 1. Prompt user for shiftKey
  • 2. Read shiftKey from the keyboard
  • 3. Prompt user for plainLetter
  • 4. Read plainLetter from the keyboard
  • 5. Compute the cipherLetter
  • 6. Output the cipherLetter to the screen
slide-5
SLIDE 5

Lecture 2 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!

slide-6
SLIDE 6

Lecture 2 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.

 Examples: (original) BASIC, LISP, Prolog, LOGO

slide-7
SLIDE 7

Lecture 2 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)

slide-8
SLIDE 8

Lecture 2 ENGR/CS 101 Computer Science Session 8

C# Programming Language

 Developed by Microsoft for .NET framework  Syntax similar to C++ and Java  Semantics similar to Java  Object-oriented  Built-in support to make GUIs (Graphical User

Interfaces)

slide-9
SLIDE 9

Lecture 2 ENGR/CS 101 Computer Science Session 9

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

 int for integers (e.g., 5, -25, 0)  char for characters (e.g. 'A', 'b', '7', '%')  string (e.g. "Hello!")

slide-10
SLIDE 10

Lecture 2 ENGR/CS 101 Computer Science Session 10

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

 // marks the beginning of a comment to the

end of the line

slide-11
SLIDE 11

Lecture 2 ENGR/CS 101 Computer Science Session 11

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:

 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.

slide-12
SLIDE 12

Lecture 2 ENGR/CS 101 Computer Science Session 12

Assignment and Expressions

 Examples:

shiftKey = 'I'; plainLetter = shiftKey; shiftNumber = shiftKey - 'A'; shiftKey = char.Parse(System.Console.ReadLine());

slide-13
SLIDE 13

Lecture 2 ENGR/CS 101 Computer Science Session 13

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';

slide-14
SLIDE 14

Lecture 2 ENGR/CS 101 Computer Science Session 14

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:

(plainLetter - 'A' + shiftNumber) % 26

 The modulus operator symbol is %

slide-15
SLIDE 15

Lecture 2 ENGR/CS 101 Computer Science Session 15

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) (((plainLetter - 'A' + shiftNumber) % 26) + (int) 'A');

slide-16
SLIDE 16

Lecture 2 ENGR/CS 101 Computer Science Session 16

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 +.

slide-17
SLIDE 17

Lecture 2 ENGR/CS 101 Computer Science Session 17

Output

 Examples:

System.Console.Write ("Enter an uppercase key letter: "); System.Console.WriteLine ("The corresponding ciphertext letter is: " + cipherLetter);

slide-18
SLIDE 18

Lecture 2 ENGR/CS 101 Computer Science Session 18

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());

slide-19
SLIDE 19

Lecture 2 ENGR/CS 101 Computer Science Session 19

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 is available at http://www.microsoft.com/express/Downloads/

 EECS students can get the full version through

UE's MS Alliance program.

slide-20
SLIDE 20

Lecture 2 ENGR/CS 101 Computer Science Session 20

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 "cs101_program1", then click OK.

 A large text window and a Solution Explorer

panel will appear.

slide-21
SLIDE 21

Lecture 2 ENGR/CS 101 Computer Science Session 21

MS VS Project Window

Program code goes here!

slide-22
SLIDE 22

Lecture 2 ENGR/CS 101 Computer Science Session 22

Putting the Code Together

// Variable declarations char shiftKey, // key letter plainLetter, // user input cipherLetter;// result int shiftNumber; // # of shift places // Ask the user for key letter and a letter to encipher 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());

slide-23
SLIDE 23

Lecture 2 ENGR/CS 101 Computer Science Session 23

Putting the Code Together

// Compute the corresponding ciphertext letter shiftNumber = shiftKey - 'A'; cipherLetter = (char)(((plainLetter - 'A' + shiftNumber) % 26) + (int)'A'); // Display the result System.Console.WriteLine ("The corresponding ciphertext letter is: " + cipherLetter);

slide-24
SLIDE 24

Lecture 2 ENGR/CS 101 Computer Science Session 24

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.

slide-25
SLIDE 25

Lecture 2 ENGR/CS 101 Computer Science Session 25

Running a Console Program

 To run the

program, do Debug -> Start Without Debugging.

 This will start

the console window and run the program.