Answer to Question #238367 in Java | JSP | JSF for java

Question #238367

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.


1
Expert's answer
2021-09-17T02:04:27-0400


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;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS