Question

In: Computer Science

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

Write a program that accepts a number of minutes and converts it to days and hours. For example, 6000 minutes represents 4 days and 4 hours. Be sure to provide proper exception handling for non-numeric values and for negative values.

Save the file as  MinuteConversionWithExceptionHandling.java

Solutions

Expert Solution

MinuteConversionWithExceptionHandling.java

import java.util.Scanner;
import java.io.IOException;
import java.util.*;

public class Main
{
   public static void main(String[] args) {
       int minutes;
       int days;
       int hours;
       int min;
       Scanner input = new Scanner(System.in);
       try {
       //Read the input
       System.out.print("Enter the minutes to convert:");
minutes =input.nextInt();
//Check for the negative input
if(minutes<0)
throw new Exception("The entered number is negative");
//Convert to days
days=minutes/(24*60);
//convert to hours
hours = (minutes%(24*60))/60;
//convert to minutes
min=(minutes%(24*60))%60;
System.out.println(days+"days"+" "+hours+"hours"+" "+min+"minutes");
  
}
//for non-numeric input
catch(InputMismatchException e) {
System.out.println("Invalid input,Please enter integer");
       } catch(Exception e) {
       System.out.println(e.getMessage());
       }
   }
}

Output

Enter the minutes to convert:hfkesjf                                                                                          

Invalid input,Please enter integer  

Output

Enter the minutes to convert:-89                                                                                              

The entered number is negative

Output

Enter the minutes to convert:6000                                                                                             

4days 4hours 0minutes


Related Solutions

Write a program that accepts a number of minutes and converts it both to hours and...
Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. (I currently have what is below) import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numOfMinutes = sc.nextInt(); double hour = numOfMinutes/60.00; double days = (hour/24); System.out.println(numOfMinutes + " minutes is " + hour + " hours or " + days + " days.");...
PART A Write a program that converts a total number of seconds to hours, minutes, and...
PART A Write a program that converts a total number of seconds to hours, minutes, and seconds. It should do the following: Prompt the user for input Read an integer from the keyboard Calculate the result Use printf to display the output Your output must be of the format: totalSeconds seconds = numHours hours, numMinutes minutes, and numSeconds seconds For example: If the user entered: 5000 Your program would output: 5000 seconds = 1 hours, 23 minutes, and 20 seconds...
using C program Assignment Write a computer program that converts a time provided in hours, minutes,...
using C program Assignment Write a computer program that converts a time provided in hours, minutes, and seconds to seconds Functional requirements Input MUST be specified in hours, minutes, and seconds MUST produce the same output as listed below in the sample run MUST correctly compute times Nonfunctional requirements MUST adhere to program template include below MUST compile without warnings and errors MUST follow the code template provided in this assignment MUST NOT change " int main() " function MUST...
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
Write a program that converts the user's number into the desired unit of measurement. The user...
Write a program that converts the user's number into the desired unit of measurement. The user will pick the desired unit from a menu shown below. Ask the follow-up question (the number to convert) and do the math ONLY if the user picks a valid choice from the menu. You can see the conversions / multipliers needed for this program in the output section below. Menu/Prompt: Enter the number that corresponds to your desired unit conversion from the choices below:...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
Write a program that request a time interval in seconds and display it in hours, minutes,...
Write a program that request a time interval in seconds and display it in hours, minutes, second format. (java)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT