(Key) Csa U4l6 Object Aliases and Equality Extra Practice
(Key) Csa U4l6 Object Aliases and Equality Extra Practice
Assume dessert1 and dessert2 are object references. The following code segment is run, and returns true.
dessert1 == dessert2
1
AP Exam Prep
public Marker(String c) {
color = c;
}
The following code segment appears in a class other than Marker. Assume that a and b are properly declared
and initialized String variables.
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 crayon and sharpie objects have the same color?
A. (crayon = sharpie)
B. (crayon == sharpie)
C. crayon.equals(sharpie)
D. crayon.equals(sharpie.getColor())
E. crayon.getColor() == sharpie.getColor()
2
Extra Practice
Do This: A citizen science project is looking to inventory all of the trees in New York City. They would like to
allow citizens to use an app to inventory trees based on species and location. They do not want two people to
enter the same tree into their system and will determine whether this has occurred by comparing the ID. Write
a class called Tree with the following specifications:
Instance Variables:
● species - String
● ID - int
● ID
if (other != null) {
int otherID = ((Tree) other).getID();
isEqual = (ID == otherID);
}
return isEqual;
}
}