Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java important uregent help Note that the class FitnessTracker is at the top of the inheritance hierarchy and that each of the classes inheriting from

Java important uregent help

Note that the class FitnessTracker is at the top of the inheritance hierarchy and that each of the classes inheriting from it, present different features.

You need to complete these four classes

To simplify the problem, our classes are only storing one type of measurement, either the total number of steps class StepsFitnessTracker, or the total distance walked class DistanceFitnessTracker, or the average heart rate class HeartRateFitnessTracker. As opposed to what happens with commercial fitness trackers, none of our trackers is able to track simultaneously distance, heart rate and steps. Each of these classes also have a method with a signature similar to: public void addXYZ(XYZ newMeasurement) For example: - StepsFitnessTracker has addSteps(Steps steps) - HeartRateFitnessTracker has addHeartRage(HeartRate newHeartRate) These methods are meant to add more measurements to these objects, and they are meant to either keep the total distance, number of steps walked, or average heart rate. See the code provided and the comments in the code to understand how they work.

Task 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: 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)) }; In the FitnessExperiment class, complete the implementation of the methods named getTotalSteps()and printExperimentDetails(). The comments in the code specify how they should operate and provide you with additional hints to get you started. You can 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). FitnessTracker HeartRateFitnessTracker DistanceFitnessTracker StepsFitnessTracker

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

}

We need to write the code so it can pass all the tests the distancefitnesstracker.java is not provided it will be programmed later

We are not allowed to change the code we are allowed to remove any part saying "// TODO Implement a method to check equality" and replace it with a code to sastsfiy the code and its needs

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Steps.java

public class Steps { // Instance variables private int value; // Constructor public Steps(int value) { this.value = value; } // Methods; you should remove the comments and complete the method bodies public int getValue() { return value; } public void setValue(int newValue) { value=newValue; }

public String toString() { String outString = "Steps ="+value; return outString; } }

HeartRate.java

public class HeartRate { // Instance variables private double value; // Constructor public HeartRate(double value) { this.value = value; } // Methods; you should remove the block comments and complete the method bodies public double getValue() { return value; } public void setValue(double newValue) { value=newValue; }

public String toString() { String outString = "Heart Rate="+value; return outString; } }

This Is heartrate.java

12:28 Notes public class TestSteps Fitness Tracker { @Test public void testTrackersSameSumEqual() { StepsFitness Tracker a = new Steps FitnessTracker("hr", new Steps(20)); StepsFitness Tracker b = new StepsFitness Tracker("hr", new Steps(10)); b.addSteps(new Steps(10)); assertEquals(a, b); } @Test public void testBothNullEqual() { Steps Fitness Tracker a = new Steps FitnessTracker("a", null); Steps Fitness Tracker b = new StepsFitness Tracker("a", null); assertEquals(a, b); } @Test public void testGetTotalSteps() { Steps Fitness Tracker a = new StepsFitness Tracker("a", new Steps(10)); StepsFitness Tracker b = new StepsFitness Tracker("b", new Steps(10)); StepsFitness Tracker c = new Steps Fitness Tracker("C", new Steps(20)); 12:28 Notes public class TestSteps Fitness Tracker { @Test public void testTrackersSameSumEqual() { StepsFitness Tracker a = new Steps FitnessTracker("hr", new Steps(20)); StepsFitness Tracker b = new StepsFitness Tracker("hr", new Steps(10)); b.addSteps(new Steps(10)); assertEquals(a, b); } @Test public void testBothNullEqual() { Steps Fitness Tracker a = new Steps FitnessTracker("a", null); Steps Fitness Tracker b = new StepsFitness Tracker("a", null); assertEquals(a, b); } @Test public void testGetTotalSteps() { Steps Fitness Tracker a = new StepsFitness Tracker("a", new Steps(10)); StepsFitness Tracker b = new StepsFitness Tracker("b", new Steps(10)); StepsFitness Tracker c = new Steps Fitness Tracker("C", new Steps(20)); 12:29 Notes assertTrue (a.getTotalSteps().getValue() == b.getTotalSteps().getValue(); assertFalsela.getTotalSteps().getValue() = c.getTotalSteps().getValue()); } public class TestHeartRateFitness Tracker { @Test public void testSameReferenceEqual() { HeartRate Fitness Tracker a = new HeartRate Fitness Tracker("a", new HeartRate(10)); HeartRateFitness Tracker b = a; assertEquals(a, b); } @Test public void testDifferentClassesNotEqual() { HeartRate Fitness Tracker a = new HeartRate Fitness Tracker("a", new HeartRate(10)); StepsFitness Tracker b = new Steps Fitness Tracker("a", new Steps(10)); assertNotEquals(a, b); } 0 12:29 Notes @ } public class TestFitness Tracker { @Test public void testSameTrackersEqual() { Fitness Tracker a = new Fitness Tracker("a"); Fitness Tracker b = new Fitness Tracker("a"); assertEquals(a,b); } @Test public void testDifferentNamesNotEqual() { Fitness Tracker a = new Fitness Tracker("a"); Fitness Tracker b = new Fitness Tracker("b"); assertNotEquals(a, b); } @Test public void testNullNotEqual() { Fitness Tracker a = null; Fitness Tracker b = new Fitness Tracker("a"); assertNotEquals(a, b); } a 12:29 Notes @lest public void testDifferentNamesNotEqual() { Fitness Tracker a = new Fitness Tracker("a"); Fitness Tracker b = new Fitness Tracker("b"); assertNotEquals(a, b); } @Test public void testNullNotEqual() { Fitness Tracker a = null; Fitness Tracker b = new Fitness Tracker("a"); assertNotEquals(a, b); }

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

Students also viewed these Databases questions

Question

=+ Is it still expanding into new countries?

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago