Advertisements
Advertisements
Question
Define a class named Step Tracker with the following specifications:
Member Variables:
| String name | − stores the user’s name. |
| int sw | − stores the total number of steps walked by the user. |
| double cb | − stores the estimated calories burned by the user. |
| double km | − stores the estimated distance walked in kilometres. |
Member Methods:
| void accept() | − to input the name and the steps walked using Scanner class methods only. |
| void calculate() | − calculates calories burned and distance in km based on steps walked using the following estimation table: |
| Metric | Calculation Formula |
| Calories Burned | steps walked × 0.04 (e.g., 1 step burns 0.04 calories) |
| Distance (Km) | steps walked/1300 (e.g., 1300 steps is approx. 1 km) |
| void display() | − Display the calories burned, distance in km and the user’s name. |
Write a main method to create an object of the class and invoke the methods.
Code Writing
Advertisements
Solution
import java.util.Scanner;
class StepTracker {
// Member Variables
String name;
int sw;
double eb; // Based on image text "eb", though "cb" is also commonly used
double km;
// Member Method to input data
void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter user name: ");
name = sc.nextLine();
System.out.print("Enter steps walked: ");
sw = sc.nextInt();
}
// Member Method to calculate metrics
void calculate() {
// Formula: steps * 0.04
eb = sw * 0.04;
// Formula: steps / 1300
// We use 1300.0 to ensure decimal division (double)
km = sw / 1300.0;
}
// Member Method to display results
void display() {
System.out.println("User Name: " + name);
System.out.println("Estimated Calories Burned: " + eb);
System.out.println("Estimated Distance (Km): " + km);
}
// Main method to invoke the methods
public static void main(String[] args) {
StepTracker obj = new StepTracker();
obj.accept();
obj.calculate();
obj.display();
}
}
shaalaa.com
Is there an error in this question or solution?
2025-2026 (March) Official Board Paper
