. Consider the following class declaration in java. public class StudentInfo { p
ID: 3798047 • Letter: #
Question
. Consider the following class declaration in java.
public class StudentInfo
{
private String major;
public String getMajor()
{ return major; }
// There may be fields, constructors, and methods that are not shown
}
The following instance variable and method appear in the client class.
private StudentInfo[] students;
/** @param major a string to be checked
* @return the number of students that have the given major.
*/
public int enrollmentInMajor(String major)
{
int count = 0;
for (int i=0; i < students.length; i++)
{
StudentInfo k = students[i];
if ( /* expression */ )
count++;
}
return count;
}
Which of the following could be used to replace /* expression */ so that
enrollmentInMajor will compile without an error and the method will return
the number of students that have the given major?
a) major == students[k].getMajor()
b) major == k.major
c) major.equals(k.major)
d) major.equals(getMajor(k))
e) major.equals(k.getMajor())
and can you please expalin why? I'm confused between equals and ==
Explanation / Answer
major == students[k].getMajor(), is correct because in this major and student[i] string will be matched and the the result will return ture if yes otherwise false, So in the end the number of students who is given the major will be stored in count.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.