There are two problems in this PSA.
Notes:
This IS a pair assignment. You MUST do the assignment with your partner. You must enter your partner work schedule (days/times when you will work together) on the webpage form by 6pm Friday or be lose 10% of the points for PSA2!
You must complete an interview with a Lab Tutor for this PSA. Interviews are individual (partly to ensure that one partner doesn't do all the work). See instructions here for more details.
This work should be done in its own directory called 'psa2'. Instructions here.
This assignment has sample solution code that you can run on ieng6. You won't be able to see the java code itself =) but you will be able to do tests to see what it does, and make sure yours does the same thing. YOUR CODE MUST DO THE EXACT SAME THING. EXACT!! Wording, spaces, everything. Instructions on how to run the sample solution code are here .
This homework requires skills from Chapter 1 and Chapter 2.
Make a vending machine change calculator:
Your program must be in a file called ChangeMaker.java
Pretend this is for a vending machine. Ask the user to input an integer, representing the price of the item they are purchasing in pennies (so $1.58 would be 158).
The vending machine is designed to be really, really inconvenient, so it ONLY accepts one $2 bill per purchase. So the items for sale all have a price between 1 and 200 cents.
The machine can dispense change as dollar-valued coins (Susan B. Anthony, Sacajawea), quarters, nickels, imaginary coins called duos that are worth 2 cents each, and pennies. (NO dimes)

Output the number of each of these coins that the user should get as change (assuming they already paid their $2). Do this using the most possible of the highest-value coins (in other words, no fair just giving back 150 pennies if the user buys something that costs 50 cents--it should be 1 1-dollar coin, and 2 quarters).
Before you write any code, think about the basic algorithm that will solve this problem. It is a good idea to sketch the algorithm in pseudocode first. When you are confident that your algorithm will solve the problem, start up an editor and start coding!
You can see a sample solution by following these instructions.
Make a MadLib generator.
Your program must be in a file called MadLib.java
The point is to make a funny paragraph by filling in words provided by someone who doesn't know the context the words will be used in. To see how a MadLib generator works, try out one on this website. First, play the game as instructed. Then try leaving all the boxes blank and just hit the Generate Madlib button to see what the original words were. (by the way, yours won't have the option of printing the original words like this)
(There are a lot of details in this description, but don't despair, it is pretty straightforward.)
The text you will be working with is a set of tips for safe driving from the State of Illinois:
ADJECTIVE1 driving is the operation of a motor vehicle in a manner that VERB1s or is likely to VERB1 persons or property. Persons doing any of the following may be committing acts of ADJECTIVE1 driving:ADJECTIVE1 driving is a serious problem that is responsible for many traffic accidents and NOUN2. It is to your benefit to avoid ADJECTIVE1 drivers and potentially dangerous situations.
- VERB2
- Running red lights and stop signs
- VERB3
- Cutting off another vehicle
- Slamming on NOUN1 in front of a tailgater
- Improper hand or facial gestures at other drivers
Ask the user to provide the following words, typed in all lower case:
Adjective
Verb in present tense (eat)
Verb that ends in -ing (eating)
Verb that ends in -ing
Noun
Abstract Noun (this is basically what it sounds like, but go here if you want to learn more)
Save all these words in String
variables: adj1, verb1, verb2, verb3, noun1, noun2 (notice these names match those in the paragraph above, that's where they go).
To make your job a little easier, we are providing you with starting java code that contains the paragraph (with gaps where the MadLib words will be inserted) already typed in. Instructions for getting this code on ieng6 are here. Comments in the code (marked like this /* comment */) show where each word should be inserted. Note that adj1 and verb1 are used more than once. The paragraph is divided into lines, and should be output that way. You can tell which strings go on which lines by the number in the variable name. So for example:
line 7 should read: basestring7a noun1 basestring7b, all on one line.
then the next line will just be basestring8.
Output the paragraph to the screen, with the MadLib words inserted where they belong. Again, this must look EXACTLY, EXACTLY like the sample solution.
Do this much first, and make sure it is working, before continuing.
So far you have been using skills that you already practiced in PSA 1B. Now we are going to try learning about different methods in the String class. In your book (page 40-42) there is a list of methods in the String class.
So, there are a couple tricks:
As you noticed, adj1 was used multiple times in the paragraph. Sometimes, it was used as the first word in a sentence, sometimes not. The user is entering all the words in lower case, so for your MadLib to look right, you need to capitalize the first letter in adj1 for the times it is at the start of a sentence (but don't capitalize the other times).
verb1 is used twice. In the original paragraph, the phrase was, “in a manner that endangers or is likely to endanger.” Notice that the first use of the verb1, it has an 's' added to it, and no 's' in the second. The user should have entered a verb with no s, so you need to add it in the appropriate place (Update 1/17 9:45pm: Rule: no editing of the basestring variables (that contain the driving tips paragraph) in order to do this).
Test it out on your friends!
Start by creating a directory (folder) in your ieng6 cs8fzz account (this is necessary for the bundle-psa2 script). After you log in, create a directory by typing the commands:Go into that directory (like double clicking to open a folder) with the command:-bash-2.05b$ mkdir psa2Note: you must name your directories (folders) and files as indicated in the problem descriptions for the turn in program to work.-bash-2.05b$ cd psa2
-bash-2.05b$ cp ~/../public/MadLib.java ~/psa2
Note that your psa2 directory has to be made already.
In order to run the sample solution program you should invoke the following command:
So for this assignment, do:-bash-2.05b$ runsample [classname]
or-bash-2.05b$ runsample ChangeMaker
-bash-2.05b$ runsample MadLib
This assignment is worth 20 points.General (4 pts):
- 1 Point: For proper formatting in both ChangeMaker.java and MadLib.java. Proper formatting means that your code is properly and consistently indented.
- 1 Point: For program documentation. You must write a 2-4 sentence description of the program (as a comment) that provides insight into what the program does and what it's useful for (in both ChangeMaker.java and MadLib.java). In MadLib.java, you already have a starting point, but it should be expanded. Also, add your names as authors.
- 2 points: Interview (done individually) see instructions here.
ChangeMaker.java (8 pts):
- 1 point: Ask user to input price and take user's input as an integer number of pennies.
- 1 point EACH: output correct number of dollar, quarter, nickel, duo and penny coins.
- 2 points: Everything matches sample solution exactly (wording, format, etc).
MadLib.java (8 pts):
- 1 points: Ask user to input words and take user's input.
- 4 points: Output words in correct places, matching sample solution output for where a new line starts, number of spaces between words (always 1 space), etc. (but not including capitalization of adj1, and adding 's' to verb1)
- 2 points: Capitalize adj1 in correct places (and not in others).
- 1 points: Add 's' to verb1 in correct place.
When you are ready to turn in your homework, runbundle-psa2from yourpsa2directory. The session should look like this:-bash-2.05b$ cd ~/psa2 -bash-2.05b$ bundle-psa2 Good; all required files are present: ChangeMaker.java MadLib.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/cs8w/turnin.dest/cs8wzz.psa2 . 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-psa2again. The program will warn you that a "previously turned-in file exists" and ask if "you wish to over-write this existing file" (delete the old version and replace it with this new one). Respond in the affirmative by typingyand pressing theEnterkey.
10% 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 PSA 2 is Wednesday, Jan 23, theoretically at midnight, but actually by the end of the last tutor hour scheduled. You get an interview by going into B260 during a scheduled tutor hour. Find the tutor, ask them if you can interview for PSA 2. 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. 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.
Note: Copying the assignment directory to partner's home directory.
You can copy the psa2 directory and its files to your partner's home directory by:(don't forget the colon at the end of the command) where cs8wxx is your partner's username. Your partner is required to enter his password.
bash% scp -r psa2 cs8wxx@ieng6.ucsd.edu:
Vim
Run the commandvimtutor, a separate program that will teach you to use Vim.Emacs
Use the built-in tutorial by runningemacsand, within the editor, typingCTRL-h t.Pico
You can reach Pico's built-in help by typingCTRL-g. If you want further instruction, look at this Pico tutorial from NC State.