28
ENGR/CS 101 CS Session Lecture 3 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 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) Lecture 3 ENGR/CS 101 Computer Science Session 1

ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

ENGR/CS 101 CS Session

Lecture 3

Log into Windows/ACENET (reboot if in

Linux)

Start Microsoft Visual Studio 2010

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)

Lecture 3 ENGR/CS 101 Computer Science Session 1

Page 2: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 2

Page 3: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

Program Specification

Work our way up to a GUI program to do the

Caesar shift cipher. 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 3

Page 4: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

Program Analysis & 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 4

Page 5: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 5

Page 6: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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

Lecture 3 ENGR/CS 101 Computer Science Session 6

Page 7: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

Source Code to

Running Program

EDITOR -> source code 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 7

Page 8: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

Microsoft Visual Studio

Microsoft Visual Studio is an IDE for

developing applications for Windows in

multiple programming languages

We will be using C#.

Lecture 3 ENGR/CS 101 Computer Science Session 8

Page 9: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 9

Page 10: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

New Project Dialog

Lecture 3 ENGR/CS 101 Computer Science Session 10

Make sure this is

on your network

drive!

Page 11: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

C# Programs

The C# IDE tries to be helpful by creating the

parts of code that all C# program have. This

includes:

using statements that cause (pre-defined) method

names in libraries like System to become known. (A

method is the same thing as a function.)

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.

Lecture 3 ENGR/CS 101 Computer Science Session 11

Page 12: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

MS VS Project Window

Lecture 3 ENGR/CS 101 Computer Science Session 12

Project code goes here!

Page 13: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 in next class

Lecture 3 ENGR/CS 101 Computer Science Session 13

Page 14: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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!")

Lecture 3 ENGR/CS 101 Computer Science Session 14

Page 15: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 15

Page 16: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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

Lecture 3 ENGR/CS 101 Computer Science Session 16

Page 17: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

Assignments and Expressions

Examples: shiftKey = 'I';

plainLetter = shiftKey;

shiftNumber = shiftKey - 'A';

shiftKey = char.Parse(System.Console.ReadLine());

Lecture 3 ENGR/CS 101 Computer Science Session 17

Page 18: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 18

Page 19: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 19

Page 20: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 20

Page 21: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 21

Page 22: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 22

Page 23: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 23

Page 24: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

Review:

Program Analysis and Design

Identify the data and types being

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 24

Page 25: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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

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 25

Page 26: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

Putting the Code Together

// Continuation from previous slide

// 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 26

Page 27: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

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 27

Page 28: ENGR/CS 101 CS Session Lecture 3 - University of Evansvilleuenics.evansville.edu/~hwang/f12-courses/engrcs101/lecture03-slide… · Examples: C/C++, Java, Pascal, COBOL, Fortran Other

Running a 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 28