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.
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