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

engr cs 101 cs session
SMART_READER_LITE
LIVE PREVIEW

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

ENGR/CS 101 CS Session Lecture 5 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 Has everyone finished the exercise from last time so that their program will encipher one uppercase letter? Lecture 5


slide-1
SLIDE 1

ENGR/CS 101 CS Session Lecture 5

 Log into Windows/ACENET (reboot if in

Linux)

 Start Microsoft Visual Studio 2010  Has everyone finished the exercise from last

time so that their program will encipher one uppercase letter?

Lecture 5 ENGR/CS 101 Computer Science Session 1

slide-2
SLIDE 2

Outline

 Problem: input more than one letter at a time  C# programming language

 Strings and indexing  For-loops

 Problem: input more than one word at a time  C# programming language

 If-statements

Lecture 5 ENGR/CS 101 Computer Science Session 2

slide-3
SLIDE 3

Problem: Input Words

 Today we will modify

  • ur GUI program to

accept and encipher whole words in uppercase letters, rather than just a single letter.

Lecture 5 ENGR/CS 101 Computer Science Session 3

slide-4
SLIDE 4

Strings

 Enciphering one letter at a time is tedious.  Want to handle an entire word.  Design: Encipher each letter in the plaintext

  • box. Need to use a string, rather than a

character.

 A string is a sequence of characters. The

Text property of a textbox is a string. E.g., we can access it using: plaintextBox.Text;

Lecture 5 ENGR/CS 101 Computer Science Session 4

slide-5
SLIDE 5

Indexing

 Individual characters are accessed by giving

the index of the character in the string.

 The syntax for accessing an individual

character is: <string var>[<index>].

 Example: plaintextBox.Text[2] is the

character 'E'

Lecture 5 ENGR/CS 101 Computer Science Session 5

'A' 'C' 'E' 'S' [0] [1] [2] [3]

slide-6
SLIDE 6

Indexing

 As shown on the previous slide, the indexes

  • f a string start at 0.

 We can use indexing to get the key letter,

  • too. It is the first character in the

keyBox.Text string. shiftKey = keyBox.Text[0];

Lecture 5 ENGR/CS 101 Computer Science Session 6

slide-7
SLIDE 7

For-Loops

 An index used to access string characters may

be any integer expression. In particular, it may be a variable. So we can make a variable count from 0 to the last index of the string to access each character one at a time.

 We do this with a for-loop (also called a

counting loop). A for-loop consists of 4 parts:

 A loop control variable and its initial value  A loop condition test of the loop control variable  A loop control variable update  A body of steps that are repeated

Lecture 5 ENGR/CS 101 Computer Science Session 7

slide-8
SLIDE 8

For-Loops

 For our Encipher handler design, we would use

the for-loop in the following way (changes in bold).

  • 1. Clear ciphertext box
  • 2. Get the shift key from keyBox.Text[0]
  • 3. For index i from 0 to length of plaintextBox.Text by 1

3.1. Get the plaintext letter from plaintextBox.Text[i] 3.2. Compute the corresponding ciphertext letter 3.3. Append the ciphertext letter to the ciphertext box.

Lecture 5 ENGR/CS 101 Computer Science Session 8

slide-9
SLIDE 9

For-Loops

 Some notes:

 Clearing the ciphertext box and getting the shift

key need to be done only once, so they happen before the loop starts.

 The loop control variable is i, with an initial value

  • f 0. It is updated by adding 1 to i until it reaches

the length of the plaintextBox.Text string.

Lecture 5 ENGR/CS 101 Computer Science Session 9

slide-10
SLIDE 10

For-Loops

 More notes:

 The length of a string is obtained using the

Length property, accessed with the dot notation (e.g., plaintextBox.Text.Length).

 The loop body should compute the ciphertext

letter and appending it to the ciphertext box in the same way as before.

Lecture 5 ENGR/CS 101 Computer Science Session 10

slide-11
SLIDE 11

For-Loops

 The syntax of a for-loop in C# is:

for ( <lcv declaration/initialization>; <loop condition>; <lcv update statement>) { <steps to be repeated> }

 For our program, we would write:

for (int i = 0; i < plaintextBox.Text.Length; i++) { // steps to be repeated }

Lecture 5 ENGR/CS 101 Computer Science Session 11

slide-12
SLIDE 12

For-Loops

 Code notes:

 Since indexing starts at 0, the length of a string is

  • ne more than the index of the last character of

the string.

 E.g., "ACES" is a 4-letter word with indexes 0-3.  Thus the loop condition uses < rather than <=.

More on conditions later.

Lecture 5 ENGR/CS 101 Computer Science Session 12

slide-13
SLIDE 13

In-class Exercise, Part 1

 Modify the GUI program to encipher a word

  • f plaintext and display the ciphertext using a

for-loop (code shown on the next 3 slides):

 Get the shiftKey using indexing instead of

char.Parse( )

 Add a for-loop around the statements that

correspond to steps 3.1 - 2.3 on Slide 8. Note that the body of the for-loop is mostly the same code as before, except for getting plainLetter using indexing instead of char.Parse( )

Lecture 5 ENGR/CS 101 Computer Science Session 13

slide-14
SLIDE 14

Putting the Code Together

// The modified parts of the program are bold // Everything is the same on this page. // Variable declarations char shiftKey, // key letter plainLetter, // user input cipherLetter;// result int shiftNumber, // # of shift places index; // of cipher letter // Clear the result box ciphertextBox.Text = "";

Lecture 5 ENGR/CS 101 Computer Science Session 14

slide-15
SLIDE 15

Putting the Code Together

// Use indexing to get the shift key shiftKey = keyBox.Text[0]; // For each letter in plaintext box, indexed by i for (int i = 0; i < plaintextBox.Text.Length; i++) { // Use indexing to get the plaintext letter plainLetter = plaintextBox.Text[i]; // Compute the corresponding ciphertext letter // This is the same as before shiftNumber = shiftKey - 'A'; index = (plainLetter - 'A' + shiftNumber) % 26; cipherLetter = (char)((int)'A' + index);

Lecture 5 ENGR/CS 101 Computer Science Session 15

slide-16
SLIDE 16

Putting the Code Together

// This is still part of loop body // Append the enciphered letter to ciphertext box // This is the same as before ciphertextBox.AppendText(cipherLetter.ToString()); } // don't forget the closing curly brace!!

Lecture 5 ENGR/CS 101 Computer Science Session 16

slide-17
SLIDE 17

In-class Exercise, Part 1

 Test your program with the string "ACES" =>

"IKMA"

 See what happens if you type in "GO ACES!"

We'll fix this "problem" next.

Lecture 5 ENGR/CS 101 Computer Science Session 17

slide-18
SLIDE 18

Problem: Input Sentences

 Now we will modify

  • ur GUI program to

accept and encipher whole sentences in uppercase letters, rather than just a single word.

Lecture 5 ENGR/CS 101 Computer Science Session 18

slide-19
SLIDE 19

If-Statements

 To prevent the program from enciphering

non-uppercase letter characters, we want to execute the shifting code only when the plaintext letter is in the uppercase alphabet.

 We do this with an if-statement.

Lecture 5 ENGR/CS 101 Computer Science Session 19

slide-20
SLIDE 20

If-Statements

 An if-statement has:

 A condition test  A body to execute when the test is true  An optional body to execute when the test is false

 We used it to decide whether to encipher a

character as follows:

  • 1. If the plaintext letter is an uppercase letter

1.1 Compute corresponding ciphertext letter Else 1.2 Set ciphertext letter to the plaintext letter

Lecture 5 ENGR/CS 101 Computer Science Session 20

slide-21
SLIDE 21

Conditions

 A condition is an expression that is either true

  • r false

 They are formed using operators == equal to

!= not equal to < less than <= less than or equal to > greater than >= greater than or equal to && logical AND - true is both operands are true || logical OR - true if one of the operands is true

Lecture 5 ENGR/CS 101 Computer Science Session 21

slide-22
SLIDE 22

If-Statements

 Example: test if a letter is uppercase (('A' <= letter) && (letter <= 'Z'))  The syntax of a C# if-statement is: if (<condition>) { <body to execute when condition is true> } else // this part is optional { <body to execute when condition is false> }

Lecture 5 ENGR/CS 101 Computer Science Session 22

slide-23
SLIDE 23

In-class Exercise, Part 2

 Modify the GUI program to encipher a sentence

  • f plaintext and display the ciphertext using an if-

statement around the cipher letter computation in the for-loop (code shown on the next slide)

 Test your program with the string "GO ACES!"

=> "OW IKMA!"

 See what happens if you type in "Go Aces!"

We'll cover error checking and enciphering lowercase letters next class. We'll also add deciphering to the program.

Lecture 5 ENGR/CS 101 Computer Science Session 23

slide-24
SLIDE 24

Putting the Code Together

// Put an if statement around the ciphertext // letter computation. New stuff in bold. if (('A' <= plainLetter) && (plainLetter <= 'Z')) { // Compute the ciphertext letter shiftNumber = shiftKey - 'A'; index = (plainLetter - 'A' + shiftNumber) % 26; cipherLetter = (char)((int)'A' + index); } else // do not encipher { cipherLetter = plainLetter; }

Lecture 5 ENGR/CS 101 Computer Science Session 24