CSAlpha Assign2: User Input and Advanced Control Flow

Create a file named assign2.txt.

You will use this file to answer questions and paste your code.

During this week's lesson, we covered the following topics:

  1. Scanner class and setup
  2. Scanning and using user input
  3. While loops
  4. For loops
  5. Switch statements

From now on, the console is your best friend. Future assignments require you to interact with your programs via user input, so please get comfortable and familiar with Scanners.

Loops are used for doing the same task repeatedly many times, with or without iterating through a certain set of values. Switch statements come in handy when you have many different cases that need to handled in different ways, such as the calculator program from Assign1.

 

Written Questions

  1. Look at the following program:

    Which two lines of code are missing? Hint: one line of code is essential for the program to run, and Eclipse will show a warning without the other line of code.

  2. Describe the behavior of the code below:

For the following examples of control flow, state the expected output.

  1. Sequential loops

  2. Nested loops

  3. Interesting switch statement; please give the expected output if input is equal to zero, one, or two.

Paste your answers in assign2.txt.

 

Core Assignment: Caesar Cipher Console

In this assignment, you will implement a console program that encodes and decodes sentences using a Caesar Cipher. A Caesar Cipher uses an alphabet shifted by a certain amount for encoding and decoding. For example, in a cipher with shift +4, the letter 'a' becomes 'e' because 'e' is four letters ahead of 'a'. Similarly, in a cipher of shift -1, the letter 'a' becomes 'z' instead. Have fun!

 

Core Input Loop

Unlike the calculator from Assign1, your cipher console should run until you tell it to stop. That is, you can keep decoding or encoding input without the program abruptly terminating.

To do this, you need a loop, so pick a suitable type of loop based on the behavior described above. Inside the loop, prompt the user for a command and then scan for user input. Please create your Scanner object outside of the loop.

Here are the commands that your program should be able to accept:

If the user types a command that doesn't match either of these, print out "Invalid command." and ask for another command.

Use the most suitable control flow structure to change the program's response for each of these user commands. Upon "quit", your program should immediately terminate. There are a few ways to do so; play around with variables, loop conditions, and jump statements.

Something you might want to consider is how to break out of multiple loops at once. Try to use this structure as little as possible, but follow the format below if it is essential.

 

 

Encoding and Decoding

After a user types "code", your program should begin prompting the user for the required values. In this case, you need to prompt the user for a sentence, prompt the user for a shift amount, encode or decode the sentence with the desired shift, and then print the result to the console.

Note: Encoding and decoding are the same process except with different shift amounts, which means you won't need to code two separate programs for encoding and decoding. For example, if you shifted a sentence by 4, then you shift it by -4 to get the original sentence.

To parse integers from a user input String. you will need this line of code: Integer.valueOf(String str). It returns the integer value of a String, which means that you can 'assign' this line of code to a variable for later use. Place the user input inside the parentheses to convert it.

For example:

Warning: If the user input String is not an integer and you attempt to convert it, your program will crash. Do not fear; we will learn more about handling these errors in Week 9. For now, you aren't required to handle the error.

Once you have both the user input and the shift amount, you can encode or decode the sentence. To do so, you will need the following pieces of code:

If you would like to know how the shift formula works, read more about the ASCII values of characters. The final step of the formula is to cast the resulting value back into a char; you'll learn that it is unnecessary to do so when assigning the value to a char variable in later weeks of the course. For now, however, use the formula as is.

Once your code is finished, shift the sentence below by -105 to make sure your program is working properly: dpohsbuvmbujpot boe hsfbu xpsl! zpvs dbftbs djqifs dpotpmf jt dpnqmfuf.

Paste your code in assign2.txt.

 

Assignment Extension

For this assignment, you can pick one of the two extensions below. Please do these extensions in a separate class from your core assignment; the extensions may share similar code with the Caesar Cipher (which you can copy over), but their overall structure will be different from a Caesar Cipher.

 

Substitution Cipher

A substitution cipher replaces each letter of the alphabet with another letter or symbol. For example, consider the following substitution cipher example:

In a substitution cipher, the creator manually selects the letters and symbols that replace the alphabet. Create a program that allows the user to create a substitution cipher of their own with ability to cipher and decipher sentences with it.

Requirements:

Unfortunately, since we have yet to learn about arrays and since Map data structures are outside the scope of this class, you will have to store each of the new custom symbols as a char variable (Yikes! That's 26 separate variables). Once we reach arrays, you can refactor this code to be cleaner.

 

Morse Code

Morse Code converts each letter of the alphabet to a series of dots and dashes. For example, the word "hello" translates to .... . .-.. .-.. ---

Create a program that allows you to translate English to Morse code and Morse Code back to English.

Requirements:

Questions to ask while coding:

As with substitution cipher, you will have to store each of the Morse Code symbols in String variables to access during the program. Once we learn about arrays, you can refactor your Morse Code program to be cleaner.

Paste your code in assign2.txt.

 

Additional Practice

Calculator Upgrade

Your calculator from Assign1 utilizes a chain of if-else statements to change between different operations. However, a switch statement would be much more suitable for this purpose. Change out your if-else chain for a switch statement.

Instead of taking input from the variables in the code, modify your program to accept numbers and operations as user input from the console.

Finally, upgrade the calculation interface so that you can calculate as many expressions as you want before intentionally quitting the program. This is different from Assign1, which required you to press the run button every time you wanted to calculate something.

 

The Collatz Sequence

For any arbitrary positive integer n, apply the following operations until the integer is reduced to 1:

For example, for the number 6, the Collatz Sequence is as follows:

6

3

10

5

16

8

4

2

1

This is the basis of the Collatz Conjecture, which asks if this sequence results in 1 for every positive integer. This problem is unsolved because no one has found a way to check that it's true for every positive integer.

Your goal for this practice is to make a program that computes the Collatz Sequence for numbers 1 - 10 and prints out the process for each sequence.

Suggestions