ANSWER
The JAVA program after simplification is as below.
import java.util.Scanner;
public class CalcMiles {
// variables declaration
static double milesPerHour;
static double minutesTraveled;
static double hoursTraveled;
static double milesTraveled;
// method declared static
public static void mphAndMinutesToMiles(double speed, double mins)
{
// computations to calculate distance travelled
hoursTraveled = mins / 60.0;
milesTraveled = hoursTraveled * speed;
// result displayed on the screen
System.out.println("Miles: " + milesTraveled);
}
// Scanner object created inside main()
// user input taken inside main()
public static void main(String [] args)
{
Scanner scnr = new Scanner(System.in);
System.out.println("Enter miles travelled per hour ");
milesPerHour = scnr.nextDouble();
System.out.println("Enter minutes required to travel ");
minutesTraveled = scnr.nextDouble();
mphAndMinutesToMiles(milesPerHour, minutesTraveled);
}
}
OUTPUT
Enter miles travelled per hour
2.3
Enter minutes required to travel
1234
Miles: 47.30333333333333
EXPLANATION
The program is simplified as explained below.
1. User input is taken using the object of Scanner class.
2. This object of Scanner class can only be created inside main() method hence, user input can only be taken inside main().
3. The code to calculate the result is separated in another method, mphAndMinutesToMiles(). This method is declared with return type void since no value is returned.
4. After user input is taken in main() for miles travelled per hour and minutes required to travel, these values are passed as parameters to the mphAndMinutesToMiles() method.
5. These parameters are used in calculation part. The total miles travelled in total hours (obtained from minutes), is calculated. The code to display the result is added to this method.
6. Inside the main method, only the code to create Scanner object, code to take user input for two variables and code to call the mphAndMinutesToMiles() is included.
7. The variables are declared as static and declared at class level.
8. No variable is declared inside any of the two methods.