Write an equals method for the StepsFitnessTracker class that tests whether the one object is equal to another object that is passed as an argument (Object otherObject). The Object superclass provides an equals method that tests if two object references are equal. Your new equals method should override the Object equals method, and should test the following: • Return true if the current (this) object, and otherObject are identical references (hint: you can use the == operator for this). • Returns false if otherObject is null. • Returns false if the current (this) object and otherObject have different classes (hint: you can use the getClass() method provided by the Object superclass, see the Java 8 API documentation for details). • Casts otherObject to a StepsFitnessTracker, and tests for equality of each instance variable1 , if the instance variables are the same the method should return true.  Now write an equals method for the DistanceFitnessTracker and HeartRateFitnessTracker classes that: • Use the superclass equals method, and return false if this method fails. • Casts the parameter to either HeartRateFitnessTracker or DistanceFitnessTracker. • Tests for equality of the subclass instance fields. Please, use JUnit testing and write a test class named TestFTEquals to test that these new equals methods work properly. For inspiration, you can have a look at the test classes provided (TestFitnessTracker, TestDistanceFitnessTracker, TestStepsFitnessTracker, and TestHeartRateFitnessTracker) as they are already providing you some basic tests about how to test for equality of objects. I already did the first part and added the equal method I need help to write the TESTFTEQUALS TEST PLease public class HeartRateFitnessTracker extends FitnessTracker { // Cumulative moving average HeartRate HeartRate avgHeartRate; // Number of heart rate measurements int numMeasurements; public HeartRateFitnessTracker(String modelName, HeartRate heartRate) { super(modelName); // Only one HeartRate to begin with; average is equal to single measurement this.avgHeartRate = heartRate; this.numMeasurements = 1; } public void addHeartRate(HeartRate heartRateToAdd) { // Calculate cumulative moving average of heart rate // See https://en.wikipedia.org/wiki/Moving_average double newHR = heartRateToAdd.getValue(); double cmaHR = this.avgHeartRate.getValue(); double cmaNext = (newHR + (cmaHR * numMeasurements)) / (numMeasurements + 1); this.avgHeartRate.setValue(cmaNext); numMeasurements ++; } // Getter for average heart rate public HeartRate getAvgHeartRate() { return avgHeartRate; } public String toString() { return "Heart Rate Tracker " + getModelName() + "; Average Heart Rate: " + getAvgHeartRate().getValue() + ", for " + numMeasurements + " measurements"; } @Override public boolean equals(Object obj) { // TODO Implement a method to check equality if(this == obj) // it will check if both the references are refers to same object return true; if(obj == null || obj.getClass()!= this.getClass()) //it check the argument of the type HeartRateFitnessTracker by comparing the classes of the passed argument and this object. return false; HeartRateFitnessTracker HRFT=(HeartRateFitnessTracker)obj; // type casting of the argument. return (HRFT.avgHeartRate == this.avgHeartRate);// comparing the state of argument with the state of 'this' Object. } }   public class DistanceFitnessTracker extends FitnessTracker{ private Distance totalDistance; public DistanceFitnessTracker(String modelName, Distance distance) { super(modelName); this.totalDistance = distance ; } public Distance getDistance() { return totalDistance; } public void addDistance(Distance distance) { this.totalDistance.setValue(this.totalDistance.getValue() + distance.getValue()); } public Distance getTotalDistance() { return totalDistance; } public String toString() { return "Distance Tracker " + getModelName() + "; Total Distance: " + getTotalDistance().getValue(); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; if (!super.equals(obj)) return false; DistanceFitnessTracker that = (DistanceFitnessTracker) obj; if(this.getDistance() == null && that.getDistance() != null) return false; if(this.getDistance() != null && that.getDistance() == null) return false; if(that.getDistance() != this.getDistance()) return false; return that.getDistance().getValue() == this.getDistance().getValue(); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Urgent Quick Question

Write an equals method for the StepsFitnessTracker class that tests whether the one object is equal to another object that is passed as an argument (Object otherObject). The Object superclass provides an equals method that tests if two object references are equal. Your new equals method should override the Object equals method, and should test the following: • Return true if the current (this) object, and otherObject are identical references (hint: you can use the == operator for this). • Returns false if otherObject is null. • Returns false if the current (this) object and otherObject have different classes (hint: you can use the getClass() method provided by the Object superclass, see the Java 8 API documentation for details). • Casts otherObject to a StepsFitnessTracker, and tests for equality of each instance variable1 , if the instance variables are the same the method should return true.  Now write an equals method for the DistanceFitnessTracker and HeartRateFitnessTracker classes that: • Use the superclass equals method, and return false if this method fails. • Casts the parameter to either HeartRateFitnessTracker or DistanceFitnessTracker. • Tests for equality of the subclass instance fields. Please, use JUnit testing and write a test class named TestFTEquals to test that these new equals methods work properly. For inspiration, you can have a look at the test classes provided (TestFitnessTracker, TestDistanceFitnessTracker, TestStepsFitnessTracker, and TestHeartRateFitnessTracker) as they are already providing you some basic tests about how to test for equality of objects.

I already did the first part and added the equal method I need help to write the TESTFTEQUALS TEST PLease

public class HeartRateFitnessTracker extends FitnessTracker {


// Cumulative moving average HeartRate
HeartRate avgHeartRate;
// Number of heart rate measurements
int numMeasurements;

public HeartRateFitnessTracker(String modelName, HeartRate heartRate) {
super(modelName);
// Only one HeartRate to begin with; average is equal to single measurement
this.avgHeartRate = heartRate;
this.numMeasurements = 1;
}

public void addHeartRate(HeartRate heartRateToAdd) {
// Calculate cumulative moving average of heart rate
// See https://en.wikipedia.org/wiki/Moving_average
double newHR = heartRateToAdd.getValue();
double cmaHR = this.avgHeartRate.getValue();
double cmaNext = (newHR + (cmaHR * numMeasurements)) / (numMeasurements + 1);

this.avgHeartRate.setValue(cmaNext);

numMeasurements ++;
}

// Getter for average heart rate
public HeartRate getAvgHeartRate() {
return avgHeartRate;
}

public String toString() {
return "Heart Rate Tracker " + getModelName() +
"; Average Heart Rate: " + getAvgHeartRate().getValue() +
", for " + numMeasurements + " measurements";
}


@Override
public boolean equals(Object obj) {
// TODO Implement a method to check equality
if(this == obj) // it will check if both the references are refers to same object
return true;
if(obj == null || obj.getClass()!= this.getClass()) //it check the argument of the type HeartRateFitnessTracker by comparing the classes of the passed argument and this object.
return false;
HeartRateFitnessTracker HRFT=(HeartRateFitnessTracker)obj; // type casting of the argument.

return (HRFT.avgHeartRate == this.avgHeartRate);// comparing the state of argument with the state of 'this' Object.

}


}

 

public class DistanceFitnessTracker extends FitnessTracker{
private Distance totalDistance;

public DistanceFitnessTracker(String modelName, Distance distance) {
super(modelName);
this.totalDistance = distance ;
}

public Distance getDistance() {
return totalDistance;
}

public void addDistance(Distance distance) {
this.totalDistance.setValue(this.totalDistance.getValue() + distance.getValue());
}


public Distance getTotalDistance() {
return totalDistance;
}

public String toString() {
return "Distance Tracker " + getModelName() +
"; Total Distance: " + getTotalDistance().getValue();
}


@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (!super.equals(obj)) return false;
DistanceFitnessTracker that = (DistanceFitnessTracker) obj;

if(this.getDistance() == null && that.getDistance() != null) return false;
if(this.getDistance() != null && that.getDistance() == null) return false;

if(that.getDistance() != this.getDistance()) return false;

return that.getDistance().getValue() == this.getDistance().getValue();

}

}

 

