COMP 1010 写作、 辅导Java,Python程序

” COMP 1010 写作、 辅导Java,Python程序ASSIGNMENT 3: Strings, chars, and Loops (Units 1-13)DEPARTMENT AND COURSE NUMBER: COMP 1010COURSE TITLE: Introduction to Computer Science 1TERM: Fall 2020Assignment 3DUE DATE: NOVEMBER 6, 2020 BY 11:59 PM CDTSubmission Guidelines: Name your sketch using your name, and the assignment number, exactly as in this example:LastnameFirstnameA3Q3. You will submit only one program in this assignment, forthe highest question Completed. There are marks for the behaviour of your program, and marks for coding. If your programdoes not run upon download, you will not receive any marks for program behaviour. Assignments must Follow the programming standards document published on the coursewebsite on UM Learn. After the due date and time assignments may be submitted but will lose 2% of marks perhour late or portion thereof. The date and time of the last file submitted controls the mark forthe entire assignment. You may submit your program multiple times, but only the most recent version will bemarked. These assignments are your chance to learn the material for the exams. Code yourassignments independently. We use software to compare all submitted assignments to eachother, and pursue academic dishonestly vigorously.Note:This assignment is an opportunity to demonstrate your knowledge of Strings, chars, and loops.Even if you know arrays, or other data structures, they should not be used on this assignment.This assignment makes use of global variables, because we havent yet covered how to pass datato and from user-defined functions (thats Unit 14). If you have read ahead, youre welcome tomodify the user-defined Functions to use parameters and return values instead of relying onglobal variables. However, this is not required.Each question builds on the previous Question. Make sure that you follow ALL instructionscarefully and finish one question completely before moving on to the next question. You areasked to do certain things in a specific way so that you dont run into difficulty later.Background:Cryptography is the study and practice of creating secure, private communication that can notbe understood by anyone that intercepts the information. Modern cryptography underlies muchof the modern world including e-commerce, computer passwords, and more.Modern cryptography algorithms are quite sophisticated but, prior to electronic computers,cryptography focused on encryption: the process of taking readable information and making itlook unreadable, Often using a secret key. The un-encrypted text is called the plaintext, and theencrypted text is called the ciphertext. The process of turning the ciphertext back into plaintextis called decryption.ASSIGNMENT 3DEPARTMENT AND COURSE NUMBER: COMP 10102Caesar Shift AlgorithmYou should be able to do Q1 Q2 after Week 7. They cover String, chars, and loops.Write an Active Processing program to implement the Caesar Cipher (also known as theCaesar Shift or shift cipher).The Caesar Cipher is one of the simplest ways to encrypt a message (its also one of the easiestto decrypt, so its not very secure). It is a type of substitution cipher, meaning that each letter inthe plaintext is replaced one at a time in order to produce the ciphertext.In the Caesar Cipher, each letter of the plaintext is replaced by the letter that is KEY positionslater in the alphabet. For example, if KEY =3, then the letter a will be replaced by d, b willbe replaced by e, and so on, up until z which will be replaced by c.Note: the Caesar Shift that we are implementing will only encrypt/decrypt letters (notnumbers, punctuation, Spaces, etc.).So for KEY = 3 each plaintext letter of the alphabet will be encrypted into the followingciphertext letter:0 1 2 3 4 5 6 7 8 9 10 11 12plaintext a b c d e f g h i j k l mcipher d e f g h i j k l m n o p13 14 15 16 17 18 19 20 21 22 23 24 25plaintext n o p q r s t u v w x y zcipher q r s t u v w x y z a b cWe can encrypt messages using the Caesar Cipher by first assigning each letter of the alphabet toa number from 0 to 25. Then, for each character in the plaintext, find the corresponding numbervalue, and add KEY modulo 26. To decrypt the message, this process is reversed.Here is an example of the plaintext theansweris encrypted using the KEY = 3:plaintext t h e a n s w e r i splaintext as number 19 7 4 0 13 18 22 4 17 8 18( plaintext + KEY ) mod 26 22 10 7 3 16 21 25 7 20 11 21ciphertext w k h d q v z h u l vMathematically, the Caesar shift encryption of a letter x by a KEY can be written as:EKEY(x) = ( x + KEY ) mod 26and the decryption can be represented mathematically as:DKEY(x) = ( x KEY ) mod 26Recall that char values are stored as a numbers by the computer, but its not as simple as a = 0,b =1, etc. We are using the ASCII encoding, and so you will need to take that into account inyour encryption (and decryption) equations.ASSIGNMENT 3DEPARTMENT AND COURSE NUMBER: COMP 10103Q1: Caesar Shift [8 marks] Create a global integer constant KEY = 3. In the setup() function, call the noLoop() command so that draw() will only run once.All repetition in this assignment will be done with loops later on. Note that none of the code you will write in this assignment will use the Processing canvas,so you do not need to include a size() command in setup(). Ask the user for some plaintext to encrypt:o At the very beginning of your program, import JOptionPane using the statementimport javax.swing.JOptionPane;o Create three global String variables called userInput, encryptedText, anddecryptedText. Initialize each of them to be an empty String.o In your draw() function, use JOptionPane.showInputDialog() to get aplaintext String from the user, and store this value in userInput. Make sure to giveyour dialogue box a Reasonable prompt.o The user should only enter text that consists entirely of letters (a-z and A-Z), nonumbers, punctuation, or spaces. In the program, we can assume that any text enteredby the user is valid for now. Well verify the user input in Q5.o Note: if you click Cancel on the input dialog box the program will crash. Thatsok, we havent learned how to handle that. Just stop your program manually. Write a function called caesarEncrypt()o This function will encrypt the userInput using the Caesar Cipher described above.o The amount of the shift is KEY.o Do not change or modify userInput in any way. Instead, store the ciphertext in theencryptedText variable.o The encryptedText should be in all lowercase letters. (Hint: it is easiest to make aversion of the plaintext in all lower case letters first, and then encrypt it. This way youdont need to deal with both upper and lower case letters).o At the end of the function, print a message with the userInput and encryptedTextto the console in the following format:You entered the plaintext: ComputerScienceand the ciphertext is: frpsxwhuvflhqfh Write a function called caesarDecrypt()o This function will decrypt the userInput using the Caesar Cipher described above.o The amount of the shift is KEY (notice that this time we subtract KEY from each letter).o Do not change or modify userInput in any way. Instead, store the decryptedplaintext in the decryptedText variable.o The decryptedText should be in all lower case letters.o At the end of the function, print a message with the userInput and decryptedTextto the console in the following format:You entered the ciphertext: frpsxwhuvflhqfhand the plaintext is: computerscienceASSIGNMENT 3DEPARTMENT AND COURSE NUMBER: COMP 10104 In draw(), call caesarEncrypt()after asking the user for the plaintext. Then, ask theuser for input again and call caesarDecrypt(). Note that at this point your draw()function should only have four lines of code.Q2: Changing the KEY [3 marks]Now lets make things more interesting, by allowing the user to pick the encryption key. Modify the global constant KEY to be an integer variable called shiftKey. Give shiftKeyan initial value of zero.o Make sure to replace KEY everywhere in your program with shiftKey. In draw(), before any of the other code, use JOptionPane.showInputDialog() to askthe user to enter an integer value for the key. JOptionPane will return a String value, soyoull need to turn It to an integer before storing the value in shiftKey. (Hint: Processinghas an int() function). But what if the user doesnt enter a valid number? After converting the user input into aninteger, check if the value of shiftKey is between 1 and 25 inclusive. If its not, keepasking the user for a new number until a valid number is entered.o Note: When the int() function is used on a String, it will return the value 0 for anyString containing characters other than digits (such as letters or symbols).Q3: Stop the Pop-Up Boxes [2 marks]You should be able to do the remaining questions after Week 8 in the course.Modify your draw() function so that all of the code currently in draw() runs over and over againuntil the text that the user wants to decrypt is quit.When the value Of userInput to decrypt is quit, the program should decrypt this word andthen stop prompting the user for any additional input. Use the command exit() to end theprogram and close the canvas.Vigenre Cipher AlgorithmThe Vigenre Cipher is another substitution cipher that is more sophisticated than the CaesarShift. Rather than having a single numeric KEY, the Vigenre Cipher uses a keyword.This keyword is repeated until it matches the length of the plaintext.Each letter in the plaintext and keyword is converted to the corresponding number between 0 and25 (A = 0, B = 1, Z = 25, just like in the Caesar Shift). The numeric value of each letter of theplaintext is added to the numeric value of the corresponding letter in the keyword, modulo 26.Mathematically, we can represent the encryption of each plaintext letter Pi into a ciphertext letterCi using the keyword letter Ki in the Vigenre Cipher as:Ci = ( Pi + Ki ) mod 26To decrypt a ciphertext letter Ci into plaintext letter Pi with keyword letter Ki:Pi = ( Ci Ki + 26 ) mod 26ASSIGNMENT 3DEPARTMENT AND COURSE NUMBER: COMP 10105For example, if the keyword is COMPUTER, with the plaintext THEANSWERISPlaintext THEANSWERIS Plaintext as number 19 7 4 0 13 18 22 4 17 8 18Key COMPUTERCOM Key as number 2 14 12 15 20 19 4 17 2 14 12Ciphertext VVQPHLAVTWE (Plaintext + Key ) % 26 21 21 16 15 7 11 0 21 19 22 4Q4: Vigenre Cipher [6 marks] Create a global String constant called KEYWORD . The value of KEYWORD should be your firstname without any punctuation in all upper case letters. Write a function called vigenereEncrypt()o This function will encrypt the userInput using the Vigenre Cipher described above.o Do not change or Modify userInput in any way. Instead, store the ciphertext in theencryptedText variable.o The encryptedText should be in all UPPERCASE letters.o At the end of the function, print a message with userInput and encryptedText tothe console in the same format as Q1. Write a function called vigenereDecrypt()o This function decrypt the userInput using the Vigenre Cipher described above.o Do not change or modify userInput in any way. Instead, store the decryptedplaintext in the decryptedText variable.o The decryptedText should be in all UPPERCASE letters.o At the end of the function, print a message with the userInput and decryptedTextto the console in the same format as Q1. Make sure to test these functions! One way to do that is to comment out the code in draw()that calls the Caesar Shift encryption/decryption functions, and call the these ones instead. Inparticular, make sure to test these functions with text that is longer than your name.Q5: Validate user input [3 marks]Both of our Caesar Cipher and Vigenre Cipher can only encrypt and decrypt letters of thealphabet. In this question create a function to check that the plaintext or ciphertext entered by theuser only contains letters. Create a global boolean variable called isAllLetters. Create a function called checkAllLetters()o In the checkAllLetters() function, check each character of userInput. If everycharacter is a letter set the value of isAllLetters to true. If one or more charactersin userInput is not a letter, set the value of isAllLetters to false.o Hint: the function Character.isLetter() checks if a char is a letterASSIGNMENT 3DEPARTMENT AND COURSE NUMBER: COMP 10106Q6: Put it all together [6 marks]Modify your draw() function so that your program has the following behaviour: Using JOptionPane.showInputDialog(), ask the user to type in one of three options:Caesar, Vigenere, or Quit.o Give the dialog box an appropriate prompt that tells the user what to type in for eachof the three selections.o Capitalization does not matter for any of the options. The program should recognizeCaesar, caesar, CAESAR , CaEsAr, etc. as the same.o Save the users selection in userInput. If the user enters Caesar, the program should prompt the user for a key until a valid valueis entered (this is your code from Q2)o Once a valid key has been entered, prompt the user for text to encrypt. Use yourcheckAllLetters() function to check the input, and keep prompting the user for avalue until they have entered plaintext that is valid (i.e. all letters). Encrypt this textusing your caesarEncrypt() function.o Next, prompt the user for text to decrypt. Again, use your checkAllLetters()function to check the input and keep prompting the user until they enter valid text.Decrypt this text using your caesarDecrypt() function. Otherwise, if the user enters Vigenere:o Prompt the user for text to encrypt. Make sure that the text is valid, and keepprompting the user until they enter text that only contains letters. Encrypt this plaintextusing your vigenereEncrypt() function.o Prompt the user for text to decrypt, make sure that it is valid, and keep prompting theuser until valid text is entered. Decrypt this text using vigenereDecrypt(). Otherwise, if the user enters Quit:o Stop prompting the user for any input.o Note: Quit should only work from this menu. If the user enters Quit whenprompted for plaintext or ciphertext, the program should not end. After completing the Caesar Shift or Vigenre Cipher selection, the program shouldprompt the user to type in an option again from Caesar, Vigenere, or Quit. If anything else is entered other than the three possible options, the program shouldignore it and prompt the user to type in an option again.[Programming Standards are worth 5 marks]Hint: Some useful String and char functions that you may or may not want to use. Not all of them are useful for this assignment.For more information about each function, check out the Processing Reference Library. equals() equalsIgnoreCase() charAt() toLowerCase() toUpperCase() substring() isLowerCase() isUpperCase() isLetter() isDigit()如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导