Write a program using an overloaded function(s) named convert() that work as given below:
i. public void convert(int seconds)
This function converts and displays number of seconds to hour, minutes and seconds
ii. public void convert(double days)
This function converts and displays number of days to years, months and days
Call the above functions from main.
import java.util.Scanner;
public class Converter {
/**
* This function converts and displays number of seconds to hour, minutes
* and seconds
*
* @param seconds
*/
public void convert(int seconds) {
int minutes = seconds / 60;
int hours = minutes / 60;
System.out.println(seconds + " seconds is equivalent to " + hours + " hours " + (minutes % 60) + " minutes " + (seconds % 60) + " seconds.");
}
/**
* This function converts and displays number of days to years, months and
* days
*
* @param days
*/
public void convert(double days) {
int currentDays = (int) days;
int years = currentDays / 365;
currentDays = currentDays % 365;
int months = currentDays / 30;
currentDays = currentDays % 30;
System.out.println((int) days + " days is equivalent to " + years + " years " + months + " months " + currentDays + " days.");
}
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
Converter converter = new Converter();
Scanner input = new Scanner(System.in);
System.out.print("Enter number of seconds: ");
int seconds = input.nextInt();
converter.convert(seconds);
System.out.print("Enter number of days: ");
double days = input.nextInt();
converter.convert(days);
input.close();
}
}
Comments
Leave a comment