Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | brenda-mcgee |
View: | 214 times |
Download: | 0 times |
Homework 5
• Due ( MT sections )
( WTh sections )
at midnightSun., 10/6
Mon., 10/7
• Problems
http://www.cs.hmc.edu/courses/2002/fall/cs5/
http://www.cs.hmc.edu/courses/2002/fall/cs5/week_05/homework.html
• CS 5 website
email [email protected] or [email protected]
• Submission problems or other concerns?
• Tutors availableAcademic Computing
labs (Parsons)
Linde Activities Center lab
Friday Afternoons 1-4 pm----------------------------------------Chris Hwang this week (10/4): ParsonsDon Lee this week (10/4): ParsonsDaniel Chan this week (10/4): LAC LabZach Andree this week (10/4): LAC LabAnnie Chang this week (10/4): LAC Lab
Saturday Afternoons 1-4pm----------------------------------------Chris Weisiger this week (10/5): ParsonsYu-Min Kim this week (10/5): ParsonsElizabeth Lee-Su this week (10/5): LAC LabAaron Homer this week (10/5): LAC Lab
Sunday Afternoons 1-4pm----------------------------------------Gabriel Neer this week (10/6): ParsonsJeff Brenion this week (10/6): LAC LabRene Logan this week (10/6): LAC Lab
• Tutors available
Sunday evenings 7-12pm----------------------------------------Jenny Xu 6-9 this week (10/6): ParsonsEric Flynn 7-10 this week (10/6): ParsonsMelissa Federowicz 7-10 this week (10/6): LAC LabMax Yi 7-10 this week (10/6): LAC LabYu-Min Kim 9-12 this week (10/6): ParsonsA. Klose 9-12 this week (10/6): ParsonsMatt Beaumont-Gay 9-12 this week (10/6): LAC LabChris Hwang 9-12 this week (10/6): LAC Lab
Monday Evenings 7-12pm----------------------------------------Adam Kangas 7-10 this week (10/7): ParsonsRyka Neher 7-10 this week (10/7): ParsonsMax Yi 7-10 this week (10/7): LAC LabAnnie Chang 9-12 this week (10/7): ParsonsPaul Scott 9-12 this week (10/7): ParsonsJohn McCollough 9-12 this week (10/7): ParsonsAlex Pipkin 9-12 this week (10/7): LAC LabChris Wottawa 9-12 this week (10/7): LAC LabAlex Utter 9-12 this week (10/7): LAC Lab
Problem 2 -- Caesar Cipher
Encryption strategy
Choose an amount to shift your message.
Move each letter that amount.
message:
eorrgb lqvwuxfwlrqv zklfk uhwxuq wr sodjxh wkhlu lqyhqwru
shift amount:3
encrypted message:
Problem 2 -- Caesar Cipher
Decryption strategy
Shift the encrypted message by every possible shift amount
Pick out the English text...
eorrgb lqvwuxfwlrqv zklfk uhwxuq wr sodjxh wkhlu lqyhqwruencrypted message:
all possible shifts: eorrgb lqvwuxfwlrqv zklfk uhwxuq wr sodjxh wkhlu lqyhqwrufpsshc mrwxvygxmsrw almgl vixyvr xs tpekyi xlimv mrzirxsvgqttid nsxywzhyntsx bmnhm wjyzws yt uqflzj ymjnw nsajsytwhruuje otyzxaizouty cnoin xkzaxt zu vrgmak znkox otbktzuxisvvkf puzaybjapvuz dopjo ylabyu av wshnbl aolpy pucluavyjtwwlg qvabzckbqwva epqkp zmbczv bw xtiocm bpmqz qvdmvbwzkuxxmh rwbcadlcrxwb fqrlq ancdaw cx yujpdn cqnra rwenwcxalvyyni sxcdbemdsyxc grsmr bodebx dy zvkqeo drosb sxfoxdybmwzzoj tydecfnetzyd hstns cpefcy ez awlrfp esptc tygpyezcnxaapk uzefdgofuaze ituot dqfgdz fa bxmsgq ftqud uzhqzfad
< skipping 9 lines >xhkkzu ejopnqypekjo sdeyd napqnj pk lhwcqa pdaen ejrajpknyillav fkpqorzqflkp tefze obqrok ql mixdrb qebfo fksbkqlozjmmbw glqrpsargmlq ufgaf pcrspl rm njyesc rfcgp gltclrmpaknncx hmrsqtbshnmr vghbg qdstqm sn okzftd sgdhq hmudmsnqbloody instructions which return to plague their inventorcmppez jotusvdujpot xijdi sfuvso up qmbhvf uifjs jowfoupsdnqqfa kpuvtwevkqpu yjkej tgvwtp vq rnciwg vjgkt kpxgpvqt
Resources
Suppose we have a method that shifts a character c by n letters.
char advanceChar(char c, int n)
Assume these work...
length() (tells the length of a String)
charAt(int n) (returns the nth char of a String)
advanceChar(‘h’, 4) is
advanceChar(‘z’, 2) is
String s = “more java”;
s.length() is
s.charAt(2) is
s.charAt( ) is ‘v’
High-level plan
We havechar advanceChar(char c, int n)
length() (tells the length of a String)
charAt(int n) (returns the nth char in a String, starting at 0)
What to do ?
advanceChar
Now we need to worry about making this piece work...
public static char advanceChar(char c, int n)
Working with chars
_ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { } ~
979899 122
123
char c = ‘y’;
c = (char)(c - 2);
c += 5;
How do we get back?
Style note
// be sure to comment each method you write!public static char advanceChar(char c, int n)
// Name: advanceChar// Inputs: a char (c) and an int (n)// Output: the char that is n places // to the right of c, where the// alphabet wraps from ‘z’ to ‘a’.
public static void main(String[] args){ int x = H.in.nextInt(); int y = H.in.nextInt(); int z = confusing(x,y); H.out.println(“z is ” + z); }
public static int confusing(int y, int x){ int a = 0; a = 10*y + x; return a;}
A message from our sponsor
Problem 1 -- finding ex
Please input a power (a double) and I will raise e to that power: 2.5
Enter the number of terms you'd like to use (an int >= 1): 10
The result is 12.179115120485767
Would you like to continue (yes/no)? no
Inside main :
This is e2.5 computed using 10 terms of the series
x0
0!x1
1!x2
2!x3
3!+ + + +…
4 terms
public static double exp(double x, int numTerms)
expWhat does exp do?
What are x and numTerms referring to?
power
public static double power(double base, int p)
What does power do?
What are base and p referring to?
Problem 1 -- finding ex
Please input a power (a double) and I will raise e to that power: 2.5
Enter the number of terms you'd like to use (an int >= 1): 10
The result is 12.179115120485767
Would you like to continue (yes/no)? no
Inside main :
Homework 5
• Due ( MT sections )
( WTh sections )
at midnightSun., 10/6
Mon., 10/7
• Problems
http://www.cs.hmc.edu/courses/2002/fall/cs5/
http://www.cs.hmc.edu/courses/2002/fall/cs5/week_05/homework.html
• CS 5 website
email [email protected] or [email protected]
• Submission problems or other concerns?
• Tutors availableAcademic Computing
labs (Parsons)
Linde Activities Center lab
Friday Afternoons 1-4 pm----------------------------------------Chris Hwang this week (10/4): ParsonsDon Lee this week (10/4): ParsonsDaniel Chan this week (10/4): LAC LabZach Andree this week (10/4): LAC LabAnnie Chang this week (10/4): LAC Lab
Saturday Afternoons 1-4pm----------------------------------------Chris Weisiger this week (10/5): ParsonsYu-Min Kim this week (10/5): ParsonsElizabeth Lee-Su this week (10/5): LAC LabAaron Homer this week (10/5): LAC Lab
Sunday Afternoons 1-4pm----------------------------------------Gabriel Neer this week (10/6): ParsonsJeff Brenion this week (10/6): LAC LabRene Logan this week (10/6): LAC Lab
• Tutors available
Sunday evenings 7-12pm----------------------------------------Jenny Xu 6-9 this week (10/6): ParsonsEric Flynn 7-10 this week (10/6): ParsonsMelissa Federowicz 7-10 this week (10/6): LAC LabMax Yi 7-10 this week (10/6): LAC LabYu-Min Kim 9-12 this week (10/6): ParsonsA. Klose 9-12 this week (10/6): ParsonsMatt Beaumont-Gay 9-12 this week (10/6): LAC LabChris Hwang 9-12 this week (10/6): LAC Lab
Monday Evenings 7-12pm----------------------------------------Adam Kangas 7-10 this week (10/7): ParsonsRyka Neher 7-10 this week (10/7): ParsonsMax Yi 7-10 this week (10/7): LAC LabAnnie Chang 9-12 this week (10/7): ParsonsPaul Scott 9-12 this week (10/7): ParsonsJohn McCollough 9-12 this week (10/7): ParsonsAlex Pipkin 9-12 this week (10/7): LAC LabChris Wottawa 9-12 this week (10/7): LAC LabAlex Utter 9-12 this week (10/7): LAC Lab
Problem 2 -- Caesar Cipher
Encryption strategy
Choose an amount to shift your message.
Move each letter that amount.
message:
eorrgb lqvwuxfwlrqv zklfk uhwxuq wr sodjxh wkhlu lqyhqwru
shift amount:3
encrypted message:
Problem 2 -- Caesar Cipher
Decryption strategy
Shift the encrypted message by every possible shift amount
Pick out the English text...
eorrgb lqvwuxfwlrqv zklfk uhwxuq wr sodjxh wkhlu lqyhqwruencrypted message:
all possible shifts: eorrgb lqvwuxfwlrqv zklfk uhwxuq wr sodjxh wkhlu lqyhqwrufpsshc mrwxvygxmsrw almgl vixyvr xs tpekyi xlimv mrzirxsvgqttid nsxywzhyntsx bmnhm wjyzws yt uqflzj ymjnw nsajsytwhruuje otyzxaizouty cnoin xkzaxt zu vrgmak znkox otbktzuxisvvkf puzaybjapvuz dopjo ylabyu av wshnbl aolpy pucluavyjtwwlg qvabzckbqwva epqkp zmbczv bw xtiocm bpmqz qvdmvbwzkuxxmh rwbcadlcrxwb fqrlq ancdaw cx yujpdn cqnra rwenwcxalvyyni sxcdbemdsyxc grsmr bodebx dy zvkqeo drosb sxfoxdybmwzzoj tydecfnetzyd hstns cpefcy ez awlrfp esptc tygpyezcnxaapk uzefdgofuaze ituot dqfgdz fa bxmsgq ftqud uzhqzfad
< skipping 9 lines >xhkkzu ejopnqypekjo sdeyd napqnj pk lhwcqa pdaen ejrajpknyillav fkpqorzqflkp tefze obqrok ql mixdrb qebfo fksbkqlozjmmbw glqrpsargmlq ufgaf pcrspl rm njyesc rfcgp gltclrmpaknncx hmrsqtbshnmr vghbg qdstqm sn okzftd sgdhq hmudmsnqbloody instructions which return to plague their inventorcmppez jotusvdujpot xijdi sfuvso up qmbhvf uifjs jowfoupsdnqqfa kpuvtwevkqpu yjkej tgvwtp vq rnciwg vjgkt kpxgpvqt
Resources
Suppose we have a method that shifts a character c by n letters.
char advanceChar(char c, int n)
Assume these work...
length() (tells the length of a String)
charAt(int n) (returns the nth char of a String)
advanceChar(‘h’, 4) is
advanceChar(‘z’, 2) is
String s = “more java”;
s.length() is
s.charAt(2) is
s.charAt( ) is ‘v’
High-level plan
We havechar advanceChar(char c, int n)
length() (tells the length of a String)
charAt(int n) (returns the nth char in a String, starting at 0)
What to do ?
advanceChar
Now we need to worry about making this piece work...
public static char advanceChar(char c, int n)
Working with chars
_ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { } ~
979899 122
123
char c = ‘y’;
c = (char)(c - 2);
c += 5;
How do we get back?
Style note
// be sure to comment each method you write!public static char advanceChar(char c, int n)
// Name: advanceChar// Inputs: a char (c) and an int (n)// Output: the char that is n places // to the right of c, where the// alphabet wraps from ‘z’ to ‘a’.
public static void main(String[] args){ int x = H.in.nextInt(); int y = H.in.nextInt(); int z = confusing(x,y); H.out.println(“z is ” + z); }
public static int confusing(int y, int x){ int a = 0; a = 10*y + x; return a;}
A message from our sponsor
Problem 1 -- finding ex
Please input a power (a double) and I will raise e to that power: 2.5
Enter the number of terms you'd like to use (an int >= 1): 10
The result is 12.179115120485767
Would you like to continue (yes/no)? no
Inside main :
This is e2.5 computed using 10 terms of the series
x0
0!x1
1!x2
2!x3
3!+ + + +…
4 terms
public static double exp(double x, int numTerms)
expWhat does exp do?
What are x and numTerms referring to?
power
public static double power(double base, int p)
What does power do?
What are base and p referring to?