1 / 12

CSCI S-1 Section 11

CSCI S-1 Section 11. Coming Soon. Problem Set Six (25 + 5 points) Tuesday, 28 July, 17:00 EST Problem Set Seven (20 points) Friday, 31 July, 17:00 EST Exam Review Monday , 3 August, 15:15 EST Final Exam Wednesday, 5 August, 15:15 EST PS 6 Office Hours Tonight Sever Hall, room 106 .

inigo
Download Presentation

CSCI S-1 Section 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI S-1Section 11

  2. Coming Soon • Problem Set Six (25 + 5 points) • Tuesday, 28 July, 17:00 EST • Problem Set Seven (20 points) • Friday, 31 July, 17:00 EST • Exam Review • Monday , 3 August, 15:15 EST • Final Exam • Wednesday, 5 August, 15:15 EST • PS 6 Office Hours Tonight • Sever Hall, room 106

  3. Caesar Cipher • You have an integer key k and a text T and you want to rotate all the letters in T by k. • What is the encryption for k=3 and a text of “ABCD”? • Java represents characters internally as numbers, so you may achieve the desired effect by converting the character to an integer, adding k to it, and converting back to a char. • What is the encryption for k=3 and a text of “ZzZz”? • If you simply add k to 'Z', you will not get 'C’ -- you need to circle around and get back to the beginning, paying attention to whether the letter is uppercase or lowercase • What arithmetic operator helps you circle back around?

  4. VigenereCipher • For Vigenere, the idea is similar except the key is not an integer but rather a word. • If the keyword is "ABCD“, Vigenerewill encrypt by shifting the first letter by 0, the second by 1, the third by 2, the fourth by 3, the fifth by 0, the sixth by 1, and so on in a cyclic manner. • The trick is to compute the shift equivalent of each letter (e.g., 'a' corresponds to a shift of 0, 'b' to a shift of 1, etc) and cycling through the keyword to get the current shift amount. Other than that, things are pretty similar to Caesar.

  5. Useful Char Methods • Character.isLetter - takes a character and returns true if the character ch is a letter. • Character.isLowerCase - takes a character and returns true if the character ch is lowercase. • Character.isUpperCase - takes a character and returns true if the character ch is uppercase.

  6. Exercise Exercise: Write a method that takes a string, replaces all uppercase letters with a + (plus) and all lowercase letters with a - (minus), and returns the modified string. All characters that are not letters should be left unchanged.

  7. upperPlusLowerMinus public static String upperPlusLowerMinus(String inString) { }

  8. upperPlusLowerMinus public static String upperPlusLowerMinus(String inString) { String outString = ""; char ch; for(inti = 0; i < inString.length(); i++) { ch= inString.charAt(i); if (Character.isLetter(ch)) if (Character.isUpperCase(ch)) outString += “+"; else outString += “-"; else outString += Character.toString(ch); } return outstring; }

  9. BigInteger • For very large numbers • java.sun.com/javase/6/docs/api/java/math/BigInteger.html • Import java.math.BigInteger; • BigIntegernum = new BigInteger("99999999999999999"); • BigIntegernum2 = new BigInteger("3");

  10. BigInteger Methods • num.isProbablePrime(certainty) returns true if num is prime with certainty (1 - 1/2^certainty) and false if it is definitely not prime • num.remainder(num2) returns num % num2 as a BigInteger • num.compareTo(num2) returns -1, 0 or 1 as num is less than, equal to, or greater than num2 • num.add(number2) returns sum of num, num2 • num.toString() converts BigInteger back into a String.

  11. BigInteger Exercise // print all nine-digit numbers that are divisible by 1234567 import java.math.BitInteger; Public static void main (String [] args) { BigIntegerdivnum = new BigInteger("1234567"); }

  12. BigInteger Exercise // print all nine-digit numbers that are divisible by 1234567 import java.math.BitInteger; Public static void main (String [] args) { BigIntegerdivnum= new BigInteger("1234567"); final BigInteger ONE = new BigInteger("1"); final BigInteger ZERO = new BigInteger("0"); for ( BigIntegeri = new BigInteger("100000000"), BigInteger j = new BigInteger("999999999"); j.compareTo(i) >= 0; i=i.add(ONE)) { BigIntegerresult = i.remainder(divnum); if (result.compareTo(ZERO) == 0) System.out.println(i.toString()); } }

More Related