Lecture 6 ENGR/CS 101 Computer Science Session 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?
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
Lecture 6 ENGR/CS 101 Computer Science Session 1
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 2
Problem: Use a GUI to enter input from a file.
C# programming language
MessageBox File Dialogs File Streams
Deciphering the Caesar shift cipher
Lecture 6 ENGR/CS 101 Computer Science Session 3
Today's program will be a GUI application. It
Prevent the program from trying to do an
Allow the user to load a plaintext message from a
Allow the user to save the ciphertext message to a
Bonus: extend the program to decipher
Lecture 6 ENGR/CS 101 Computer Science Session 4
Same as last time Add buttons to load
Lecture 6 ENGR/CS 101 Computer Science Session 5
Often want to notify user
Use MessageBox to pop up
if (shiftKey.Text == "") { MessageBox.Show ("Missing shift key!"); return; // go back to waiting }
Lecture 6 ENGR/CS 101 Computer Science Session 6
Add the code from the previous slide to the
The code goes at the beginning of the handler
Bonus: add a second check that the user
Lecture 6 ENGR/CS 101 Computer Science Session 7
Another GUI element
Declare a variable
OpenFileDialog fileChooser = new OpenFileDialog();
Lecture 6 ENGR/CS 101 Computer Science Session 8
To make the dialog pop up and get the file
DialogResult result = fileChooser.ShowDialog();
Check if the user clicked "Cancel" and if so, go
// Check if user clicked Cancel if (result == DialogResult.Cancel) return;
Lecture 6 ENGR/CS 101 Computer Science Session 9
A file stream is a program object that is opened
// 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 10
Add a Load button to your program's interface Double-click on the button to get the handler
Between the { }'s, type in the code from the
Run and test your program until it works
Lecture 6 ENGR/CS 101 Computer Science Session 11
Add a Save button to your program. The handler should save the ciphertext to a file.
The save file dialog type is SaveFileDialog, but
To write to a file, open a StreamWriter object with
StreamWriter fileOutput = new StreamWriter (fileChooser.FileName); fileOutput.WriteLine (cipherText.Text); fileOutput.Close();
Lecture 6 ENGR/CS 101 Computer Science Session 12
One of the reasons the Caesar shift cipher is
We could decipher by writing a ShiftLetterBack
Unfortunately, the % (modulus) operator only
Lecture 6 ENGR/CS 101 Computer Science Session 13
But in modular arithmetic, subtracting is the
For example, shift key 'I', gives a shift number
This means that we can use the ShiftLetter
Lecture 6 ENGR/CS 101 Computer Science Session 14
Bonus exercise: Add a Decipher button and
The Decipher button handler will be the same
it computes the complementary shift number to
it reverses the use of the plainText and cipherText