engr cs 101 cs session
play

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

ENGR/CS 101 CS Session Lecture 9 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2012 Any questions about the exercise from last time? Lecture 4 ENGR/CS 101 Computer Science Session 1 Outline


  1. ENGR/CS 101 CS Session Lecture 9  Log into Windows/ACENET (reboot if in Linux)  Start Microsoft Visual Studio 2012  Any questions about the exercise from last time? Lecture 4 ENGR/CS 101 Computer Science Session 1

  2. Outline  Problem: Encode a single letter using Caesar shift cipher  Substitution Cipher Project  Textboxes  Getting user input  Computing ciphertext letter  Displaying result Lecture 4 ENGR/CS 101 Computer Science Session 2

  3. Problem Specification  Will work our way up to a program to do the Caesar shift cipher on messages in a file. Today's program will do the following:  Allow the user to enter a shift key letter in uppercase  Allow the user to enter a plaintext letter in uppercase to be enciphered  Have a button for the user to click to have the plaintext letter enciphered  Display the ciphertext letter Lecture 4 ENGR/CS 101 Computer Science Session 3

  4. Interface Mockup  Textboxes to enter plaintext and shift key, and to display ciphertext  Button to encipher plaintext.  Labels to identify the textboxes Lecture 4 ENGR/CS 101 Computer Science Session 4

  5. Create the GUI  Create a new project. Make sure the language template is Visual C#.  Select " Windows Form Application ". Give the project a name like "SubstitutionCipher".  Make sure the location is on your network drive.  Click on OK. You will get the GUI designer with a blank form. Change the Text property to something like "Substitution Cipher". Lecture 4 ENGR/CS 101 Computer Science Session 5

  6. Textboxes  A Textbox is a GUI element that can be used to get text input from a user. It also can be used to display results. (This is better than a label for larger amounts of output.)  The text in a Textbox is accessed through the Text property. Lecture 4 ENGR/CS 101 Computer Science Session 6

  7. In-class Exercise, Part 1  Create a simple form with three Textboxes for the plaintext, ciphertext, and shift key, and a Button for doing the encipher.  For textboxes containing the plaintext and the ciphertext, change the Multiline property to True . After this, pull on the handles to make them as large as you like.  Label and arrange these elements however you like on the form. Change properties like color and font as you wish. Lecture 4 ENGR/CS 101 Computer Science Session 7

  8. In-class Exercise, Part 1  Change the Name properties of the GUI elements as follows:  Plaintext textbox is named plaintextBox  Ciphertext textbox is named ciphertextBox  Shift key textbox is named keyBox  Encipher button is named encipherBtn Lecture 4 ENGR/CS 101 Computer Science Session 8

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

  10. Application Data  Double-click on the Encipher button to create the handler method stub.  Unlike the Dot Chaser program, all of the action happens within the handler when the Encipher button is clicked.  Declare the application data in the handler char shiftKey, // key letter plainLetter, // user input cipherLetter; // result Lecture 4 ENGR/CS 101 Computer Science Session 10

  11. Handler Design  The specific design of the handler code for the Encipher button is as follows 1. Clear the ciphertext box 2. Get the shift key from keyBox.Text 3. Get the plaintext letter from plaintextBox.Text 4. Compute the ciphertext letter 5. Append the cipher letter to the ciphertext box  Clearing the ciphertext box is simply setting the Text property of the empty string. ciphertextBox.Text = ""; Lecture 4 ENGR/CS 101 Computer Science Session 11

  12. Getting User Input  We will get the user data from the keyBox and plaintextBox using the Text property like so:  keyBox.Text  plaintextBox.Text  Since the 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(keyBox.Text); plainLetter = char.Parse(plaintextBox.Text); Lecture 4 ENGR/CS 101 Computer Science Session 12

  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'; Lecture 3 ENGR/CS 101 Computer Science Session 13

  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: index = (plainLetter - 'A' + shiftNumber) % 26;  The modulus operator symbol is % Lecture 3 ENGR/CS 101 Computer Science Session 14

  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) ((int) 'A' + index); Lecture 3 ENGR/CS 101 Computer Science Session 15

  16. Displaying the Result  To display the result, it must be placed in the Text property of the ciphertext box.  In preparation for the next phase of the project, we will do this by concatenating the resulting cipher letter to the ciphertext box Text. ciphertextBox.Text += cipherLetter; Lecture 4 ENGR/CS 101 Computer Science Session 16

  17. In-class Exercise, Part 2  Double-click on the Encipher button to get to the handler function stub, if you haven't done so already.  Finish writing the code for the Encipher button handler. All of the code is shown together on the next two slides. Lecture 4 ENGR/CS 101 Computer Science Session 17

  18. Encipher Button Handler Code // Application data char shiftKey, // key letter plainLetter, // user input cipherLetter; // result int shiftNumber, // # of shift places index; // of cipher letter // Clear the ciphertext (result) box ciphertextBox.Text = ""; Lecture 4 ENGR/CS 101 Computer Science Session 18

  19. Encipher Button Handler Code // Get the key letter and a letter to encipher shiftKey = char.Parse(keyBox.Text); plainLetter = char.Parse(plaintextBox.Text); // Compute the corresponding ciphertext letter shiftNumber = shiftKey - 'A'; index = (plainLetter - 'A' + shiftNumber) % 26; cipherLetter = (char)((int)'A' + index); // Display the result // Append the enciphered letter to ciphertext box ciphertextBox.Text += cipherLetter; Lecture 4 ENGR/CS 101 Computer Science Session 19

  20. Test the Program  When you have competed writing the handler code, try running it with different keys and input.  What happens if you try to input an entire word in the plaintext box?  Next class we will enhance the project to handle encoding an entire word. Lecture 4 ENGR/CS 101 Computer Science Session 20

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