Given the CoffeeDrink class defined by two Ingredient objects contained in it (coffee and milk) as well
Question:
- Given the CoffeeDrink class defined by two Ingredient objects contained in it (coffee and milk) as well as the Ingredient class representing an ingredient in terms of its name and its strength. Strength is an integer between 1-5, 5 being the highest strength. The lower the ratio of milk strength to coffee strength, the stronger the coffee drink.
Implement a method testCoffeeDrinks that takes one parameter: - drinks, an non-empty array of CoffeeDrink objects The method finds and prints to the screen the strongest drink (You must use the compareTo method for full credit). If more than one such drink occur in the array, pick the first one. The method also creates, populates and returns an array of integers containing the frequency of each possible coffee strength (1-5), in the drinks array. For example, if the information of the drinks in the array is given in the table below:
Coffee drink num | Ingredient 1 | Ingredient 2 |
0 | Coffee, 4 | Milk, 3 |
1 | Coffee, 2 | Milk, 3 |
2 | Coffee, 2 | Milk, 1 |
3 | Coffee, 4 | Milk, 1 |
4 | Coffee, 3 | Milk, 2 |
5 | Coffee, 4 | Milk, 4 |
The program will display:
coffee strength:4
milk strength: 1
And return the array {0,2,1,3,0}
Given:
public class Ingredient{
private String name;
private int strength;
public Ingredient(String name, int strength) {
this.name=name; this.strength=strength;
}
public int getStrength()
{
return strength;
}
public String toString() {return name+" strength: "+strength;}
}
public class CoffeeDrink
{
private Ingredient coffee, milk;
public CoffeeDrink(Ingredient i1, Ingredient i2)
{
this.coffee=i1;
this.milk=i2;
}
public Ingredient getIngred1()
{
return coffee;
}
public Ingredient getIngred2()
{
return milk;
}
// compares two drinks based on their milk/coffee ratio.
//drink 1 is stronger than drink 2 if that ratio is smaller than
// the corresponding ratio of drink 2
public int compareTo(CoffeeDrink other)
{
if ((double)milk.getStrength()/coffee.getStrength() > (double) other.getIngred2().getStrength()/other.getIngred1().getStrength())
return 1;
else if ((double)milk.getStrength()/coffee.getStrength() < (double) other.getIngred2().getStrength()/other.getIngred1().getStrength())
return -1;
else return 0;
}
public String toString() { return coffee.toString()+" "+milk.toString();}
}
Business Law Principles for Today's Commercial Environment
ISBN: 978-1305575158
5th edition
Authors: David P. Twomey, Marianne M. Jennings, Stephanie M Greene