Problem Solving Assignment 4


Due Oct 17, midnight

There are two problems in this PSA. Here is the overview. Note, there is an interview process required for this PSA. Details at bottom after turning instructions. This work should be done in it's own directory (psa4).

This homework requires skills from Chapters 1,2, and 3.

  1. Perfect Numbers Write a program that determines if an integer is a perfect number. A number, n, is a perfect number if the sum of its positive factors (other than itself) sum to n. This code should be written in a file called Perfect.java.
  2. Ceasar Cipher Write a program that reads in from the user a string consisting of spaces and lowercase character, and encrypts the string using a Caesar cipher. This code should be written in a file called Ceasar.java.

Details

You must put a comment at the top of your code with the following three things in it: Use a multi-line comment (/* */).

You must observe an 80 column limit. No line of your program may be longer than the 80th column on the screen.

You MUST name your files as indicated in the problem descriptions for the turnin program to work.


Perfect Number

Your file must be called Perfect.java.
Your output must match the sample program output exactly (in terms of content and format). This allows the graders to check for your program's correctness more efficiently, and spend more time giving you feedback. See below.

Write a program which reads in from the user, with prompting, a positive, non-zero integer, and determines if that integer is a perfect number. A number, n, is a perfect number if the sum of its positive factors (other than itself) sum to n. For example, 6 is a perfect number, since its positive factors, 1, 2, and 3, sum to 6. Remember that a number x is a factor of another number y if x divides evenly into y. Your program should display to the screen the answer with an appropriate message. Your program can assume that the user will enter an integer when prompted, but you cannot assume that a positive integer will be entered. So your program should contain a validation loop that continues to prompt the user until they enter a positive integer. The name of the class you define in your program must be Perfect. Want to know other perfect numbers to test? Try the web!

NOTE: This code only needs to work on variables of the type int However, you might want to try a large number and see how long it takes!


Ceasar Cipher

Your file must be called Ceasar.java
Your output must match the sample program output exactly (in terms of content and format). This allows the graders to check for your program's correctness more efficiently, and spend more time giving you feedback. See below.

Background:

A cipher takes a sequence of characters called the "plaintext" and returns another sequence of characters called the "ciphertext" which is (a) apparently hard to read, but (b) can be converted back into the original plaintext by following a certain algorithm.

A substitutional cipher works by substituting for each letter of the alphabet another particular letter of the alphabet; recovering the plaintext then just involves doing the reverse substitution. In this assignment, you will implement a kind of substitutional cipher called a rotational cipher or Caesar cipher (check out this for more on codes, ciphers, and codebreaking).

Substitutional ciphers are not considered very good ways to hide information that you really want to keep secret; they are easy to 'break'. There are many better ways of securely encrypting sensitive information these days. But still, substitutional ciphers are useful in some contexts; for example, the "rot 13" rotational cipher is commonly used to obscure (usually controversial or potentially offensive) text in email or newsgroups on the Internet.

Directions: Write a program that reads in from the user a string, and encrypts the string using a Caesar cypher. The Caesar cypher shifts every letter down the alphabet 4 characters, with wraparound. For example, every 'a' character is changed to an 'e', every 'M' is changed to a 'Q', every 'y' is changed to a 'c'. Notes:


How SHOULD our programs really work?

You can copy working solutions for both assignments using the command:

cp ~/../public/PSA4/*.jar .
This will put 2 .jar files in your current directory. These are different from class files, though they also execute a code. To run the code (for example SamplePerfectOutput.jar type:
java -jar SamplePerfectOutput.jar
YOUR CODE SHOULD PRODUCE IDENTICAL OUTPUT TO THESE CODES, IN TERMS OF FORMATS AND ANSWERS. This is important in order to allow our graders to do the best job of grading your code.

How to turnin your homework electronically

When you are ready to turn in your homework, run bundle-psa4 from your psa1 directory. The session should look like this:

-bash-2.05b$ cd ~/ps4a
-bash-2.05b$ bundle-psa4
Good; all required files are present:

        1 .     Perfect.java
        3 .     Ceasar.java

Do you want to go ahead and turnin these files? [y/n]y
OK.  Proceeding.


Performing turnin of approx. XXXX bytes 
Copying to /home/linux/ieng6/cs8f/turnin.dest/cs8fzz.psa4
.
Done.
Total bytes written: XXXX 
Please check to be sure that's reasonable.
Turnin successful.

If you want to make changes after you've turned in your homework, make the changes and run bundle-psa4 again. The program will warn you that a "previously turned-in file exists" and ask if "you wish to over-write this existing file". Respond in the affirmative by typing y and pressing the Enter key.

Getting your interview done

15% of your grade on this assignment will come from an interview you will have with one of the tutors during Open Lab Hours. You have 48 weekday hours after the electronic turnin deadline to get your interview done. The deadline for the interview for PSA4 is Wed Oct 19 (theoretically at midnight, but actually by the end of the last tutor hour scheduled).

You get an interview by going into B250 during a scheduled tutor hour. Find the tutor, ask them if you can interview for PSA4. If they have others who are waiting for interviews, your name will go on the end of the list of people waiting. Interviews take about 5 minutes each. It is your responsibility to go in early enough that you can get your interview completed. If everyone waits until the last hour on Wed, this will not work.

The tutor will ask you to log in, change to the directory where your work is (psa4) and then to follow their directions. They will ask you to run your program to show that it works, open files in your favorite editor, and ask you questions about HOW your code works. Additionally, they may question you as to how you would (theoretically) make small changes to the program to have it do something slightly different. You will not actually be required to change your code.

Learning to be able to explain your problem solving process and discuss code with another human being is a very important professional skill. We hope that this process will help you develop this skill.