In: Computer Science
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of the method is to see whether the given name exists in names. If yes, return true; if not, return false. (Use the equals method.) Note that names might not be full, so numNames tells us how many Names are stored in the array and therefore how far in the array we should search. (In other words, it could be that names.length > numNames.) Write this method.
class Name {
private String first;
private String last;
public Name(String first, String last) {
this.first = first;
this.last = last;
}
public boolean equals(Name other) {
return this.first.equals(other.first) && this.last.equals(other.last);
}
}
public class TestNames {
public static boolean exists(Name[] names, int numNames, Name name) {
// iterating through given array and checking if has the given name
//using equals method
for (int i = 0; i < numNames; i++) {
if (names[i].equals(name))
return true;
}
return false;
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me