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.
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); }); });
Give the output of the following Java program segment:
String a[] = {"Atasi", "Aditi", "Anant", "Amit", "Ahana"};
System.out.println(a[1].charAt(1) + "*" + a[2].charAt(2));
The output of the above statement is:
Assertion (A): The substring() method modifies the original String.
Reason (R): The substring() method can extract part of a String from a specific index.
Rewrite the following program segment using a for loop:
Given program segment:
int a = 5, b = 10;
while (b > 0)
{
b -= 2;
}
System.out.println(a * b);
Note: Ensure that the variable $b$ remains accessible for the System.out.println statement after the loop terminates.