-1

So I made a class that is supposed to calculate the number of beers needed to become intoxicated. My class receives User Input for the name of the beer, the alcohol content, and then the user's weight to make the calculation.

Here's my whole Beer class

public class Beer {

private String name;
private double alcoholContent;

//Default apple values (Constructors)
public Beer()
{
    this.name = "";
    this.alcoholContent = 0.0;
}
//Accessors
public String getName()
{
    return this.name;
}
public double getAlcoholContent()
{
    return this.alcoholContent;
}
//Mutators
public void setName (String aName)
{
    this.name = aName;
}
public void setAlcoholContent (double aAlcoholContent)
{
    if (aAlcoholContent < 0 || aAlcoholContent > 1)
    {
        System.out.println("That is an invalid alcohol content");
        return;
    }
    this.alcoholContent = aAlcoholContent;
}
//Methods
public double Intoxicated (double aWeight)
{
    double numberOfDrinks = (0.08 + 0.015) * aWeight / (12 * 7.5 * this.alcoholContent);
    return numberOfDrinks;
}

This is specifically my intoxicatedmethod in the class (I think it's right):

public double Intoxicated (double aWeight)
{
    double numberOfDrinks = (0.08 + 0.015) * aWeight / (12 * 7.5 * this.alcoholContent);
    return numberOfDrinks;
}

This is what the output window is supposed to look like, receiving User Input for the weight and then performing the calculation to see how many beers it would take based on the user's input when previously defining two beers to be considered intoxicated:

What’s the weight of the person consuming said beverages?

185

It would take 3.166 "firstBeerName" beers to become intoxicated.

It would take 1.979 "secondBeerName" beers to become intoxicated.

The intoxicated formula was given to me, I don't know how to properly set up my class testing main method file which calls this class to reflect that output.

Community
  • 1
  • 1
Chris M
  • 41
  • 9
  • It's not clear what you're asking for. You don't know how to write a `main` method? Or run a Java program? Or get user input? Or print output? Or instantiate a `Beer` object? Some combination of this? – DavidS Oct 18 '16 at 17:09
  • It looks like your primary obstacle is to get the user input, i.e. the weight of the person, in order to calculate the number of beers. You can refer to the linked question for that. – Tunaki Oct 18 '16 at 17:44

4 Answers4

0

You need to write a testing class, that contains a main method. In the main method you can create several Beer-Objects.

By iterating over your Beers, you can get the wanted results.

  • Look here to get information about how to set up a main method.
  • Create an Array of Beer-Objects in that method with different alcohol content
  • Get the user input for the weight and then
  • Iterate over your Array, call intoxicated() and print the results
Community
  • 1
  • 1
Dario
  • 582
  • 1
  • 5
  • 17
0

I would encourage you to throw IllegalArgumentException when checking condition in setter:

public void setAlcoholContent(double aAlcoholContent) {
    if (aAlcoholContent < 0 || aAlcoholContent > 1) {
        throw new IllegalArgumentException("Alcohol content can't be more than 1 or less than 0");
    }
    this.alcoholContent = aAlcoholContent;
}

And for your question you can test it like this:

public static void main(String[] args) {
    List<Beer> beers = new ArrayList<>();
    beers.add(new Beer("firstBeerName", 0.04));
    beers.add(new Beer("secondBeerName", 0.06));
    Scanner reader = new Scanner(System.in);
    System.out.println("What’s the weight of the person consuming said beverages?");
    double weight = reader.nextDouble();
    DecimalFormat decimalFormat = new DecimalFormat("0.000");
    for(Beer beer : beers){
        System.out.println("It would take " + decimalFormat.format(beer.Intoxicated(weight)) + " " + beer.getName() +" beers to become intoxicated.");
    }
}

Also you can use loop for creating new beers, just ask user for amount of beers that he can obtain result for, and then create for loop.

Kamil Banaszczyk
  • 1,133
  • 1
  • 6
  • 23
0

You can write a main method like this:

public static void main(String [ ] args)
{
Beer beer1 = new Beer().
beer1.setName("firstBeerName");
beer1.setAlcoholContent(3.166);

Scanner reader = new Scanner(System.in);  // Reading from System.in
System.out.println("What’s the weight of the person consuming said beverages?");
double weight = reader.nextDouble(); 

double answer = beer1.Intoxicated(weight);
System.out.println("It would take "+answer+" "+beer1.getName()+" beers to become intoxicated.")


// similar for beer2
}
donlys
  • 440
  • 6
  • 13
0

You are going to want to create a main method which does the following:

1) Prints the prompt for the beer values (name and % alcohol)

2) Takes in user input for those beer values

3) Prints the prompt for the user's weight

4) Takes in the user input for the weight

5) Calculates and prints the result

For printing prompts, you will most likely want to use System.out.println("Some prompt here!");

For taking input, you will most likely want to use a Scanner. You can search around on this website and others, as well as read the documentation, for how to take input with using that class.

Here is an example of a main method:

public static void main(String[] args) {
   Beer blueMoon = new Beer("Blue Moon", 5.4);
   Beer hoegaarden = new Beer("Hoegaarden", 4.9);

   System.out.println("Enter your weight: ");
   Scanner input = new Scanner();
   Double weight = input.nextLine();

   double value = beer1.Intoxicated(weight);

   System.out.println("It would take " + value + " of " + blueMoon.getName() + " to become intoxicated.");

}

I would suggest renaming your Intoxicated method to intoxicated, as method names are generally camelCased in Java.

I am not going to give you the exact code because this seems like homework and I already graduated, but that should be enough to get you started. My advice would be to search around for any specific questions you come up with.

Adam
  • 2,214
  • 1
  • 15
  • 26