Home Page > > Details

COMP 1010Help With , Java,Python Programming,Help With c++ ProgrammingDebug Matlab Programming|Debug Matlab Programming

ASSIGNMENT 3: Strings, chars, and Loops (Units 1-13)
DEPARTMENT AND COURSE NUMBER: COMP 1010
COURSE TITLE: Introduction to Computer Science 1
TERM: Fall 2020
Assignment 3
DUE DATE: NOVEMBER 6, 2020 BY 11:59 PM CDT
Submission 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, for
the highest question completed.
• There are marks for the behaviour of your program, and marks for coding. If your program
does not run upon download, you will not receive any marks for program behaviour.
• Assignments must follow the programming standards document published on the course
website on UM Learn.
• After the due date and time assignments may be submitted but will lose 2% of marks per
hour late or portion thereof. The date and time of the last file submitted controls the mark for
the entire assignment.
• You may submit your program multiple times, but only the most recent version will be
marked.
• These assignments are your chance to learn the material for the exams. Code your
assignments independently. We use software to compare all submitted assignments to each
other, 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 haven’t yet covered how to pass data
to and from user-defined functions (that’s Unit 14). If you have read ahead, you’re welcome to
modify the user-defined functions to use parameters and return values instead of relying on
global variables. However, this is not required.
Each question builds on the previous question. Make sure that you follow ALL instructions
carefully and finish one question completely before moving on to the next question. You are
asked to do certain things in a specific way so that you don’t run into difficulty later.
Background:
Cryptography is the study and practice of creating secure, private communication that can not
be understood by anyone that intercepts the information. Modern cryptography underlies much
of 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 it
look unreadable, often using a secret key. The un-encrypted text is called the plaintext, and the
encrypted text is called the ciphertext. The process of turning the ciphertext back into plaintext
is called decryption.
ASSIGNMENT 3
DEPARTMENT AND COURSE NUMBER: COMP 1010
2
Caesar Shift Algorithm
You 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 the
Caesar Shift or shift cipher).
The Caesar Cipher is one of the simplest ways to encrypt a message (it’s also one of the easiest
to decrypt, so it’s not very secure). It is a type of substitution cipher, meaning that each letter in
the 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 positions
later in the alphabet. For example, if KEY =3, then the letter ‘a’ will be replaced by ‘d’, ‘b’ will
be 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 (not
numbers, punctuation, spaces, etc.).
So for KEY = 3 each plaintext letter of the alphabet will be encrypted into the following
ciphertext letter:
0 1 2 3 4 5 6 7 8 9 10 11 12
plaintext a b c d e f g h i j k l m
cipher d e f g h i j k l m n o p
13 14 15 16 17 18 19 20 21 22 23 24 25
plaintext n o p q r s t u v w x y z
cipher q r s t u v w x y z a b c
We can encrypt messages using the Caesar Cipher by first assigning each letter of the alphabet to
a number from 0 to 25. Then, for each character in the plaintext, find the corresponding number
value, 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 s
plaintext 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 21
ciphertext w k h d q v z h u l v
Mathematically, the Caesar shift encryption of a letter x by a KEY can be written as:
EKEY(x) = ( x + KEY ) mod 26
and the decryption can be represented mathematically as:
DKEY(x) = ( x – KEY ) mod 26
Recall that char values are stored as a numbers by the computer, but it’s 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 in
your encryption (and decryption) equations.
ASSIGNMENT 3
DEPARTMENT AND COURSE NUMBER: COMP 1010
3
Q1: 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 statement
import javax.swing.JOptionPane;
o Create three global String variables called userInput, encryptedText, and
decryptedText. Initialize each of them to be an empty String.
o In your draw() function, use JOptionPane.showInputDialog() to get a
plaintext String from the user, and store this value in userInput. Make sure to give
your dialogue box a reasonable prompt.
o The user should only enter text that consists entirely of letters (a-z and A-Z), no
numbers, punctuation, or spaces. In the program, we can assume that any text entered
by the user is valid for now. We’ll verify the user input in Q5.
o Note: if you click “Cancel” on the input dialog box the program will “crash”. That’s
ok, we haven’t 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 the
encryptedText variable.
o The encryptedText should be in all lowercase letters. (Hint: it is easiest to make a
version of the plaintext in all lower case letters first, and then encrypt it. This way you
don’t need to deal with both upper and lower case letters).
o At the end of the function, print a message with the userInput and encryptedText
to the console in the following format:
You entered the plaintext: ComputerScience
and 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 decrypted
plaintext 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 decryptedText
to the console in the following format:
You entered the ciphertext: frpsxwhuvflhqfh
and the plaintext is: computerscience
ASSIGNMENT 3
DEPARTMENT AND COURSE NUMBER: COMP 1010
4
• In draw(), call caesarEncrypt()after asking the user for the plaintext. Then, ask the
user 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 let’s 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 shiftKey
an 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 ask
the user to enter an integer value for the key. JOptionPane will return a String value, so
you’ll need to turn it to an integer before storing the value in shiftKey. (Hint: Processing
has an int() function).
• But what if the user doesn’t enter a valid number? After converting the user input into an
integer, check if the value of shiftKey is between 1 and 25 inclusive. If it’s not, keep
asking 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 any
String 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 again
until 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 and
then stop prompting the user for any additional input. Use the command exit() to end the
program and close the canvas.
Vigenère Cipher Algorithm
The Vigenère Cipher is another substitution cipher that is more sophisticated than the Caesar
Shift. Rather than having a single numeric KEY, the Vigenère 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 and
25 (A = 0, B = 1, … Z = 25, just like in the Caesar Shift). The numeric value of each letter of the
plaintext 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 letter
Ci using the keyword letter Ki in the Vigenère Cipher as:
Ci = ( Pi + Ki ) mod 26
To decrypt a ciphertext letter Ci into plaintext letter Pi with keyword letter Ki:
Pi = ( Ci – Ki + 26 ) mod 26
ASSIGNMENT 3
DEPARTMENT AND COURSE NUMBER: COMP 1010
5
For example, if the keyword is COMPUTER, with the plaintext “THEANSWERIS”
Plaintext THEANSWERIS Plaintext as number 19 7 4 0 13 18 22 4 17 8 18
Key COMPUTERCOM Key as number 2 14 12 15 20 19 4 17 2 14 12
Ciphertext VVQPHLAVTWE (Plaintext + Key ) % 26 21 21 16 15 7 11 0 21 19 22 4
Q4: Vigenère Cipher [6 marks]
• Create a global String constant called KEYWORD . The value of KEYWORD should be your first
name without any punctuation in all upper case letters.
• Write a function called vigenereEncrypt()
o This function will encrypt the userInput using the Vigenère Cipher described above.
o Do not change or modify userInput in any way. Instead, store the ciphertext in the
encryptedText variable.
o The encryptedText should be in all UPPERCASE letters.
o At the end of the function, print a message with userInput and encryptedText to
the console in the same format as Q1.
• Write a function called vigenereDecrypt()
o This function decrypt the userInput using the Vigenère Cipher described above.
o Do not change or modify userInput in any way. Instead, store the decrypted
plaintext 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 decryptedText
to 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. In
particular, 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 Vigenère Cipher can only encrypt and decrypt letters of the
alphabet. In this question create a function to check that the plaintext or ciphertext entered by the
user 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 every
character is a letter set the value of isAllLetters to true. If one or more characters
in userInput is not a letter, set the value of isAllLetters to false.
o Hint: the function Character.isLetter() checks if a char is a letter
ASSIGNMENT 3
DEPARTMENT AND COURSE NUMBER: COMP 1010
6
Q6: 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 each
of the three selections.
o Capitalization does not matter for any of the options. The program should recognize
“Caesar”, “caesar", “CAESAR” , “CaEsAr”, etc. as the same.
o Save the user’s selection in userInput.
• If the user enters “Caesar”, the program should prompt the user for a key until a valid value
is entered (this is your code from Q2)
o Once a valid key has been entered, prompt the user for text to encrypt. Use your
checkAllLetters() function to check the input, and keep prompting the user for a
value until they have entered plaintext that is valid (i.e. all letters). Encrypt this text
using 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 keep
prompting the user until they enter text that only contains letters. Encrypt this plaintext
using your vigenereEncrypt() function.
o Prompt the user for text to decrypt, make sure that it is valid, and keep prompting the
user 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” when
prompted for plaintext or ciphertext, the program should not end.
• After completing the Caesar Shift or Vigenère Cipher selection, the program should
prompt 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 should
ignore 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()

Contact Us - Email:99515681@qq.com    WeChat:codinghelp
Programming Assignment Help!