CS 100, Summer 2001 Monday, 7/23 Lecture 14 ----------------------------------------------------------------------- Announcements: + Project 1 due 7/26 + Exercise 10 due 7/24 + Pick up graded A4 and Prelim 2 + reading: Savitch 6.3, 6.4 + Grades will be posted to Carpenter later today ----------------------------------------------------------------------- Topics: + Prelim 2 + Project 1 (encryption) + Multidimensional arrays ----------------------------------------------------------------------- ---------------------------------------------------------------------- + Review Questions - If you didn't ask your question on Thursday, you missed out. - Review sessions are your chance to get your questions answered before the exam. Instead you'll now get the answers after the exam. - Static v. final! ----------------------------------------------------------------------- ---------------------------------------------------------------------- + Prelim 2 - Scoping: many people missed this! Remember, curly braces are walls. (Work through the prelim problem) - Constructors: the purpose of a constructor is to give values to all of the instance variables. Make this your mantra for constructors. - References: these point to objects. (Work through the prelim problem) - Arrays/methods: biggest problem was people leaving out a method comment. You must always include a method comment! Include what the method does, its input, and its output. ----------------------------------------------------------------------- ---------------------------------------------------------------------- + Project 1 - Encryption: encoding. Read the excerpt from "The Code Book" (not a programming book!) - an excellent tale of world history from the Arabs to today, and how cryptography has had an influential role on it (changed the outcomes of wars, e.g. WWII with the Enigma machine). - Example 1: plaintext: Attack at dawn. ciphertext: Attack at dawn. - Not a very good encryption - each letter substituted with itself! - Example 2: plaintext: Attack at dawn. ciphertext: Buubdl bu ebxo. - Better. This is a Caesar shift of 1 - each letter substituted with the next letter in the alphabet. The substitution string is "bcdefghijklmnopqrstuvwxyza". There are 25 possible Caesar shifts. (Always shift to the left) - Example 3: plaintext: Attack at dawn. ciphertext: Qzzqea qz rqvf. - Even better. Each letter substituted with some other letter, but there's no simple pattern, as with the Caesar shift. The substitution string is "qwertyuiopasdfghjklzxcvbnm". There are 25! possible substitution strings. - Your Encrypt.java will take in the name of an input (plaintext) file, the name of an output (ciphertext) file, and a substitution string, and then perform the appropriate encryption. - We'll also provide sample output for a given input. + Decryption - Well and good, but now what do you do with the encrypted text if you (or a friend) want to read it later? If you know the substitution string used to encrypt it, this is easy: just run it through the encrypter again with an inverted substitution string. - Example 2: Use a Caesar shift to the right: "zabcdefghijklmnopqrstuvwxy" to undo the encryption. - Example 3: Invert the mapping: "kxvmcnophqrszyijadlegwbuft" - If you don't know the string, you need to start guessing. But 25! is a lot of possible choices. To assist you, we've provided Decrypt.class. Use it to test out your guesses for which letters substitute for other letters. + Frequency Analysis - To get an idea for which letters map to others, you can look at the relative frequencies. Most common letters are "e", "t", and "a", so if your message is mostly "r", "q", and "z", you might hypothesize that "r" is either "e", "t", or "a", and so on. - You'll write a program to do this frequency analysis as a helpful tool for decrypting a message whose substitution string you don't know. See the reading for more details. - We'll also provide sample output for a given input. + The Character class - char is a base type. But Character is a Java predefined class, that comes with several handy methods. Like Math, you don't need to call the methods using an object (they are static). - Examples: char c = 'a'; boolean isUpper = Character.isUpperCase(c); // false See list of useful methods on Savitch p. ? - So the Character class gives you methods to use on chars. DO NOT CONFUSE Character with char. One is a class; one is a base type. char.isUpperCase() will NOT work, nor will c.isUpperCase(). + This project will give you practice using arrays, exceptions, and file I/O. ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Arrays - Review: arrays are objects. They store multiple items, all of the same type. - syntax for array declaration type[] name = new type[howMany]; + Multidimensional arrays (6.5) - syntax for 2 dimensions type[][] name = new type[firstDim][secondDim]; - EXAMPLE: InterestTable.java - Draw pictures - And with objects: draw reference pointers + Can go higher by adding more []'s. ---------------------------------------------------------------------- PICK UP graded Assignment 4, Prelim 2 PICK UP handed out Exercise 10 hand in your Exercise 9!