Java Problem Create an object of type FitnessExperiment that stores an array of StepsFitnessTracker, DistanceFitnessTracker, and HeartRateFitnessTracker objects. In the FitnessExperiment class Add to...


Java Problem


Create an object of type FitnessExperiment that stores an array of
StepsFitnessTracker, DistanceFitnessTracker, and
HeartRateFitnessTracker objects. In the FitnessExperiment class


Add to your FitnessExperiment
class a method called getTotalDistance() that calculates the total distance walked by all the
objects stored in your FitnessExperiment.
appropriately both values (total number of steps and total distance walked in that experiment) using
the printExperimentDetails() method.


FitnessExperiemnt.java


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() + "\n";
}
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() {
// TODO Implement
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() { double totalDistance=0;
for(FitnessTracker ft :fitnessTrackers){ if(ft instanceof
DistanceFitnessTracker) totalDistance = totalDistance +
((DistanceFitnessTracker)ft).getDistance(); } return totalDistance;
}


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;


}
}


Distancefitnesstrakcer.java


public class DistanceFitnessTracker extends FitnessTracker {


Distance distance;


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





I keep getting this error what to do?


Steps Tracker steps; Total Steps: 230
Steps Tracker steps2; Total Steps: 150
Steps Tracker steps2; Total Steps: 150
Heart Rate Tracker hr; Average Heart Rate: 80.0, for 1 measurements
Heart Rate Tracker hr; Average Heart Rate: 80.0, for 1 measurements
uk.ac.sheffield.com1003.problemsheet2.DistanceFitnessTracker@470e2030
uk.ac.sheffield.com1003.problemsheet2.DistanceFitnessTracker@3fb4f649
uk.ac.sheffield.com1003.problemsheet2.DistanceFitnessTracker@33833882
Exception in thread "main" 8 number of fitness trackers are participated in this experiment
Total number of steps : 530
java.lang.Error: Unresolved compilation problem:
The method getDistance() is undefined for the type DistanceFitnessTracker


at uk.ac.sheffield.com1003.problemsheet2.FitnessExperiment.getTotalDistance(FitnessExperiment.java:91)
at uk.ac.sheffield.com1003.problemsheet2.FitnessExperiment.printExperimentDetails(FitnessExperiment.java:69)
at uk.ac.sheffield.com1003.problemsheet2.FitnessExperiment.main(FitnessExperiment.java:34)



Jun 02, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions ยป

Submit New Assignment

Copy and Paste Your Assignment Here