Copyright RIT 2005
$Id: writeup.xml,v 1.2 2009/10/06 20:03:55 vcss231 Exp vcss231 $
The purpose of this lab is to give you some experience with writing conditional statements and repetition statements in Java.
You are to work on this lab completely on your own.
This lab consist of two activities that will give you some practice with writing conditional statements and loops to solve simple problems.
For the first activity, you will write a program that converts binary integers into their decimal equivalent. For example, the binary integer 1101 is equivalent to the decimal integer 13.
For the second activity, you will write a program that prints out the lyrics to the song "The Twelve Customer Bug Reports" (a parody of a familiar holiday song). Note that it would be possible to simply have the program output one long string, or a series of shorter strings, with the lyrics as the string(s). If your program does this, you will not receive any credit for this activity. Instead, to get credit, your program must use loops and conditional statements to handle the repetitive parts of the lyrics.
Read this entire document.
Write a program called BinaryConverter.java
that prompts a user for a binary
integer (one that consists of only 1s and 0s),
converts that integer to its decimal equivalent, and
prints the result. Your program should continue to
accept input until the user enters a single '0', which
will end the program. The user will enter a binary
integer with a maximum of ten digits. You may assume
that the user entered a valid binary integer of not
more than ten digits.
To do the conversion, recall that every position in a binary digit represents a power of two. For example, the binary digit 1101 can be expressed in decimal as:
|
Note that you can "pick-off" binary digits from right to left by finding the remainder after dividing the integer by 2. If the remainder is '1', then subtract 1 from the integer. Next, divide the integer by 10 to "pick-off" the next digit. Continue to do this until the integer is 0. Each time you get another digit, you should multiply it by the appropriate power of two and add it to the result so far.
Here is a sample run of the program when it is working correctly.
% java BinaryConverter Enter a binary integer, at most 10 digits long, 0 to quit: 1101 1101 converted to decimal is 13 Enter a binary integer, at most 10 digits long, 0 to quit: 0 |
When you have compiled your code and tested it to your satisfaction submit it using the following command:
try grd-231 lab5-1
BinaryConverter.java
Write a program called TwelveDays.java
that prints out the lyrics to a parody song of a
popular holiday tune of a similar title.
you can find the complete lyrics at
Auxiliary/TwelveDays.out
Note you will not receive
credit for this activity if you simply print out one
long string, or a series of shorter strings, for the
lyrics. You must use loops and conditionals to
eliminate any repetition in the lyrics. For example,
you could use a 'for' loop to print out each verse,
as the verses are very similar to one another, and
there are a fixed number of them. The
first line of every verse begins with
"For the " followed by the day, followed by
"customer bug reports, my manager said to me:".
To indicate the day, you could
print out a string that corresponds to the iteration
number of the loop.
The final set of lines of a verse is
again going to depend on the iteration number of the
loop. Write your code such that each selection
statement is associated with only one
"gift", i.e., make your code as short as
possible. (hint: think about how the switch
statement works).
Here is a sample run of the program when it is working correctly. Only the first couple, and last verses are printed for brevity, but the rest of the lyrics follow the same format. You do not need to worry about centering the text, but separate each verse with a blank line.
One last note, pay particular attention to the first line of the song.
For the first verse it begins with a "See if they..." but for the
second, and following verses, the line begins with "And see if
they...".
% java TwelveDays The Twelve Customer Bug Reports For the first customer bug reports, my manager said to me: See if they can cause the crash again. For the second customer bug reports, my manager said to me: Ask them how they did it And see if they can cause the crash again. For the third customer bug reports, my manager said to me: Try to reproduce it Ask them how they did it And see if they can cause the crash again. . . . For the twelfth customer bug reports, my manager said to me: Tell them it's a feature Say it's not supported Change the documentation Blame it on the hardware Find a way around it Say they need an upgrade Reinstall the software ** Ask for a core dump ** Run with the debugger Try to reproduce it Ask them how they did it And see if they can cause the crash again. |
When you have compiled your code and tested it to your satisfaction submit it using the following command:
try grd-231 lab5-2
TwelveDays.java
Grade Breakdown: