SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Introduction To Parameters And Objects
Exercise:
Exercises
Question:14 | ISBN:9780136091813 | Edition: 2

Question

Write a method called inputBirthday that accepts a Scanner for the console as a parameter and prompts the user to enter a month, day, and year of birth, then prints the birthdate in a suitable format. Here is an example dialogue with the user:
On what day of the month were you born? 8
What is the name of the month in which you were born? May
During what year were you born? 1981
You were born on May 8, 1981. You’re mighty old!

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

import java.util.Scanner;
public class Ch03Ex14
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		
		inputBirthday(keyboard);
	}

	public static void inputBirthday(Scanner keyboard)
	{
		System.out.print("On what day of the month were you born? ");
		int day = keyboard.nextInt();

		System.out.print("What is the name of the month in which you were born? ");
		String month = keyboard.next();

		System.out.print("During what year were you born? ");
		int year = keyboard.nextInt();

		System.out.println("You were born on " + month + " " + day + ", " + year + ". You're mighty old!");
	}
}

Output:

On what day of the month were you born? 8
What is the name of the month in which you were born? May
During what year were you born? 1981
You were born on May 8, 1981. You're mighty old!

 

0 0

Discussions

Post the discussion to improve the above solution.