194k views
4 votes
Create an application in Java that asks a user for a number of hours, days, weeks, and years. It then computes the equivalent number of minutes (ignoring leap years).

1 Answer

3 votes

Answer:

//import the Scanner class

import java.util.Scanner;

//Begin class definition

public class NumberOfMinutes{

//Begin main method

public static void main(String []args){

//Create an object of the Scanner class

Scanner input = new Scanner(System.in);

//initialize a variable nm to hold the number of minutes

int nm = 0;

//Prompt the use to enter the number of hours

System.out.println("Please enter the number of hours");

//Receive the input using the Scanner object and

//Store the entered number of hours in a variable nh

int nh = input.nextInt();

//Prompt the user to enter the number of days

System.out.println("Please enter the number of days");

//Receive the input using the Scanner object and

//Store the entered number of days in a variable nd

int nd = input.nextInt();

//Prompt the user to enter the number of weeks

System.out.println("Please enter the number of weeks");

//Receive the input using the Scanner object and

//Store the entered number of weeks in variable nw

int nw = input.nextInt();

//Prompt the user to enter the number of years

System.out.println("Please enter the number of years");

//Receive the input using the Scanner object and

//Store the entered number of years in a variable ny

int ny = input.nextInt();

//Convert number of hours to minutes and

//add the result to the nm variable

nm += nh * 60;

//Convert number of days to minutes and

//add the result to the nm variable

nm += nd * 24 * 60;

//Convert number of weeks to minutes and

//add the result to the nm variable

nm += nw * 7 * 24 * 60;

//Convert number of years to minutes and

//add the result to the nm variable

nm += ny * 52 * 7 * 24 * 60;

//Display the number of minutes which is stored in nm

System.out.println("The number of minutes is " + nm);

} //End main method

} //End of class definition

Sample Output:

Please enter the number of hours

>>12

Please enter the number of days

>>2

Please enter the number of weeks

>>4

Please enter the number of years

>>5

The number of minutes is 2664720

Step-by-step explanation:

The code contains comments explaining every line of the program. Please go through the comments. The actual lines of executable code are written in bold face to distinguish them from comments.

A sample output has also been provided above. Also, a snapshot of the program file, showing the well-formatted code, has been attached to this response.

Create an application in Java that asks a user for a number of hours, days, weeks-example-1
User Denizdurmus
by
6.5k points