engr cs 101 cs session lecture 6
play

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

ENGR/CS 101 CS Session Lecture 6 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 Load project from last class Questions? Lecture 6 ENGR/CS 101 Computer Science Session 1 Outline Problem: Use a


  1. ENGR/CS 101 CS Session Lecture 6  Log into Windows/ACENET (reboot if in Linux)  Start Microsoft Visual Studio 2010  Load project from last class  Questions? Lecture 6 ENGR/CS 101 Computer Science Session 1

  2. Outline  Problem: Use a GUI to enter input from a file . Also prevent the program from trying to do an encipherment if the input is missing.  C# programming language  MessageBox  File Dialogs  File Streams  Deciphering the Caesar shift cipher Lecture 6 ENGR/CS 101 Computer Science Session 2

  3. Problem Specification  Today's program will be a GUI application. It will extend the program from the last class do the following:  Prevent the program from trying to do an encipherment when the input is missing.  Allow the user to load a plaintext message from a file by clicking a button.  Allow the user to save the ciphertext message to a file by clicking a button.  Bonus: extend the program to decipher ciphertext back to plaintext. Lecture 6 ENGR/CS 101 Computer Science Session 3

  4. Interface Mockup  Same as last time  Add buttons to load plaintext from a file, and save ciphertext to a file Lecture 6 ENGR/CS 101 Computer Science Session 4

  5. MessageBox  Often want to notify user when something is missing. E.g., not filling in shift key textbox before enciphering.  Use MessageBox to pop up a message, then return to go back to waiting. if (shiftKey.Text == "") { MessageBox.Show ("Missing shift key!"); return; // go back to waiting } Lecture 6 ENGR/CS 101 Computer Science Session 5

  6. In-class Exercise, Part 1  Add the code from the previous slide to the Encipher button handler so that the program does not try to do an encipherment when the shift key box is empty.  The code goes at the beginning of the handler function before any other code.  Bonus: add a second check that the user entered an uppercase letter in the shift key box. If not, the program should pop up an error message box and go back to waiting. Lecture 6 ENGR/CS 101 Computer Science Session 6

  7. Reading from a File  Another GUI element that can be created by a program is the open file dialog.  Declare a variable and create a new object using the C# new command OpenFileDialog fileChooser = new OpenFileDialog(); Lecture 6 ENGR/CS 101 Computer Science Session 7

  8. OpenFileDialog  To make the dialog pop up and get the file name from the user, call the ShowDialog function and save the result in a DialogResult variable: DialogResult result = fileChooser.ShowDialog();  Check if the user clicked "Cancel" and if so, go back to waiting // Check if user clicked Cancel if (result == DialogResult.Cancel) return; Lecture 6 ENGR/CS 101 Computer Science Session 8

  9. File Streams  A file stream is a program object that is opened (i.e., attached to a physical file) when it is created. StreamReader objects are used to read data from a file: // Open the file chosen in the dialog StreamReader fileInput = new StreamReader (fileChooser.FileName); // Read entire file contents into plainText box plainText.Text = fileInput.ReadToEnd(); fileInput.Close(); // Detach the physical file cipherText.Text = ""; // Clear cipherText box Lecture 6 ENGR/CS 101 Computer Science Session 9

  10. In-class Exercise, Part 2  Add a Load button to your program's interface  Double-click on the button to get the handler function shell.  Between the { }'s, type in the code from the previous 3 slides. I.e., starting with OpenFileDialog fileChooser = ... at the bottom of Slide 7.  Run and test your program until it works correctly. Lecture 6 ENGR/CS 101 Computer Science Session 10

  11. In-class Exercise, Part 3  Add a Save button to your program.  The handler should save the ciphertext to a file.  The save file dialog type is SaveFileDialog, but otherwise works exactly the same way as the OpenFileDialog as far as getting a file name from the user.  To write to a file, open a StreamWriter object with the chosen file name and write the cipherText contents to it. StreamWriter fileOutput = new StreamWriter (fileChooser.FileName); fileOutput.WriteLine (cipherText.Text); fileOutput.Close(); Lecture 6 ENGR/CS 101 Computer Science Session 11

  12. Deciphering  One of the reasons the Caesar shift cipher is not a good method for keeping secrets is that it is easy to decipher when you know the key.  We could decipher by writing a ShiftLetterBack function that is just like the ShiftLetter function except that it subtracts the shift number rather than adding it.  Unfortunately, the % (modulus) operator only works reliably on positive numbers. Lecture 6 ENGR/CS 101 Computer Science Session 12

  13. Deciphering  But in modular arithmetic, subtracting is the same as adding its complementary number (modulus base minus the number). In our case, this will be 26 minus the shift number.  For example, shift key 'I', gives a shift number of 8. The complementary shift number for deciphering is 26 - 8 = 14. (This is equivalent to a shift key of 'S'.)  This means that we can use the ShiftLetter function to decipher by giving it the complementary shift number. Lecture 6 ENGR/CS 101 Computer Science Session 13

  14. Deciphering  Bonus exercise: Add a Decipher button and handler to your program that deciphers the text in the ciphertext box and put the plaintext result in the plaintext box.  The Decipher button handler will be the same as the Encipher button handler except  it computes the complementary shift number to pass to the ShiftLetter function  it reverses the use of the plainText and cipherText boxes. Lecture 6 ENGR/CS 101 Computer Science Session 14

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