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

engr cs 101 cs session lecture 6
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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?

slide-2
SLIDE 2

Lecture 6 ENGR/CS 101 Computer Science Session 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

slide-3
SLIDE 3

Lecture 6 ENGR/CS 101 Computer Science Session 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.

slide-4
SLIDE 4

Lecture 6 ENGR/CS 101 Computer Science Session 4

Interface Mockup

 Same as last time  Add buttons to load

plaintext from a file, and save ciphertext to a file

slide-5
SLIDE 5

Lecture 6 ENGR/CS 101 Computer Science Session 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 }

slide-6
SLIDE 6

Lecture 6 ENGR/CS 101 Computer Science Session 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.

slide-7
SLIDE 7

Lecture 6 ENGR/CS 101 Computer Science Session 7

Reading from a File

 Another GUI element

that can be created by a program is the

  • pen file dialog.

 Declare a variable

and create a new

  • bject using the C#

new command

OpenFileDialog fileChooser = new OpenFileDialog();

slide-8
SLIDE 8

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

slide-9
SLIDE 9

Lecture 6 ENGR/CS 101 Computer Science Session 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

slide-10
SLIDE 10

Lecture 6 ENGR/CS 101 Computer Science Session 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.

slide-11
SLIDE 11

Lecture 6 ENGR/CS 101 Computer Science Session 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

  • therwise 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();

slide-12
SLIDE 12

Lecture 6 ENGR/CS 101 Computer Science Session 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.

slide-13
SLIDE 13

Lecture 6 ENGR/CS 101 Computer Science Session 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

  • f 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.

slide-14
SLIDE 14

Lecture 6 ENGR/CS 101 Computer Science Session 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.