Question: Help is needed quickly Modify the class FitnessExperiment class so that it uses polymorphism to: (i) display the properties of each object in a FitnessExperiment,
Help is needed quickly
Modify the class FitnessExperiment class so that it uses polymorphism to: (i) display the properties of each object in a FitnessExperiment, and (ii) identify any StepsFitnessTracker, HeartRateFitnessTracker and DistanceFitnessTracker objects that are equal. Here we are trying to identify those cases where a user mistake might have been made during the initialisation of the FitnessExperiment object (e.g. the data for the same tracker being introduced twice). Those situations should be flagged to the person leading the experiment to verify if the error was genuine or if the object was really the same object. The place were this code should be added is in bold
FitnessExperiement
public class FitnessExperiment { FitnessTracker[] fitnessTrackers; public static void main(String[] args) { FitnessTracker[] trackers = { new StepsFitnessTracker("steps", new Steps(230)), new StepsFitnessTracker("steps2", new Steps(150)), new StepsFitnessTracker("steps2", new Steps(150)), new HeartRateFitnessTracker("hr", new HeartRate(80)), new HeartRateFitnessTracker("hr", new HeartRate(80)), new DistanceFitnessTracker("dist1", new Distance(1000)), new DistanceFitnessTracker("dist2", new Distance(2430)), new DistanceFitnessTracker("dist2", new Distance(2430)) }; FitnessExperiment fe = new FitnessExperiment(trackers); fe.printExperimentDetails(); }
// Constructor public FitnessExperiment(FitnessTracker[] fitnessTrackers) { this.fitnessTrackers = fitnessTrackers; } // Methods to implement:
// Implementation hint: Should it use the corresponding toString() methods for // each of the objects stored in the fitnessTrackers array? public String toString() { // TODO Implement String s = ""; for (FitnessTracker ft : fitnessTrackers) { s = s + ft.toString() + " "; } return s; } // Method displays in console the details of this experiment which include: // - Summary of the measurements of each individual fitness tracker // (indicating if they are steps, distance or heart rate measurements) // - The total number of fitness trackers that participated in this experiment // - A summary of total number of steps and total distance walked in this experiment // Implementation hint: Should it use the toString() method right above this method? public void printExperimentDetails() { System.out.print(this.toString()); System.out.println(fitnessTrackers.length + " number of fitness trackers are participated in this experiment"); System.out.println("Total number of steps : " + getTotalSteps()); System.out.println("Total number of steps : " + getTotalDistance()); }
// Method should iterate through all fitness tracker step measurements and returns a double // with the total step count (see Task 2) // Implementation hint: The instanceof operator and casting may become handy here public int getTotalSteps() { // TODO Implement int totalSteps = 0; for (FitnessTracker ft : fitnessTrackers) { if (ft instanceof StepsFitnessTracker) totalSteps = totalSteps + ((StepsFitnessTracker) ft).getTotalSteps().getValue();
} return totalSteps; }
// Method should iterate through all fitness tracker distance measurements and returns a double with the total distance // Implementation hint: The instanceof operator and casting may become handy here public double getTotalDistance() { return 0; } public FitnessTracker[] getTrackersEqualTo(FitnessTracker tracker) { int count = 0; for(FitnessTracker fitnessTracker:fitnessTrackers){ if(fitnessTracker.equals(tracker)) count++; } FitnessTracker[] resultfitnessTrackers = new FitnessTracker[count]; int index = 0; for(FitnessTracker fitnessTracker:fitnessTrackers){ if(fitnessTracker.equals(tracker)) resultfitnessTrackers[index++] = fitnessTracker; } return resultfitnessTrackers; } // Implementation hint: use the above getTrackersEqualTo() method public void printAllEqualTrackers() { // TODO Implement a method which prints every duplicate tracker } }


Everything needed is here please solve as soon as possible


