Java: Write a program that accepts a number of minutes and converts it both to hours and days.

During my travels on the internet, I ran across a neat little programming project on brainly.com. The project / problem I found is below:

Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes equals 100 hours and equals 4.167 days. Save the class as minutesconversion.java

This type of project builds your programming skills and mathematic skills all at the same time!. Anyways and without further ado, lets get down to business and figure out the math and then write some code.

First, we need to figure out the mathematics and then we can write the code.

Question:  How can we convert minutes to hours and day?

Answer:

To convert minutes to hours, we can times the minutes by the conversion factor  \frac{1 hr}{60 min}

To get our days, we can convert the hours to days by multiplying the hours by the following conversion factor \frac{1 day}{24hr}

One solution to the problem:

 
public static void main(String[] args) {
         //Get input from command line
         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
         System.out.print("Enter the number of minutes: ");
          try {
              //Get input and turn to a float
              Float fltNumMins = Float.parseFloat(reader.readLine());
              //Convert minutes to hours
              Float fltHrs = fltNumMins /60;
              //Convert hours to days
              Float fltDays = fltHrs / 24; 
              //Output our data.
              System.out.println("Minutes: " + fltNumMins + " Hours: " + fltHrs + " Days: " + fltDays);
          }
          catch (IOException e) {
              System.err.println("Error: " + e);
          }
    }

Well, I hope this helps a striving computer programmer or computer scientist . Note, there are other ways and better ways. For instance, you could round the output to 3 decimal places. Anyways, the code will get you started. Good Luck!

 

Leave a Reply

Your email address will not be published. Required fields are marked *