Question:

Define a class named StepTracker with the following specifications:

Member Variables:

  • String name — stores the user's name.
  • int sw — stores the total number of steps walked ($sw$).
  • double cb — stores the estimated calories burned ($cb$).
  • double km — stores the estimated distance walked in kilometers ($km$).

Member Methods:

1. void accept()
To input the name and the sw (steps walked) using Scanner class methods only.

2. void calculate()
Calculates calories burned ($cb$) and distance in ($km$) based on steps walked ($sw$) using the provided estimation logic.

Show Hint

Whenever a class-based Java question is asked, first identify the member variables, then write input methods, calculation methods, and finally an output method for proper structure.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Declare the class and member variables. 
First, we define a class named StepTracker. Inside this class, we declare the given data members: name, sw, cb, and km. These variables will store the user's name, number of steps walked, calories burned, and distance covered respectively.

Step 2: Write the accept() method. 
In this method, we use the Scanner class to take input from the user. The user's name is stored in name, and the total number of steps walked is stored in sw.

Step 3: Write the calculate() method.
This method calculates the values of calories burned and distance walked. Since the estimation table is not fully visible in the image, placeholders are written where the actual formulas should be inserted.

Step 4: Write the program structure.

import java.util.Scanner;

class StepTracker
{
    String name;
    int sw;
    double cb;
    double km;

    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter name: ");
        name = sc.nextLine();
        System.out.print("Enter steps walked: ");
        sw = sc.nextInt();
    }

    void calculate()
    {
        // Insert the exact formulas here according to the estimation table
        // Example:
        // cb = ...
        // km = ...
    }

    void display()
    {
        System.out.println("Name = " + name);
        System.out.println("Steps Walked = " + sw);
        System.out.println("Calories Burned = " + cb);
        System.out.println("Distance Walked = " + km + " km");
    }
}

Step 5: Final conclusion.
Thus, the class StepTracker is defined with the required member variables and methods. The exact formulas for cb and km should be filled according to the estimation table shown in the complete question.

document.addEventListener("DOMContentLoaded", function () { renderMathInElement(document.body, { delimiters: [ { left: "\\[", right: "\\]", display: true }, { left: "\\(", right: "\\)", display: false } ] }); document.querySelectorAll("pre code").forEach(function (block) { hljs.highlightElement(block); }); }); 

Was this answer helpful?
0
0