Due: Thursday, October 18
Goal: Introduction to Classes and Objects
Create a "Main.java" file that contains a main method with the code shown below. If you use Netbeans
then this can just be the default main method it creates for your project.
public class Main
{
public static void main(String[] args)
{
// For more beer alcohol content, see http://www.realbeer.com/edu/health/calories.php
Beer guinness = new Beer();
guinness.name = "Guinness"; // Name
guinness.alcohol = 0.074; // % alcohol for 12 oz
Beer alaskan = new Beer();
alaskan.name = "Alaskan Amber";
alaskan.alcohol = 0.05;
double drinks;
// Estimate of how many drinks until legally drunk in AK
// for Tinkerbell who only weighs 95 pounds
drinks = alaskan.intoxicated(95);
System.out.println("Tink at 95 pounds can drink about " + drinks +
" " + alaskan.name + " beers in one hour until intoxicated.");
// How many Alaskan Ambers that Bubba can drink
drinks = alaskan.intoxicated(250);
System.out.println("Bubba at 250 pounds can drink about " + drinks +
" " + alaskan.name + " beers in one hour until intoxicated.");
// How many Guiness' that Bubba can drink
drinks = guinness.intoxicated(250);
System.out.println("Bubba at 250 pounds can drink about " + drinks +
" " + guinness.name + " beers in one hour until intoxicated.");
}
}
Create a new class named Beer in a separate file named Beer.java. If you are using NetBeans then there is a menu option to create a new class and it will automatically put it in a separate file. In the new class make:
// This method returns the number of drinks that a person
// of (weight) pounds can drink using the alcohol percentage in this
// beer, assuming a drink of 12 ounces. It is an estimate. It assumes
// the legal limit is 0.08 BAC.
public double intoxicated(double weight)
{
double numDrinks;
// This is a simplification of the Widmark formula
numDrinks = (0.08 + 0.015) * weight / (12 * 7.5 * alcohol);
return numDrinks;
}
The program should compile and run. Note that we are making two Beer objects, one for Alaskan Amber and another for Guiness. This is called making two "instances" of the Beer class. Each instance has different values. In this case, one has the values for Guiness and the other has values for Alaskan Amber.
In the next drill we'll modify this program to make the instance variables private, so you might want to save your solution.
After you have a working program and understand how it works, show the working code to your instructor or email it to kenrick@uaa.alaska.edu by the end of the day.