Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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)

 

 

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education