Can someone please help me solve this getter and setter Java program? Thank you!
ID: 3587891 • Letter: C
Question
Can someone please help me solve this getter and setter Java program? Thank you!
Create and test a Beer class:
Define a Beer class that contains the following private instance variables, both having accessors and mutators (getters and setters):
String name; // the beer’s name and
double alcohol; // the alcohol %, eg, .05 for 5%
Also add the following method to calculate the number of 12 ounce drinks of beer required for a person of a given weight to become intoxicated at .08 blood alcohol – this is the only method needed beyond the getters and setters:
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;
}
Write code in a main method that creates two Beer objects with different alcohol percentages. For each Beer object, invoke (call) the intoxicated() method for a light individual and a heavy individual and output the estimated number of drinks for them to become legally intoxicated. “Light” could mean 100 pounds, “heavy” 200 pounds, but you should choose those values.
Explanation / Answer
BeerTest.java
public class BeerTest {
public static void main(String[] args) {
Beer b1 = new Beer();
b1.setAlcohol(0.05);
b1.setName("AAAAA");
Beer b2 = new Beer();
b2.setAlcohol(0.05);
b2.setName("BBBBB");
double in1 = b1.intoxicated(100);
double in2 = b2.intoxicated(200);
System.out.println(b1.getName()+" intoxicated "+in1);
System.out.println(b2.getName()+" intoxicated "+in2);
}
}
Beer.java
public class Beer {
private String name; // the beer’s name and
private double alcohol; // the alcohol %, eg, .05 for 5%
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAlcohol() {
return alcohol;
}
public void setAlcohol(double alcohol) {
this.alcohol = alcohol;
}
}
Output:
AAAAA intoxicated 2.111111111111111
BBBBB intoxicated 4.222222222222222