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. 1 To test for equality of each instance variable, you might want to also use the equals methods, which means that similarly to what has been suggested in this problem sheet, you would also override the default behaviour of the equals methods for each of the classes Distance, Steps, and HeartRate in your Problem sheet 1 package (uk.ac.sheffield.com1003.problemsheet1). 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. 1 To test for equality of each instance variable, you might want to also use the equals methods, which means that similarly to what has been suggested in this problem sheet, you would also override the default behaviour of the equals methods for each of the classes Distance, Steps, and HeartRate in your Problem sheet 1 package (uk.ac.sheffield.com1003.problemsheet1). 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 StepsFitnessTracker extends FitnessTracker {
I/ Stores total number of steps
private Steps totalSteps;
public StepsFitnessTracker(String modelName, Steps
steps) {
super(modelName);
this.totalSteps = steps;
}
I/ Add steps to the total
public void addSteps(Steps stepsTOAdd) {
int numSteps = this.totalSteps.getValue() +
stepsToAdd.getValue();
this.totalSteps.setValue(numSteps);
I/ Getter for total number of steps
public Steps getTotalSteps() {
return totalSteps;
public String toString() {
return "Steps Tracker " + getModelName() +
"; Total Steps: "+ getTotalSteps().getValue();
@Override
public boolean equals(Object obj) {
II 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 StepsFitnessTracker by
comparing the classes of the passed argument and this object.
return false;
StepsFitnessTracker SFFT=(StepsFitnessTracker)obj; /
type casting of the argument.
if(SFFT.getTotalSteps() == this.totalSteps &&
this.totalSteps == null) return true; // check whether they both
are null
// check if one is null and other is not.
if(SFFT.getTotalSteps() == null && this.totalSteps !=
null) return false;
if(SFFT.getTotalSteps() != null && this.totalSteps ==
null) return false;
// at the end check whether value of both are same.
return (SFFT.getTotalSteps().getValue() ==
this.totalSteps.getValue();
Transcribed Image Text:public class StepsFitnessTracker extends FitnessTracker { I/ Stores total number of steps private Steps totalSteps; public StepsFitnessTracker(String modelName, Steps steps) { super(modelName); this.totalSteps = steps; } I/ Add steps to the total public void addSteps(Steps stepsTOAdd) { int numSteps = this.totalSteps.getValue() + stepsToAdd.getValue(); this.totalSteps.setValue(numSteps); I/ Getter for total number of steps public Steps getTotalSteps() { return totalSteps; public String toString() { return "Steps Tracker " + getModelName() + "; Total Steps: "+ getTotalSteps().getValue(); @Override public boolean equals(Object obj) { II 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 StepsFitnessTracker by comparing the classes of the passed argument and this object. return false; StepsFitnessTracker SFFT=(StepsFitnessTracker)obj; / type casting of the argument. if(SFFT.getTotalSteps() == this.totalSteps && this.totalSteps == null) return true; // check whether they both are null // check if one is null and other is not. if(SFFT.getTotalSteps() == null && this.totalSteps != null) return false; if(SFFT.getTotalSteps() != null && this.totalSteps == null) return false; // at the end check whether value of both are same. return (SFFT.getTotalSteps().getValue() == this.totalSteps.getValue();
public class TestFitnessTracker {
eTest
public void testSameTrackersEqual() {
FitnessTracker a - new FitnessTracker("a");
FitnessTracker b- new FitnessTracker("a");
assertEquals(a, b);
еТest
public void testDifferentNamesNotEqual() {
FitnessTracker a = new FitnessTracker("a");
FitnessTracker b- new FitnessTracker("b");l
assertNotEquals(a, b);
еTest
public void testNullNotEqual() {
FitnessTracker a- null;
FitnessTrackerb- new FitnessTracker("a");
assertNotEquals(a, b);
Transcribed Image Text:public class TestFitnessTracker { eTest public void testSameTrackersEqual() { FitnessTracker a - new FitnessTracker("a"); FitnessTracker b- new FitnessTracker("a"); assertEquals(a, b); еТest public void testDifferentNamesNotEqual() { FitnessTracker a = new FitnessTracker("a"); FitnessTracker b- new FitnessTracker("b");l assertNotEquals(a, b); еTest public void testNullNotEqual() { FitnessTracker a- null; FitnessTrackerb- new FitnessTracker("a"); assertNotEquals(a, b);
Expert Solution
trending now

Trending now

This is a popular 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