engr cs 101 cs session lecture 4
play

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

ENGR/CS 101 CS Session Lecture 4 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 Find your project from last class Did everyone finish the program from last class to the point that enciphers


  1. ENGR/CS 101 CS Session Lecture 4  Log into Windows/ACENET (reboot if in Linux)  Start Microsoft Visual Studio 2010  Find your project from last class  Did everyone finish the program from last class to the point that enciphers everything in a line? Lecture 4 ENGR/CS 101 Computer Science Session 1

  2. Outline  Problem: preserve the spaces and non-letter characters from the plaintext in the ciphertext  Problem: input more than one line at a time  C# programming language  If-statements and conditions  While loops Lecture 4 ENGR/CS 101 Computer Science Session 2

  3. Program Specification  Today's program first will do the following:  Ask the user for an uppercase key letter (to represent shift A-> key) and an uppercase line to encipher with this key.  Display the corresponding ciphertext for each line. The output should preserve whitespace and non-alphabetic characters.  Example run (user input in bold ): Enter an uppercase key letter: I Enter an uppercase line to encipher: GO ACES! The corresponding ciphertext line is: OW IKMA! Lecture 4 ENGR/CS 101 Computer Science Session 3

  4. If-Statements  While our last program accepted any line of plaintext, the spaces and non-letter characters also are enciphered.  To prevent this from happening, we want to execute the shifting code in the ShiftLetter function only when the received letter is in the uppercase alphabet.  Do this with an if-statement . Lecture 3 ENGR/CS 101 Computer Science Session 4

  5. 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  The syntax of an 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 3 ENGR/CS 101 Computer Science Session 5

  6. Conditions  A condition is an expression that is either true or 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  Example: test if a letter is uppercase (('A' <= letter) && (letter <= 'Z')) Lecture 3 ENGR/CS 101 Computer Science Session 6

  7. If-Statements  Here is our modified ShiftLetter function; the new code is in bold . private static char ShiftLetter (char letter, int shiftNumber) { char cipherLetter = letter ; // start with the letter // test if letter is an uppercase alphabetic char if (('A' <= letter) && (letter <= 'Z')) { cipherLetter = (char) (((letter - 'A' + shiftNumber) % 26) + (int) 'A'); } return cipherLetter; } Lecture 3 ENGR/CS 101 Computer Science Session 7

  8. In-class Exercise, Part 1  Modify the ShiftLetter function so that it only computes the cipher letter for uppercase alphabetic characters.  Test your program on "GO ACES!" => "OW IKMA!"  Bonus: Modify the ShiftLetter function so that it will compute the cipher letter for lowercase alphabetic characters as well. Some notes:  The formula for lowercase letters is the same as for the uppercase letters except that 'a' and 'z' are used instead of 'A' and 'Z'. Lecture 3 ENGR/CS 101 Computer Science Session 8

  9. Program Specification  Now we will modify today's program to do the following:  Ask the user for an uppercase key letter (to represent shift A-> key) then repeatedly ask for an uppercase line to encipher with this key until the user types ".."  Display the corresponding ciphertext for each line. The output should preserve whitespace and non-alphabetic characters.  Example run (user input in bold ): Enter an uppercase key letter: I Enter an uppercase line to encipher (.. to quit): GO ACES! The corresponding ciphertext line is: OW IKMA! Enter an uppercase line to encipher (.. to quit): * CS IS FUN* The corresponding ciphertext line is: *KA QA NCV* Enter an uppercase line to encipher (.. to quit): .. Lecture 4 ENGR/CS 101 Computer Science Session 9

  10. Program Design  Identify the data being used  No new data  Write the steps an algorithm 1. Ask user for shiftKey 2. Ask user for plainText 3. Compute shiftNumber = shiftKey - 'A' 4. While plainText is not ".." 4.1 Output results label 4.2. For each letter in plainText 4.2.1. Compute the corresponding cipherLetter 4.2.2. Output the cipherLetter to the screen 4.3. Output a newline 4.4. Ask user for plainText Lecture 4 ENGR/CS 101 Computer Science Session 10

  11. While-Loops  For indexing a string, we know how many times we want to repeat the encipher action, so we use a for-loop  We don't know when a user may enter "..", so we need to use a while-loop to implement this behavior.  The syntax for the while loop is: while (<condition>) { // do something } Lecture 4 ENGR/CS 101 Computer Science Session 11

  12. While-Loops  The condition of the while-loop is tested and if it is true, the body of the loop is executed. When the condition becomes false, execution continues with the statement after the loop.  For our program, if the user types in anything except "..", we want to encipher the plaintext. We can do this by using the following condition: while (plainText != "..") Lecture 4 ENGR/CS 101 Computer Science Session 12

  13. In-class Exercise, Part 2  Modify the program to repeatedly ask the user for a line to encipher until the user enters ".."  Some notes:  The while-loop starts after the first time we ask the user for the line to encipher.  The rest of main program code goes in the while- loop body.  Asking the user for the next line to encipher must be added to the end of the while-loop body, so that it is repeated. Lecture 4 ENGR/CS 101 Computer Science Session 13

  14. Putting the Code Together  Modified or added code in bold. This page is the same except for the prompt. // Variable declarations char shiftKey, // key letter cipherLetter; // result int shiftNumber; // # of places to shift string plainText; // user input // Ask the user for key letter and a line to encipher System.Console.Write("Enter an uppercase key letter: "); shiftKey = char.Parse(System.Console.ReadLine()); System.Console.Write ("Enter an uppercase line to encipher (.. to quit) : "); plainText = System.Console.ReadLine(); Lecture 4 ENGR/CS 101 Computer Science Session 14

  15. Putting the Code Together  This page has the added while-loop header // Compute the # of places to shift shiftNumber = shiftKey - 'A'; while (plainText != "..") { // Display label for output System.Console.WriteLine ("The corresponding ciphertext line is: "); // For each letter in plainText for (int i = 0; i < plainText.Length; i++) { // Compute the corresponding cipher letter cipherLetter = ShiftLetter(plainText[i], shiftNumber); Lecture 4 ENGR/CS 101 Computer Science Session 15

  16. Putting the Code Together  This page has the end of the while-loop //This is the continuation of the for-loop // Display the cipherLetter System.Console.Write(cipherLetter); } // end of for-loop // Display a newline System.Console.WriteLine(); // Ask user for next line to encipher System.Console.Write ("Enter an uppercase line to encipher (.. to quit): "); plainText = System.Console.ReadLine(); } // end of while-loop Lecture 4 ENGR/CS 101 Computer Science Session 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend