In: Computer Science
JAVA:
Write a program to perform time conversion. The user will select from the following menu: Hours to minutes Days to hours Minutes to hours Hours to days. Each choice has a separate method which is called when the user makes the selection. Please see the output below: Hours to minutes. 2. Days to hours. 3. Minutes to hours. 4. Hours to days. Enter your choice: 2 Enter the number of days: 5 There are 120 hours in 5 days.
Program:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Scanner class declaration
int choice=0; // variable declaration
System.out.println("1 Hours to minutes");
System.out.println("2 Days to hours");
System.out.println("3 Minutes to hours");
System.out.println("4 Hours to days");
System.out.print("Enter your choice: ");
choice=sc.nextInt(); // Accept the choice from the user
switch(choice) // switch case to select the choice
{
case 1:hoursToMinutes(); // If you want to choose hours to minutes conversion
break;
case 2:daysToHours(); // If you want to choose days to hours conversion
break;
case 3:minutesToHours(); // If you want to choose minutes to hours conversion
break;
case 4:hoursToDays(); // If you want to choose hours to Days conversion
break;
}
}
public static void hoursToMinutes() { // called function
Scanner sc = new Scanner(System.in); // Scanner class declaration
int minutes; // variable declaration
System.out.print("Enter the number of hours: ");
int hours=sc.nextInt(); // Accept the number of hours
minutes = hours*60; // calculate minutes
System.out.println("There are "+minutes+" minutes in "+hours+" hours."); // print the number of minutes
}
public static void daysToHours() { // called function
Scanner sc = new Scanner(System.in); // Scanner class declaration
int hours; // variable declaration
System.out.print("Enter the number of days: ");
int days=sc.nextInt(); // Accept number of days
hours = days*24; // calculate numebr of hours
System.out.println("There are "+hours+" hours in "+days+" days"); // print the number of hours
}
public static void minutesToHours() { // called function
Scanner sc = new Scanner(System.in); // Scanner class declaration
float hours; // variable declaration
System.out.print("Enter the number of minutes: ");
int minutes=sc.nextInt(); // Accept the number of minutes
hours = (float)minutes/60; // calculate the number of hours
System.out.println("There are "+hours+" hours in "+minutes+" minutes"); // print the number of hours
}
public static void hoursToDays() { // called function
Scanner sc = new Scanner(System.in); // Scanner class declaration
float days; // variable declaration
System.out.print("Enter the number of hours: ");
int hours=sc.nextInt(); // Accept the number of hours
days = (float)hours/24; // calculate the number of days`
System.out.println("There are "+days+" days in "+hours+" hours"); // print the number of days
}
}
Output: