Question

For this Java program I have to be implementing a Pedometer class. The driver file will...

For this Java program I have to be implementing a Pedometer class. The driver file will be provided for you
( PedometerDriver.java ) and can be downloaded in Canvas.
Write a stand alone class.
Name your class Pedometer and source file Pedometer.java .
Program 6
Overview
Goals
Class and File Naming
Here is the Unified Modeling Language (UML) diagram for the Pedometer class. See the end of this
document for information on how to read a UML document.
Pedometer
- steps : int
- strideLength : double
- feetpermile : int
- feetperkm : double
«constructor» Pedometer()
«constructor» Pedometer(s : int)
«constructor» Pedometer(s : int, sl : double)
+ caloriesBurned(weight: double) : double
+ miles() : double
+ kilometers() : double
+ getSteps() : int
+setSteps(theSteps: int) : void
+ reset() : void
+ getStrideLength() : double
+ setStrideLength(slength : double) : void
+ step() : void
+ addSteps(moreSteps : int) : void
Here are brief descriptions of the fields:
Field Description Notes
steps Holds the number of steps should be >= 0
strideLength Length of a stride in feet must be positive
feetpermile Number of feet per mile, which is 5280 should be declared final
feetperkm Number of feet per kilometer, should be set to 3280.84 should be declared final
What to Implement
Here are brief descriptions of all of the methods you should implement. Make sure to name them and
implement them exactly as you see them here as this is how Web-CAT will test them:
Method Description
Pedometer() Default constructor, set steps and strideLength both to 0
Pedometer(s)
Specific constructor, sets steps to parameter s if greater than or equal to
0
Pedometer(s,sl)
Specific constructor, sets steps to parameter s if greater than or equal to
0 , no change otherwise; sets stride length to parameter sl if greater
than 0 , no change otherwise
caloriesBurned(weight)
Takes weight as parameter, calculates and returns number of calories
burned
miles() Returns the amount of miles walked
kilometers() Returns the amount of kilometers walked
getSteps() Returns the number of steps
setSteps(theSteps)
Sets the number of steps for the current object if the parameter is greater
than or equal to 0 , no change otherwise
reset() Resets the number of steps to 0
getStrideLength() Returns the value of strideLength
setStrideLength(slength)
Sets the strideLength for the current object if the parameter is greater
than 0 , no change otherwise
step() Increments the number of steps by 1
addSteps(moreSteps) If parameter is greater than 0 , adds it to steps, no change otherwise
To calculate the number of calories burned for the caloriesBurned() method, use the table below.
Please realize that this is not a scientifically accurate representation of calories burned for walking!
Weight Calories burned per step
≤ 0 lbs 0
> 0 and ≤ 150 lbs 0.05
> 150 and < 200 lbs 0.10
≥ 200 and < 250 lbs 0.17
≥ 250 and < 300 lbs 0.23
≥ 300 and < 350 lbs 0.31
≥ 350 lbs 0.51
The test file has been given to you and you should not change anything in this file. Running this driver file
with your implementation of Pedometer should generate the output found below.
Note: WebCAT will test more than just the following, though.
I have burned 500.0 calories.
You have burned 935.0000000000001 calories.
They have burned 2300.46 calories.
I have walked 6.627532664268131 miles.
You have walked 8.60135361731066 kilometers.
0 steps and stride of 0.0 feet.
0 steps and stride of 0.0 feet.
0 steps and stride of 0.0 feet.
0 steps and stride of 0.0 feet.

The driver file to output it is the following code:

import java.util.Scanner;

public class DrivePedometer
{
public static void main(String [] args)
{
Pedometer mine = new Pedometer();
Pedometer yours = new Pedometer(500);
Pedometer theirs = new Pedometer(10002,5.12);
  
mine.setStrideLength(3.5);
yours.setStrideLength(2.5);
  
for (int i = 0; i < 10000; i++)
mine.step();
  
yours.addSteps(5000);
  
System.out.println("I have burned " + mine.caloriesBurned(150) + " calories.");
System.out.println("You have burned " + yours.caloriesBurned(200) + " calories.");
System.out.println("They have burned " + theirs.caloriesBurned(250) + " calories.");
  
yours.reset();
yours.setSteps(11290);
  
System.out.println("I have walked " + mine.miles() + " miles.");
System.out.println("You have walked " + yours.kilometers() + " kilometers.");
  
Pedometer other = new Pedometer(-5);
System.out.println(other.getSteps() + " steps and stride of " + other.getStrideLength() + " feet.");
other.setStrideLength(0);
System.out.println(other.getSteps() + " steps and stride of " + other.getStrideLength() + " feet.");
other.setSteps(-500);
System.out.println(other.getSteps() + " steps and stride of " + other.getStrideLength() + " feet.");
other.setSteps(0);
System.out.println(other.getSteps() + " steps and stride of " + other.getStrideLength() + " feet.");


}
}

What code do I need for the Pedometer class file to make the driver output like the sample?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Pedometer {

    private int steps;
    private double strideLength;
    private final int feetpermile = 5280;
    private final double feetperkm = 3280.84;

    public Pedometer() {
        this.steps = 0;
        this.strideLength = 0;

    }

    public Pedometer(int s) {
        this.steps = s >= 0 ? s : 0;
        this.strideLength = 0;

    }

    public Pedometer(int s, double sl) {
        this.steps = s >= 0 ? s : 0;
        this.strideLength = sl;
    }

    public double caloriesBurned(double weight) {
        if (weight <= 0) {
            return 0;
        } else if (weight <= 150) {
            return 0.05 * steps ;
        } else if (weight < 200) {
            return 0.10 * steps ;
        } else if (weight < 250) {
            return 0.17 * steps ;
        } else if (weight < 300) {
            return 0.23 * steps ;
        } else if (weight < 350) {
            return 0.31 * steps ;
        } else {
            return 0.37 * steps * weight;
        }
    }

    public double miles() {
        return strideLength*steps/feetpermile;
    }

    public double kilometers() {
        return strideLength*steps/feetperkm;
    }

    public int getSteps() {
        return steps;
    }

    public void setSteps(int steps) {
        this.steps = steps>0?steps:this.steps;
    }

    public double getStrideLength() {
        return strideLength;
    }

    public void setStrideLength(double strideLength) {
        this.strideLength = strideLength;
    }

    public void step() {
        this.steps++;
    }

    public void addSteps(int moreSteps) {
        this.steps += moreSteps > 0 ? moreSteps : 0;
    }

    public void reset() {
        steps = 0;
    }


}

====================================================================================

Add a comment
Know the answer?
Add Answer to:
For this Java program I have to be implementing a Pedometer class. The driver file will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • Get the file “HW4Part4a.java” from the repository. Modify the program class to match the new file...

    Get the file “HW4Part4a.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named “pow2” with one parameter, an integer n. The function is defined as follows: if n ≤ 0 then pow2(n) = 1. Otherwise, pow2(n) = 2 · pow2(n − 1). public class HW4Part4a { public static void main(String[] args) { for (int i = 0; i <= 20; i++) { System.out.println("pow2("+i+") = "+pow2(i)); }...

  • How to build Java test class? I am supposed to create both a recipe class, and...

    How to build Java test class? I am supposed to create both a recipe class, and then a class tester to test the recipe class. Below is what I have for the recipe class, but I have no idea what/or how I am supposed to go about creating the test class. Am I supposed to somehow call the recipe class within the test class? if so, how? Thanks in advance! This is my recipe class: package steppingstone5_recipe; /** * *...

  • Distance Walked Write a program to calculate how many mile, feet, and inches a person walks...

    Distance Walked Write a program to calculate how many mile, feet, and inches a person walks a day, given the stride length in inches (the stride length is measured from heel to heel and determines how far walk with each step], the number of steps per minute, and the minutes that person walks a day. Note that 1 mile is 5280 feet and 1 feet is 12 inches. In your program, there should be the follo constants: final Int FEET_PER_MILE...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template...

    Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template (20 pts) – TempDriver.cpp Template Exercise Write template for two functions called minimum and maximum. Each function should accept two arguments and return the lesser or greater of the two values. Test these templates in a driver program. The template with the following types: int, double, string and FeetInches (which is an object). You will need: The FeetInches class (which was provided in Week...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • So far I have this code but I'm not sure if it's right or how to...

    So far I have this code but I'm not sure if it's right or how to finish it. #include "distance.h" #include <iostream> #include <iomanip> #include <string> using namespace std; Distance::Distance() { feet = 0; miles = 0; yards = 0; inches = 0; } Distance::Distance(int inch) {    } Distance::Distance(int m, int y, int f, double i) { if(m < 0 || y < 0 || f < 0) { feet = 0; miles = 0; yards = 0; }...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT