Java Reference
In-Depth Information
Programming Projects
Many of these Programming Projects can be solved using AW's CodeMate.
To access these please go to: www.aw-bc.com/codemate .
1.
Write a program that outputs the lyrics for “Ninety-nine Bottles of Beer on the
Wall.” Your program should print the number of bottles in English, not as a
number. For example:
Ninety-nine bottles of beer on the wall,
Ninety-nine bottles of beer,
Take one down, pass it around,
Ninety-eight bottles of beer on the wall.
One bottle of beer on the wall,
One bottle of beer,
Take one down, pass it around,
Zero bottles of beer on the wall.
Your program should not use ninety-nine output statements!
Design your program with a class named BeerSong whose constructor takes
an integer parameter that is the number of bottles of beer initially on the wall.
If the parameter is less than zero, set the number of bottles to zero. Similarly,
if the parameter is greater than 99, set the number of beer bottles to 99. Then
make a public method called printSong that outputs all stanzas from the
number of bottles of beer down to zero. Add any additional private methods
you find helpful.
2.
Define a class called Fraction . This class is used to represent a ratio of two inte-
gers. Include mutator functions that allow the user to set the numerator and the
denominator. Also include a member function that returns the value of numera-
tor or denominator as a double. Include an additional member function that
outputs the value of the fraction reduced to lowest terms (e.g., instead of output-
ting 20/60 the method should output 1/3). This will require finding the greatest
common divisor for the numerator and denominator, then dividing both by that
number. Embed your class in a test program.
3.
Define a class called Odometer that will be used to track fuel and mileage for an
automobile. The class should have member variables to track the miles driven
and the fuel efficiency of the vehicle in miles per gallon. Include a mutator func-
tion to reset the odometer to zero miles, a mutator function to set the fuel effi-
ciency, a mutator function that accepts miles driven for a trip and adds it to the
odometer's total, and an accessor method that returns the number of gallons of
gasoline that the vehicle has consumed since the odometer was last reset.
Use your class with a test program that creates several trips with different fuel
efficiencies. You should decide which variables should be public, if any.
Search WWH ::




Custom Search