Here is a basic Counter class. class Counter { private int num = 0; // Not needed for this question public void increment() { num++; } public boolean equals(Counter other) { return this.num == other.num; } } 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(Counter[] counters, int numCounters, Counter counter) The goal of the method is to see whether the given counter exists in counters. If yes, return true; if not, return false. (Use the equals method.) Note that counters might not be full, so numCounters tells us how many Counters are stored in the array and therefore how far in the array we should search. (In other words, it could be that counters.length > numCounters.) Write this method.
CODE IN JAVA:
Counter.java file:
class Counter {
private int num = 0; // Not needed for this
question
public void increment() {
num++;
}
public boolean equals(Counter other) {
return this.num == other.num;
}
}
TestCounter.java file:
public class TestCounter {
public static boolean exists(Counter[] counters, int
numCounters, Counter counter) {
for(int i = 0 ; i < numCounters;
i++) {
if(counters[i].equals(counter))
return true ;
}
return false ;
}
}
Get Answers For Free
Most questions answered within 1 hours.