KnowledgeBoat Logo
|

Computer Applications

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 by the user.
  • double cb – stores the estimated calories burned by the user.
  • double km – stores the estimated distance walked in kilometers.

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:
MetricCalculation Formula
Calories Burnedsteps walked x 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.

Java

Java Classes

5 Likes

Answer

import java.util.Scanner;

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

    public 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();
    }

    public void calculate()
    {
        cb = sw * 0.04;
        km = sw / 1300.0;
    }

    public void display()
    {
        System.out.println("Name: " + name);
        System.out.println("Calories Burned: " + cb);
        System.out.println("Distance (km): " + km);
    }

    public static void main(String args[])
    {
        StepTracker obj = new StepTracker();
        obj.accept();
        obj.calculate();
        obj.display();
    }
}

Output

BlueJ output of Define a class named StepTracker with the following specifications: Member Variables: (a) String name – stores the user's name (b) int sw – stores the total number of steps walked by the user. (c) double cb – stores the estimated calories burned by the user. (d) double km – stores the estimated distance walked in kilometers. Member Methods: (e) void accept() – to input the name and the steps walked using Scanner class methods only. (f) void calculate() – calculates calories burned and distance in km based on steps walked using the following estimation table: (g) 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.

Answered By

3 Likes


Related Questions