Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Very Urgent Help Please Create an object of type FitnessExperiment that stores an array of StepsFitnessTracker, DistanceFitnessTracker, and HeartRateFitnessTracker objects. You could use code

Java Very Urgent Help Please

Create an object of type FitnessExperiment that stores an array of StepsFitnessTracker, DistanceFitnessTracker, and HeartRateFitnessTracker objects. You could use code such as the following to initialise the array containing the fitness tracker measurements also we need a new class called DistanceFitnessTracker that extends FitnessTracker. DistanceFitnessTracker will represent a fitness tracker able to record only distance. You will not be asked to convert between distance units the default behaviour of the Distance class will be used, namely, kilometres. Now, your array containing FitnessTracker objects could be initialised as follows

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)) };

In the FitnessExperiment class, complete the implementation of the methods named getTotalSteps()and printExperimentDetails(). The comments in the code specify how they should operate .You should use the method getSteps()in StepsFitnessTracker, but think how you will know the real type of each object in the array of fitness trackers (trackers in the example above). :

Similarly to how the constructors for StepsFitnessTracker and HeartRateFitnessTracker operate, the constructor of DistanceFitnessTracker objects should be initialised as follows: public DistanceFitnessTracker(String modelName, Distance distance)

Implement the DistanceFitnessTracker class, and add to your FitnessExperiment class a method called getTotalDistance() that calculates the total distance walked by all the objects stored in your FitnessExperiment. Make sure that your getTotalSteps()in the class FitnessExperiment still works as expected. And last, make sure that you display appropriately both values (total number of steps and total distance walked in that experiment) using the printExperimentDetails() method.

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 } */

}

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 } */

}

public class FitnessTracker { // String containing model name of fitness activity tracker private String modelName; public FitnessTracker(String modelName) { this.modelName = modelName; } public String getModelName() { return modelName; }

/* @Override public boolean equals(Object obj) { // TODO Implement a method to check equality } */ }

public class FitnessExperiment { FitnessTracker[] fitnessTrackers; public static void main(String[] args) { } // 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 } // 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 } // 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 } // 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() { // TODO Implement } public FitnessTracker[] getTrackersEqualTo(FitnessTracker tracker) { // TODO Implement a method that finds the trackers which are equal to tracker } // Implementation hint: use the above getTrackersEqualTo() method public void printAllEqualTrackers() { // TODO Implement a method which prints every duplicate tracker } */ }

We are not allowed to change anything in the code except we add the code asked in the question^

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions

Question

What is meant by the adjective asbestiform?

Answered: 1 week ago

Question

1. Identify what positions are included in the plan.

Answered: 1 week ago