Write a program called CalculateSeconds.java. This program should do the following:
1. Prompt the user to enter her/his name.
2. Greet the user by name and ask to enter a number of years (assume it will be an integer).
2. Compute the number of seconds in the number of years entered by the user (this should be done by calling the numOfSeconds() method).
3. Print the resulting number of seconds as shown below.
The numOfSeconds() method will:
1. Take an integer value as the input parameters
2. Calculate the number of seconds in the given number of years (assume that each year has exactly 365 days, with 24 hours in each day). Note, that you would need to use long and not int for these calculations to be accurate for a large number of years!
3. Return the number of seconds (as a long, not an int).
output
Please enter your name:
Peter
Hello, Peter. Please enter the number of years:
15
There are 473040000 seconds in 15 years, Peter.
import java.util.Scanner;
public class CalculateSeconds {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
// 1. Prompt the user to enter her/his name.
System.out.println("Please enter your name: ");
String name = keyboard.nextLine();
// 2. Greet the user by name and ask to enter a number of years (assume it will
// be an integer).
System.out.println("Hello, " + name + ". Please enter the number of years: ");
int years = keyboard.nextInt();
// 2. Compute the number of seconds in the number of years entered by the user
// (this should be done by calling the numOfSeconds() method).
long seconds = numOfSeconds(years);
// 3. Print the resulting number of seconds as shown below.
System.out.println("There are " + seconds + " seconds in "+years+" years, " + name + ".");
//
keyboard.close();
}
/**
* The numOfSeconds() method will:
*
* 1. Take an integer value as the input parameters
*
* 2. Calculate the number of seconds in the given number of years (assume that
* each year has exactly 365 days, with 24 hours in each day). Note, that you
* would need to use long and not int for these calculations to be accurate for
* a large number of years!
*
* 3. Return the number of seconds (as a long, not an int).
*
* @param years
* @return
*/
private static long numOfSeconds(int years) {
return years*86400*365;
}
}
Comments
Leave a comment