85.7k views
3 votes
A pedometer treats walking 1 step as walking 2.5 feet. Define a method named feetToSteps that takes a double as a parameter, representing the number of feet walked, and returns an integer that represents the number of steps walked. Then, write a main program that reads the number of feet walked as an input, calls method feetToSteps() with the input as an argument, and outputs the number of steps.

Use floating-point arithmetic to perform the conversion.

Ex: If the input is:

150.5
the output is:

60
The program must define and call a method:
public static int feetToSteps(double userFeet)

CODE:
import java.util.Scanner;

public class LabProgram {

/* Define your method here */

public static void main(String[] args) {
/* Type your code here. */
}
}

User Jana
by
9.2k points

1 Answer

5 votes

Answer:

Here's the completed code that implements the feetToSteps method and the main program that reads the number of feet walked as input and outputs the number of steps walked:

import java.util.Scanner;

public class LabProgram {

public static int feetToSteps(double userFeet) {

double steps = userFeet / 2.5; // Convert feet to steps

return (int) Math.round(steps); // Round steps and convert to integer

}

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

double feetWalked = scnr.nextDouble();

int stepsWalked = feetToSteps(feetWalked);

System.out.println(stepsWalked);

scnr.close();

}

}

Step-by-step explanation:

In this code, the feetToSteps method takes a double parameter userFeet, representing the number of feet walked, and converts it to the number of steps walked by dividing it by 2.5 (since 1 step = 2.5 feet). The result is then rounded to the nearest integer using the Math.round method and casted to an integer before being returned.

The main program reads the number of feet walked as input using a Scanner object, calls the feetToSteps method with the input as an argument, and outputs the number of steps walked using System.out.println. Finally, the Scanner object is closed to free up system resources.

User Aerial
by
7.8k points