Question
Java Code Help Urgent Question Please Help The main goal was to write a code that finds equality The original code public class StepsFitnessTracker extends
Java Code Help
Urgent Question Please Help
The main goal was to write a code that finds equality
The original code
public class StepsFitnessTracker extends FitnessTracker {
// Stores total number of steps private Steps totalSteps; public StepsFitnessTracker(String modelName, Steps steps) { super(modelName); this.totalSteps = steps; } // Add steps to the total public void addSteps(Steps stepsToAdd) { int numSteps = this.totalSteps.getValue() + stepsToAdd.getValue(); this.totalSteps.setValue(numSteps); } // 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 obj2) { // TODO Implement a method to check equality } }
This is my code
public class StepsFitnessTracker extends FitnessTracker {
// Stores total number of steps private Steps totalSteps; public StepsFitnessTracker(String modelName, Steps steps) { super(modelName); this.totalSteps = steps; } // Add steps to the total public void addSteps(Steps stepsToAdd) { int numSteps = this.totalSteps.getValue() + stepsToAdd.getValue(); this.totalSteps.setValue(numSteps); } // 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) { // 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. return (SFFT.totalSteps == this.totalSteps);// comparing the state of argument with the state of 'this' Object. }
}
When I run it through the following Junit eclipse test
Code for the test which I run my code through
public class TestStepsFitnessTracker {
@Test public void testTrackersSameSumEqual() { StepsFitnessTracker a = new StepsFitnessTracker("hr", new Steps(20)); StepsFitnessTracker b = new StepsFitnessTracker("hr", new Steps(10)); b.addSteps(new Steps(10)); assertEquals(a, b); }
@Test public void testBothNullEqual() { StepsFitnessTracker a = new StepsFitnessTracker("a", null); StepsFitnessTracker b = new StepsFitnessTracker("a", null); assertEquals(a, b); } @Test public void testGetTotalSteps() { StepsFitnessTracker a = new StepsFitnessTracker("a", new Steps(10)); StepsFitnessTracker b = new StepsFitnessTracker("b", new Steps(10)); StepsFitnessTracker c = new StepsFitnessTracker("c", new Steps(20));
assertTrue(a.getTotalSteps().getValue() == b.getTotalSteps().getValue()); assertFalse(a.getTotalSteps().getValue() == c.getTotalSteps().getValue()); } }
What we need to do is to add a code Implement a method to check equality
It fails to pass the TestTrackerSameSumEqual.
I get 2 passes and one failure in the following point in the picture
Runs: 3/3 Errors: 0 E Failures: 1 ukac.sheffield.com 1003.problemsheet2.test. TestSteps Fitness Tracl testGetTotalSteps (0.016 s) testTrackersSameSumEqual (0.020 s) testBothNullEqual (0.003 s)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started