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:
Walter Savitch ,kenrick Mock
Chapter:
Flow Of Control
Exercise:
Programming Projects
Question:3 | ISBN:9780132830317 | Edition: 5

Question

One way to estimate the adult height of a child is to use the following formula, which uses the height of the parents:

All heights are in inches. Write a program that takes as input the gender of the child, the height of the mother in inches, and the height of the father in inches, and outputs the estimated adult height of the child in inches. The program should allow the user to enter a new set of values and output the predicted height until the user decides to exit. The user should be able to input the heights in feet and inches, and the program should output the estimated height of the child in feet and inches. Use the int data type to store the heights.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.util.Scanner;
public class HeightDemo 
{
	public static void main(String[] args) 
	{
		String genderChild, units, userChoice;
		int fatherHeight, motherHeight, childHeight;

		Scanner scn = new Scanner(System.in);
		while (true) 
		{
			fatherHeight = 0;
			motherHeight = 0;
			childHeight = 0;
			System.out.println("Enter your userChoice('boy' or 'girl'):");
			genderChild = scn.next();
			System.out.println("Enter 'feet' for feet  and inches,or 'inch' for inches:");
			units = scn.next();
			if (units.equals("feet")) 
			{
				System.out.println("Enter  father's height feet:");
				fatherHeight = 12 * scn.nextInt();
				System.out.println("Enter father's height inches:");
				fatherHeight += scn.nextInt();
				System.out.println("Enter mother's height feet:");
				motherHeight = 12 * scn.nextInt();
				System.out.println("Enter mother's height inches:");
				motherHeight += scn.nextInt();
			}
			else if (units.equals("inch")) 
			{
				System.out.println("Enter father's height inches:");
				fatherHeight = scn.nextInt();
				System.out.println("Enter mother's height inches:");
				motherHeight = scn.nextInt();
			}
		   if (genderChild.equals("boy")) 
			{
				childHeight = (int) (((motherHeight * 13 / 
						12.0)	+ fatherHeight) / 2.0);
			}
			else if (genderChild.equals("girl"))
			{
				childHeight = (int) (((fatherHeight + 12 
						/13.0)+ fatherHeight) / 2.0);
			}
			if (units.equals("feet")) 
			{
	System.out.println("The children will be"  
			+ " about " + childHeight / 12 + " feet, "+ childHeight % 12 + " inches tall.");
			}
			else if (units.equals("inch"))
			{
		System.out.println("The children will be about " + childHeight +" inches tall.");
			}
			System.out.println("Enter 'yes' to continue (or) 'no' to exit.");
			userChoice = scn.next();
			if (userChoice.equals("yes"))
				System.out.println("Continuing...");
			else if (userChoice.equals("no"))
				break;
		}		
	}
}

Output:

Enter your userChoice('boy' or 'girl'):
boy
Enter 'feet' for feet  and inches,or 'inch' for inches:
feet
Enter  father's height feet:
5
Enter father's height inches:
5
Enter mother's height feet:
4
Enter mother's height inches:
5
The children will be about 5 feet, 1 inches tall.
Enter 'yes' to continue (or) 'no' to exit.
y
Enter your userChoice('boy' or 'girl'):
girl
Enter 'feet' for feet  and inches,or 'inch' for inches:
inch
Enter father's height inches:
7
Enter mother's height inches:
7
The children will be about 7 inches tall.
Enter 'yes' to continue (or) 'no' to exit.
no

 

0 0

Discussions

Post the discussion to improve the above solution.