public class DistanceFitnessTracker extends FitnessTracker{
private Distance distance;
public DistanceFitnessTracker(String modelName, Distance distance) {
super (modelName);
this.distance = distance ;
}
public Distance getDistance() {
return distance;
}
public void addDistance(Distance distance) {
this.distance.setValue(this.distance.getValue() + distance.getValue());
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getclass() != obj.getClass()) return false;
if (!super.equals(obj)) return false;
DistanceFitnessTracker that = (DistanceFitnessTracker) obj;
if(this.getDistance() == null && that.getDistance() != nul1) return false;
if(this.getDistance() != null && that.getDistance() == null) return false;
if(that.getDistance() != this.getDistance()) return false;
return that.getDistance().getValue() == this.getDistance().getValue();
}
Transcribed Image Text:public class DistanceFitnessTracker extends FitnessTracker{ private Distance distance; public DistanceFitnessTracker(String modelName, Distance distance) { super (modelName); this.distance = distance ; } public Distance getDistance() { return distance; } public void addDistance(Distance distance) { this.distance.setValue(this.distance.getValue() + distance.getValue()); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getclass() != obj.getClass()) return false; if (!super.equals(obj)) return false; DistanceFitnessTracker that = (DistanceFitnessTracker) obj; if(this.getDistance() == null && that.getDistance() != nul1) return false; if(this.getDistance() != null && that.getDistance() == null) return false; if(that.getDistance() != this.getDistance()) return false; return that.getDistance().getValue() == this.getDistance().getValue(); }
public class TestDistanceFitnessTracker {
@Test
public void testTrackersDifferentSumNotEqual() {
DistanceFitnessTracker a = new DistanceFitnessTracker("hr", new Distance(20));
DistanceFitnessTracker b = new DistanceFitnessTracker("hr", new Distance(20));
b.addDistance(new Distance(10));
assertNotEquals(a, b);
assertNotEquals(b, a);
}
Transcribed Image Text:public class TestDistanceFitnessTracker { @Test public void testTrackersDifferentSumNotEqual() { DistanceFitnessTracker a = new DistanceFitnessTracker("hr", new Distance(20)); DistanceFitnessTracker b = new DistanceFitnessTracker("hr", new Distance(20)); b.addDistance(new Distance(10)); assertNotEquals(a, b); assertNotEquals(b, a); }
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY