JAVA
Using the data provided in the attachment, create a program that will display a child's predicted adult height.
This program will need to create an array of objects to search.
This program needs to validate the input for valid range and input values. If invalid data is given, the program needs to indicate that data is invalid and suggest appropriate values/ranges. However, you do not need to validate alphabetic data given for numeric input.
1. The program will need to prompt the user for a child's gender, age, and height in inches.
2. The program will take this data and compute and display the expected adult height. The data is to be obtained via an array of objects.
3. The program will need to prompt to try again. Ten extra credit points will be awarded to additionally display the expected height in feet and inches. All other requirements must be met to qualify.
The general tasks are as follows:
Create 2 fixed arrays for the data. One array for girl's data. One array for boy's data. You will need to type in the values given into your program's code..
You can ignore ages 1/4, 1/2, 3/4, 1 1/2 and 2 1/2. Thus the age should correspond to the index of the array. I.E - Birth is index 0, Age 1 is index 1, Age 2 is index 2, etc. The contents of the arrays needs to be the growth percentage for that age as in the attachment.
Create a class that would indicate age, gender, and growth percentage. Use appropriate data types. If you wish, the class can be based on age and include both boy and girl percentages.
Using data from the arrays, create objects for this data and store the objects into a new array. The array of objects is to be used to obtaining data for computations.
========================
JAVA PROGRAM
========================
///////////////HeightInfo.java////////////////////
/**
*Class: HeightInfo
*/
public class HeightInfo {
//private fields
private int age;
private double growthPercentageBoy;
private double growthPercentageGirl;
//constructor
public HeightInfo(int age, double growthPercentageBoy,
double growthPercentageGirl) {
this.age = age;
this.growthPercentageBoy =
growthPercentageBoy;
this.growthPercentageGirl =
growthPercentageGirl;
}
//accessors
public int getAge() {
return age;
}
public double getGrowthPercentageBoy() {
return growthPercentageBoy;
}
public double getGrowthPercentageGirl() {
return growthPercentageGirl;
}
}
///////////////////////////HeightPredictor.java////////////////////////////
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Class: HeightPredictort
*/
public class HeightPredictor {
//constant field to hold boys and girls growth
percentages
//each index holds the growth percentage at that
age
//e.g. index = 0 means age = 0, index = 1 means age =
1
private static final double[] BOYS_GROWTH_PERCENTAGE =
{28.6,42.2,49.5,53.8,58.0,61.8,
65.2,69.0,72.0,75.0,78.0,81.1,
84.2,87.3,91.5,96.1,98.3,99.3,99.8};
private static final double[] GIRLS_GROWTH_PERCENTAGE
= {30.9,44.7,52.8,57.0,61.8,66.2,
70.3,74.0,77.5,80.7,84.4,88.4,
92.9,96.5,98.3,99.1,99.6,100.0,100.0};
//main method
public static void main(String[] args){
//create array of HeightInfo
objects from the above arrays
HeightInfo[] heightInfoList =
createHeightInfo();
//fields to capture user
input
String gender;
int age;
double heightInInch;
//create scanner instance to read
input from console
Scanner sc = new
Scanner(System.in);
//read and validate child's
gender
do{
System.out.println("Enter child's gender (M/F):");
gender =
sc.nextLine();
if(gender.equalsIgnoreCase("M") ||
gender.equalsIgnoreCase("F")){
break;//if valid value, then break
}
System.out.println("Invalid gender. Please try again!");
}while(true);
//read and validate child's
age
do{
System.out.println("Enter child's age ( 0 to 18 ):");
age =
Integer.parseInt(sc.nextLine());
if(age >= 0
&& age <= 18){
break;//if valid value, then break
}
System.out.println("Invalid age. Please try again!");
}while(true);
//read and validate child's height
in inches
do{
System.out.println("Enter child's height in inches:");
heightInInch =
Double.parseDouble(sc.nextLine());
if(heightInInch
> 0){
break;//if valid then break
}
System.out.println("Invalid height. Please try again!");
}while(true);
//call generatePredictedHeight
method to generte predicted height
double predictedHeight =
generatePredictedHeight(age, gender, heightInInch,
heightInfoList);
//print height in inch and then in
ft/inch
printHeight(predictedHeight);
}
/**
* Method to create Array of HeightInfo from 2 constant
arrays
* of growth percentage
* @return
*/
private static HeightInfo[] createHeightInfo(){
HeightInfo[] heightInfoArr = new
HeightInfo[BOYS_GROWTH_PERCENTAGE.length];
for(int age = 0 ; age <
BOYS_GROWTH_PERCENTAGE.length; age++){
HeightInfo hi =
new HeightInfo(age, BOYS_GROWTH_PERCENTAGE[age],
GIRLS_GROWTH_PERCENTAGE[age]);
heightInfoArr[age] = hi;
}
return heightInfoArr;
}
/**
* Print height in inch and then in feet/inch
* from the given value heightInInch
* @param heightInInch
*/
private static void printHeight(double
heightInInch){
DecimalFormat df = new
DecimalFormat("#.0");
int feetValue =
(int)(heightInInch/12);
System.out.println("The expected
height is = "+df.format(heightInInch)+ " inch"+" or "+
feetValue+ "feet "+df.format(heightInInch%12)+"
inch");
}
/**
* Generate Predicted height of the given gender
* with the given heightInInch at given age
* It will consult the passed heightInfoArr to get the
growth PErcentage
* of the child
* Finally it will return the predicted height
* @param age
* @param gender
* @param heightInInch
* @param heightInfoArr
* @return
*/
private static double generatePredictedHeight(int age,
String gender,
double
heightInInch,HeightInfo[] heightInfoArr){
double growthPercentage = 0;
double predictedHeight = 0;
//iterate through the
heightInfoArr
for(HeightInfo hi:
heightInfoArr){
if(hi.getAge()
== age){ //if age matches
//get the growth percentage based on
gender
if(gender.equalsIgnoreCase("M")){
growthPercentage =
hi.getGrowthPercentageBoy();
}else if(gender.equalsIgnoreCase("F")){
growthPercentage =
hi.getGrowthPercentageGirl();
}
break;
}
}
if(growthPercentage!= 0){
//calculate the
predicted height
predictedHeight
= heightInInch * (100 / growthPercentage);
}
return predictedHeight;
}
}
==============================
OUTPUT
==============================
RUN1
Enter child's gender (M/F):
123
Invalid gender. Please try again!
Enter child's gender (M/F):
M
Enter child's age ( 0 to 18 ):
-25
Invalid age. Please try again!
Enter child's age ( 0 to 18 ):
23
Invalid age. Please try again!
Enter child's age ( 0 to 18 ):
12
Enter child's height in inches:
60
The expected height is = 71.3 inch or 5feet 11.3 inch
RUN2
Enter child's gender (M/F):
F
Enter child's age ( 0 to 18 ):
4
Enter child's height in inches:
-5
Invalid height. Please try again!
Enter child's height in inches:
36.5
The expected height is = 59.1 inch or 4feet 11.1 inch
JAVA Using the data provided in the attachment, create a program that will display a child's...
Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that holds an array for the integer values 1-10. Pass the array to a method that will calculate the values to the power of 2 of each element in the array. Then return the list of calculated values back to the main method and print the values
Q1. Write a program in Java a. Create 2 separate arrays, of user defined values, of same size b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....
Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A if A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1. Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and...
In Java Problem Description/Purpose: Write a program that will compute and display the final score of two teams in a baseball game. The number of innings in a baseball game is 9. Your program should read the name of the teams. While the user does not enter the word done for the first team name, your program should read the second team name and then read the number of runs for each Inning for each team, calculate the...
Hello. I am using a Java program, which is console-baed. Here is my question. Thank you. 1-1 Write a Java program, using appropriate methods and parameter passing, that obtains from the user the following items: a person’s age, the person’s gender (male or female), the person’s email address, and the person’s annual salary. The program should continue obtaining these details for as many people as the user wishes. As the data is obtained from the user validate the age to...
In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...
Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...
I should use the array and loop to create a java program according to the instruction, but I have no idea how to do it. Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...
JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...
In this lab, you will create a program to help travelers book flights and hotel stays that will behave like an online booking website. The description of the flights and hotel stays will be stored in two separate String arrays. In addition, two separate arrays of type double will be used to store the prices corresponding to each description. You will create the arrays and populate them with data at the beginning of your program. This is how the arrays...