Java Reference
In-Depth Information
Programming Projects
Visit www.myprogramminglab.com to complete select exercises online and get instant
feedback.
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 num-
ber. 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 methods that allow the user to set the numerator and the
denominator. Also include a method that returns the value of numerator divided
by denominator as a double . Include an additional method that outputs the value
of the fraction reduced to lowest terms (e.g., instead of outputting 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 instance variables to track the miles driven and
the fuel efficiency of the vehicle in miles per gallon. Include a mutator method
to reset the odometer to zero miles, a mutator method to set the fuel efficiency, a
mutator method 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