Programming Drill #11

Due: Monday, October 22
Goal:  Introduction to Encapsulation

Start with your solution to the Beer program from Drill #10.

  1. Modify the Beer class so that the name and alcohol variables are private.
  2. Add public Accessor and Mutator (get and set) methods to the Beer class for the name and alcohol variables. In other words, add getName, setName, getAlcohol, and setAlcohol methods.
  3. Modify the main method to use the accessor/mutator methods. At this point the program should give the same outputs as before.

You have noticed a problem in usage. Some people are entering the alcohol percentage the wrong way. For example, if the alcohol percentage is 5% then some people are entering 5.0 instead of 0.05. As a result, the program tells people they will be legally intoxicated after a miniscule sip of beer.

Fix this problem by modifying the mutator method, setAlcohol. If the variable that is being set is over 1 then set the alcohol variable to the incoming value divided by 100. Otherwise set the alcohol variable to the incoming value. Test your program by setting the percent alcohol to 5.0 for Alaskan Amber in your main. The results should be unchanged.

After you have a working program and understand how it works, show the working code to your instructor or email it to by the end of the day.