E2.3 - CPSC 210 - PrairieLearn
E2.3 - CPSC 210 - PrairieLearn
Consider the domain of climate change agreements. Nations set targets that become parts of agreements.
Assume
the class Target has three fields agreement, carbonLevels, and timespan.
We want to implement equals in Target.
Two Targets should be equal as long as their agreement and carbonLevels
are the same, regardless of whether their
timespan is the same.
Given this, select each of the following that must be part of the code for the Target.equals method:
(a) refer to this.agreement
(b) declare its parameter's type to be Target
(c) refer to its parameter's timespan
(d) refer to its parameter's carbonLevels
(e) have an explicit REQUIRES for the parameter to be non-null
(f) cast its parameter's type to Target
(g) have an implicit REQUIRES for the parameter to be non-null
(h) refer to this.carbonLevels
(i) refer to its parameter's agreement
(j) refer to this.timespan
Select all possible options that apply.
100%
Now, select each of the following that the hashCode method must, may, or must not use to influence its return
value. (Must means "is required to", may means "can but is not required to", and must not means "is required not to".)
hashCode must 0% use this.agreement
hashCode must 0% use this.carbonLevels
hashCode may 0% use this.timespan
Correct answer
Given this, select each of the following that must be part of the code for the Target.equals method:
(a)
refer to this.agreement
(d)
refer to its parameter's carbonLevels
(f)
cast its parameter's type to Target
(h)
refer to this.carbonLevels
(i)
refer to its parameter's agreement
Now, select each of the following that the hashCode method must, may, or must not use to influence its return
value. (Must means "is required to", may means "can but is not required to", and must not means "is required not to".)
hashCode may use this.agreement
hashCode may use this.carbonLevels
hashCode must NOT use this.timespan
Submitted answer
4 hide
Submitted at 2022-11-07 18:43:35 (PST) 50%
Given this, select each of the following that must be part of the code for the Target.equals method:
(a)
refer to this.agreement
(d)
refer to its parameter's carbonLevels
(f)
cast its parameter's type to Target
(h)
refer to this.carbonLevels
(i)
refer to its parameter's agreement
100%
Now, select each of the following that the hashCode method must, may, or must not use to influence its return
value. (Must means "is required to", may means "can but is not required to", and must not means "is required not to".)
hashCode must 0% use this.agreement
hashCode must 0% use this.carbonLevels
hashCode may 0% use this.timespan
Submitted answer
3 show
Submitted at 2022-11-07 18:37:53 (PST) saved, not graded
Submitted answer
2 show
Submitted at 2022-11-07 18:12:46 (PST) saved, not graded
Exam 2
Assessment overview
Question
Submission status: 50%
Total points: 12.5 /25
Auto-graded question
Report an error in this question
Attached files
No attached files
Attachments can't be added or deleted because the assessment is closed.