Question
A pedometer treats walking 2,000 steps as walking 1 mile. Write a stepsToMiles() method that takes the number of steps as an integer parameter and
A pedometer treats walking 2,000 steps as walking 1 mile. Write a stepsToMiles() method that takes the number of steps as an integer parameter and returns the miles walked as a double. The stepsToMiles() method throws an Exception object with the message "Exception: Negative step count entered." when the number of steps is negative. Complete the main() method that reads the number of steps from a user, calls the stepsToMiles() method, and outputs the returned value from the stepsToMiles() method. Use a try-catch block to catch any Exception object thrown by the stepsToMiles() method and output the exception message.
Output each floating-point value with two digits after the decimal point, which can be achieved as follows: System.out.printf("%.2f", yourValue);
Ex: If the input of the program is:
5345
the output of the program is:
2.67
Ex: If the input of the program is:
-3850
the output of the program is:
Exception: Negative step count entered.
import java.util.Scanner; import java.util.InputMismatchException;
public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int userNum, divNum, quotient; try { // Read userNum System.out.print("Enter userNum: "); userNum = input.nextInt(); // Read divNum System.out.print("Enter divNum: "); divNum = input.nextInt(); // Calculate the quotient quotient = userNum / divNum; // Print the result System.out.println("Quotient: " + quotient); } catch (ArithmeticException e) { // Catch ArithmeticException and print the error message System.out.println("Arithmetic Exception: " + e.getMessage()); } catch (InputMismatchException e) { // Catch InputMismatchException and print the error message System.out.println("InputMismatch Exception: " + e.toString()); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started