54.0k views
5 votes
A. Download the attached �Greetings.java� file.

b. Implement the �getGreetings� method, so that the code prompts the user to enter the first name, the last name, and year of birth, then it returns a greetings message in proper format (see the example below). The �main� method will then print it out. Here is an example dialogue with the user:
Please enter your first name: tom
Please enter your last name: cruise
Please enter your year of birth: 1962
Greetings, T. Cruise! You are about 50 years old.
Note that the greetings message need to be in the exact format as shown above (for example, use the initial of the first name and the first letter of the last name with capitalization).
c. Submit the final �Greetings.java� file (DO NOT change the file name) online.

1 Answer

2 votes

Answer:

Code:

import java.util.*;

public class Greetings

{

public static void main(String[] args)

{

Scanner s = new Scanner(System.in);

System.out.println(getGreetings(s));

}

private static String getGreetings(Scanner console)

{

System.out.print("Please enter your first name: ");

String firstName=console.next();

char firstChar=firstName.toUpperCase().charAt(0);

System.out.print("Please enter your last name: ");

String lastName=console.next();

lastName=lastName.toUpperCase().charAt(0)+lastName.substring(1, lastName.length());

System.out.print("Please enter your year of birth: ");

int year=console.nextInt();

int age=getCurrentYear()-year;

return "Greetings, "+firstChar+". "+lastName+"! You are about "+age+" years old.";

}

private static int getCurrentYear()

{

return Calendar.getInstance().get(Calendar.YEAR);

}

}

Output:-

A. Download the attached �Greetings.java� file. b. Implement the �getGreetings� method-example-1
User Janek
by
3.3k points