Lab 2: Fundamental Data Types
CS 141: Introduction to Programming,
Fall 2017
This lab is due Friday, Sept 8 by the end of lab.
For this lab, we will use pair programming.
You will be assigned a partner. If there are an odd number of people, then the extra person may choose to
work alone or create a group of 3.
Here is how it works [see wiki]:
"Pair programming is an agile software development technique in which two programmers work together at one workstation. One, the driver, writes code while the other, the observer or navigator, reviews each line of code as it is typed in. The two programmers switch roles frequently.
While reviewing, the observer also considers the "strategic" direction of the work, coming up with ideas for improvements and likely future problems to address. This frees the driver to focus all of his or her attention on the "tactical" aspects of completing the current task, using the observer as a safety net and guide."
Programming Problems
Do the following problems. Create a separate Netbeans project for each.:
- Word Game: Create a Mad Lib.
Either use a story you found online (e.g. Letter From Home)
or make one up your own. You will need to prompt the user for the words and store their inputs into String variables.
You then need to output the story with the words properly inserted.
- p. 172, P4.9: Giving Change. This is tricker to do than you might think. Be careful with rounding and output format.
Amount Due: $ 11.71
Amount Received: $20.00
Your total change is $8.29
dollars = 8
quarters = 1
dimes = 0
nickels = 0
pennies = 4
The Process: Before getting near a computer, do the following with your partner:
- Understand the problem. Pull out some coins. Think about how you would figure out the change.
- Write out a strategy in pseudocode, e.g. :
- Prompt user for amounts due and received. Compute the change.
- Convert the change to cents, stored in an integer variable: (cents = 1171).
- Do an integer divide by 100 to get the dollars. (result: dollars = 11)
- Use the "mod" function to get the remainder. (remainder = 71) In Java, the symbol for the mod function is %.
- Do an integer divide of the remainder by 25 to get the quarters. ( quarters = 2)
- Use the "mod" function to get the new remainder. (remainder = 6)
- Repeat for dimes, nickels, pennies. (in this example, the result should be 0 dimes, 1 nickel, 1 penny)
- Print out the results so it looks similar to the aoutpu given above.
- Don't start coding until the above makes sense.
- When you do start coding, follow the process for pair programming. You can either use a timer (e.g. every 10 minutes) to determine
when to swap places, or you can decide on how many steps (e.g. 2-4) to complete before swapping.
Develop Good Programming Habits
Formatting: Make it a habit to format your Java code. Do this as you are working because it will make it easier to see the program structure which, in turn,
will make it easier to locate and debug errors.
Commenting: Make it a habit to comment your code, including
placing the following at the top of your code:
- Your name.
- Name of lab (i.e. Lab 2).
- Name of the problem (e.g. Word Game).
- Date.
- A brief description of what the program does.
Deliverables
You are to turn in the Giving Change program. To receive full credit on this lab, you need to:
-
Make sure that your Java code:
- Is well commented.
- Is well formatted, i.e. proper indentation and line returns.
- When executed, the Input/Output (I/O) is formatted as shown in the sample runs. (If you use dialog boxes instead of
the Scanner object, the user input will be a little different).
-
Demonstrate your program to the instructor or lab assistant by the end of lab on the due date. You will be asked to
display your commented/formatted code and to run the code. If your code is not properly formatted or does not give the appropriate output,
you will be given the opportunity to correct the code but only if there is still time to make corrections before the due date/time.
This is why it is to your benefit to complete and demonstrate your code early.
You also may be asked questions about what the code is doing, how it works, and
how it might be changed slightly to alter the output.
-
Submit your code to Wise:
- For this lab, you only need to submit the .java file(s). That is, close Netbeans and navigate to your Netbeans project folder.
Here, you will see the "src" folder which contains your .java file. Find the .java file
and attach it your Wise submission. Include your partner's name both in the code and
in the inline section of the WISE submission.
It is also suggested that you delete any old java files in the src folder that are not part of the
final program.