public class StepsFitness Tracker extends Fitness Tracker { // Stores total number of steps private Steps totalSteps; public Steps FitnessTracker(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); } 1/ Getter for total number of steps public Steps getTotalSteps() { return totalSteps; } public String to String() { return "Steps Tracker" - getModelName() + "; Total Steps: ".getTotalSteps0.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 Steps Fitness Tracker by comparing the classes of the passed argument and this object. return false; Steps Fitness Tracker SFFT=(Steps Fitness Tracker)obj; // type casting of the argument. if(SFFT.getTotalSteps() == this.totalSteps && this.totalSteps == null) return true; // check whether they both are null 1/ 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; Il at the end check whether value of both are same. return (SFFT.getTotalSteps().getValue() == this.totalSteps.getValue(); } } public class HeartRate Fitness Tracker extends Fitness Tracker 1/ Cumulative moving average HeartRate HeartRate avgHeartRate; // Number of heart rate measurements int numMeasurements; public HeartRate Fitness Tracker(String modelName, HeartRate heartRate) { supermodelName); // Only one HeartRate to begin with; average is equal to single measurement this.avgHeartRate = heartRate; this.numMeasurements = 1; } public void add HeartRate(HeartRate heartRate ToAdd) { 1/ 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* num Measurements))/(num Measurements + 1); this.avgHeartRate.setValue(cmaNext); num Measurements ++; } Il Getter for average heart rate public HeartRate getAvgHeartRate() { return avgHeartRate; } public String toString() { return "Heart Rate Tracker" + getModelName() - "; Average Heart Rate: " + getAvgHeartRate().getValue() + ", for" + num Measurements" 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 HeartRate Fitness Tracker by comparing the classes of the passed argument and this object. return false; HeartRate Fitness Tracker HRFT= (HeartRate Fitness Tracker)obj; // type casting of the argument. return (HRFT.avgHeartRate == this.avgHeartRate);/1 comparing the state of argument with the state of this' Object. } public class DistanceFitness Tracker extends Fitness Tracker private Distance distance; public DistanceFitness Tracker(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; DistanceFitness Tracker that = (DistanceFitness Tracker) 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 HeartRate Feresstracker extends Ptnesstracker Cumulative moving average Heartace HeartRate avgHeartRate Number of heart rate measurements Intrum Measurements Public HeartacenesTacker Sering modelame, HeartRate heartRate) supermodelame Only one HeartRate to begin werage is equal to single measurement this avgheter this num Measurements: public void add HeartRate HeartRate heartRate Todd) { / Calculate cumulative moving average of heart rate See holen wikipedia.org/Moving average double RhearTodo gervalued double this.gear Rate geriau double maNext-CR- num Measurements ou leasurements this.argherace.servecomalet num Measurements > W Getter for average heart public HeartRane gezaugarne return avgHeartRate public String toering return Heart Rate Tracker geklodename] Average Heart Rate getAvgHeartRate geval measurements Override pubic boolean equals(Object) f # TODO Implement method to check equality this will check on the references are refers to same object return true obju gelass-hisgerClass/ check the argument of the type Mearateness Tracker by comparing the classes of the passed argument and this object return false HeartRate Tracker HRT (HeartRate FitnessTracero oye casting of the argument. return HRFT zugeerte -- tis.ughtearthate:// comparing the state of argument with the state of this Object 3 public class Steps Fitness Tracker extends Riness Trackert /Stores total number of steps private Steps totales public Steps Fitness Tracker(Sering modelName Steps steps) { Supermodellemes this totales steps 1 1/ Add steps to the total public void addSteps Steps stepstonda int numSteps this.colepsgervalue steps Todd.getve vel: this.total Steps.setValueum Steps 1/ Getter for total number of steps public Steps gettona Steps return totales public String to Sering return "Steps TractersedilodeNames. Total Steps - gerFocatepsgervalued > @Override public boolean equals(Object obj // TODO Implement method to check equality this == objwill check if both the references are refers to same object return true fob full serClass-this.getClass() check the argument of the type Steps Fitness Tracker by comparing the classes of the passed argument and this object return false Steps Fitness Tracker SFFT Steps Pitness Trackerjobs: type casting of the argument SFFT getTotaStepsistotaSteps 88 this.totalSteps - null return true. Il check whether they both are null 1/ check it one is null and other is not ESFFT gesTotaltepso - this.totalSteps null) return false ASFFT get TotaStep 88 this totaSteps -- null) return false at the end check whether value of both are same. return (SFFT getTotalSteps getValue this.totalSteps gervalech 13 14 public class FitnessTracker { 15 16 // String containing model name of fitness activity tracker 17 private String model Name; 18 1199 public Fitness Tracker (String modelName) { 20 this.modelName = modelName; 21 3 22 236 24 25 26 27 public String getModelName() { return modelName; } 28 @Override public boolean equals(Object obj) { // TODO Implement a method to check equality */ 29 230 31 32 33 34 35 36 1 1 Consoles on hitsat binjavaw
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
