Question: public class Beverage { private int temperature; public Beverage(int t) { temperature = t; } public int getTemperature() { return temperature; } public boolean equals(Object
public class Beverage
{
private int temperature;
public Beverage(int t)
{
temperature = t;
}
public int getTemperature()
{
return temperature;
}
public boolean equals(Object other)
{
if (other == null)
{
return false;
}
Beverage b = (Beverage) other;
return (b.getTemperature() == temperature);
}
}
The following code segment appears in a class other than Beverage. Assume that x and y are properly declared and initialized int variables.
Beverage hotChocolate = new Beverage(x);
Beverage coffee = new Beverage(y);
boolean same = /* missing code */;
Which of the following can be used as a replacement for /* missing code */ so that the boolean variable same is set to true if and only if the hotChocolate and coffee objects have the same temperature values?
-
A) (hotChocolate = coffee)
-
B) (hotChocolate == coffee)
-
C) hotChocolate.equals(coffee)
-
D) hotChocolate.equals(coffee.getTemperature())
-
E) hotChocolate.getTemperature().equals(coffee.getTemperature())
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
