调用计算方法

时间:2016-10-18 16:56:28

标签: java

所以我做了一个应该计算醉酒所需啤酒数量的课程。我的班级会收到用户输入,其中包含啤酒名称,酒精含量,以及用户的体重以进行计算。

这是我的整个啤酒课

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;
}

这是我在课堂上特别陶醉的方法(我认为是对的):

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

这就是输出窗口应该是什么样子,接收用户输入的权重,然后执行计算以根据用户在先前定义两个时的输入来查看需要多少啤酒啤酒被认为是陶醉的:

  

饮用这种饮料的人的体重是多少?

     

185

     

需要3.166&#34; firstBeerName&#34;啤酒变得陶醉。

     

需要1.979&#34; secondBeerName&#34;啤酒变得陶醉。

给了我陶醉的公式,我不知道如何正确设置我的类测试主方法文件,该文件调用此类来反映该输出。

4 个答案:

答案 0 :(得分:0)

您需要编写一个包含main方法的测试类。在main方法中,您可以创建多个Beer - 对象。

通过迭代您的啤酒,您可以获得想要的结果。

  • 查看here以获取有关如何设置主方法的信息。
  • 使用不同的酒精含量
  • 创建Beer intoxicated() - 该方法中的对象
  • 获取重量的Array,然后
  • 迭代您的数组,致电template: "#=highlighter(data.ChoiceCode, data.mychoice, data.secondChoiceCode) #" 并打印结果

答案 1 :(得分:0)

我建议你在setter中检查条件时抛出IllegalArgumentException:

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.");
    }
}

对于你的问题,你可以这样测试:

class $class_name$:
    def __init__(self, $arg1$, $arg2$):
        self.$arg1$ = $arg1$
        self.$arg2$ = $arg2$
$END$

此外,您可以使用循环创建新啤酒,只需向用户询问他可以获得结果的啤酒数量,然后创建循环。

答案 2 :(得分:0)

你可以写一个这样的主方法:

{{1}}

答案 3 :(得分:0)

您将要创建一个main方法,该方法执行以下操作:

1)打印啤酒值(名称和%酒精)的提示

2)获取那些啤酒值的用户输入

3)打印用户体重的提示

4)获取权重的用户输入

5)计算并打印结果

对于打印提示,您很可能希望使用System.out.println("Some prompt here!");

为了获取输入,您很可能想要使用Scanner。您可以在本网站和其他网站上搜索,以及阅读文档,了解如何使用该类进行输入。

以下是主要方法的示例:

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.");

}

我建议将Intoxicated方法重命名为intoxicated,因为方法名称通常是用Java编写的。

我不会给你确切的代码,因为这看起来像是作业,我已经毕业了,但这应该足以让你开始。我的建议是搜索你提出的任何具体问题。