0% found this document useful (0 votes)
59 views3 pages

(Key) Csa U4l6 Object Aliases and Equality Extra Practice

Uploaded by

Zeyna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

(Key) Csa U4l6 Object Aliases and Equality Extra Practice

Uploaded by

Zeyna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSA Unit 4 Lesson 6

Name(s) __________________________________________________ Period _______ Date _____________________

[KEY] Extra Practice - Object Aliases and Equality

Check for Understanding

Assume dessert1 and dessert2 are object references. The following code segment is run, and returns true.

dessert1 == dessert2

What assumptions can be made about dessert1 and dessert2?

A. Either dessert1 or dessert2 must be null.

B. Both dessert1 and dessert2 must be null.

C. dessert1 has an overridden equals method.

D. dessert1 and dessert2 are aliases.

1
AP Exam Prep

Consider the code segment below.

public class Marker {


private String color;

public Marker(String c) {
color = c;
}

public string getColor() {


return crayon;
}

public boolean equals(Object other) {


if (other == null) {
return false;
}

Marker temp = (Marker) other;


return (temp.getColor().equals(this.color));
}
}

The following code segment appears in a class other than Marker. Assume that a and b are properly declared
and initialized String variables.

Marker crayon = new Marker(a);


Marker sharpie = new Marker(b);
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 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

Accessor methods for:

● ID

An equals() method which compares two trees based on their ID.

One possible solution:

public class Tree {


private String species;
private int ID;

public Tree(String species, int ID) {


this.species = species;
this.ID = ID;
}

public String getID() {


return ID;
}

public boolean equals(Object other) {


boolean isEqual = false;

if (other != null) {
int otherID = ((Tree) other).getID();
isEqual = (ID == otherID);
}

return isEqual;
}
}

You might also like