Someone please code this: Code a program that prompts the user for the size of t
ID: 3869519 • Letter: S
Question
Someone please code this:
Code a program that prompts the user for the size of the array.
Using the size, the program creates several arrays that will store information about your family.
There will be one array for all your family member's names, another for their roles, and the last one for their ages.
The values in each array at the same location are for a given family member.
Once the arrays are populated, print the information from each array.
The size and input will be the only class variables.
Use printf().
Code your program according to the specifications below.
Use the looping structure generally associated with array processing.
Name the program YourLastNameFirstInitialLE62.java.
The methods will be called from the main().
1st Input Prompt: Code this in a method named arraySize().
How many family members in your immediate family including yourself?
2nd Input Prompt: In a method named setNames declare the familyNames array and populate it. This method will return the familyName array to the calling statement. The 9 in the prompt below will print 1 for the first family member, then 2 for the 2nd one and so forth. Use the loop-control variable as the value for 9.
Enter family member 9:
3rd Input Prompt: In a method named setRoles declare the familyRoles array and populate it. The Xs in the prompt is the name of the family member. The setRoles method will return the familyRoles array to the calling statement as well as receive the familyNames array from the calling statement. This method will be called as an argument in the call to printFamilyInfo().
What relationship is Xxxxxxxxxx to you?
4th Input Prompt: In a method named setAges declare the familyAges array and populate it. The Xs in the prompt is the name of the family member. The setAges method will return the familyAges array to the calling statement as well as receive the familyNames array from the calling statement. setAges() will be called as an argument in the call to printFamilyInfo().
How old is Xxxxxxxxxx?
Output Specifications: Where the Xs represent the family member's name and role and the Zs the age. Code the output print statement in a method named printFamilyInfo() that accepts the familyNames, familyRoles, and familyAges arrays. In the printFamilyInfo method use a for loop to print the information for each family member from each array. The print for the header has to be outside of the for loop because it's printed only once.
MY FAMILY
Name: Xxxxxxxxxx
Role: Xxxxxxxxxx
Age: ZZ9
Name: Xxxxxxxxxx
Role: Xxxxxxxxxx
Age: ZZ9
Here is the sample OUTPUT:
How many family members in your immediate family including yourself? 4
Enter family member 1: Fred
Enter family member 2: Wilma
Enter family member 3: Pebbles
Enter family member 4: Bambam
What relationship is Fred to you? Father
What relationship is Wilma to you? Self
What relationship is Bambam to you? Brother
How old is Fred? 45
How old is Wilma? 43
How old is Pebbles? 18
How old is Bambam? 15
MY FAMILY
Name: Fred
Role: Father
Age: 45
Name: Wilma
Role: Mother
Age: 43
Name: Pebbles
Role: Self
Age: 18
Name: Bambam
Role: Brother
Age: 15
Explanation / Answer
Below is your code. Let me know if you have any issue in comments.,...
YourLastNameFirstInitialLE62.java
import java.util.Scanner;
public class YourLastNameFirstInitialLE62 {
public static int arraySize(Scanner inp) {
System.out.printf("How many family members in your immediate family including yourself?");
return Integer.parseInt(inp.next());
}
public static String[] setNames(int size, Scanner sc) {
String[] familyNames = new String[size];
for (int i = 0; i < size; i++) {
System.out.printf("Enter family member %d : ", (i + 1));
familyNames[i] = sc.next();
}
return familyNames;
}
public static String[] setRoles(String[] familyNames, Scanner sc) {
String[] familyRoles = new String[familyNames.length];
for (int i = 0; i < familyNames.length; i++) {
System.out.printf("What relationship is %s to you? ", familyNames[i]);
familyRoles[i] = sc.next();
}
return familyRoles;
}
public static Integer[] setAges(String[] familyNames, Scanner sc) {
Integer[] familyAges = new Integer[familyNames.length];
for (int i = 0; i < familyNames.length; i++) {
System.out.printf("How old is %s? ", familyNames[i]);
familyAges[i] = Integer.parseInt(sc.next());
}
return familyAges;
}
public static void printFamilyInfo(String[] familyNames, String[] familyRoles, Integer[] familyAges) {
System.out.printf(" MY FAMILY ");
for (int i = 0; i < familyNames.length; i++) {
System.out.printf(" Name: %s", familyNames[i]);
System.out.printf(" Role: %s", familyRoles[i]);
System.out.printf(" Age: %d", familyAges[i]);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = arraySize(sc);
String[] familyNames = setNames(size, sc);
printFamilyInfo(familyNames, setRoles(familyNames, sc), setAges(familyNames, sc));
}
}
Sample Run: -
How many family members in your immediate family including yourself?4
Enter family member 1 : Fred
Enter family member 2 : Wilma
Enter family member 3 : Pebbles
Enter family member 4 : Bambam
What relationship is Fred to you? Father
What relationship is Wilma to you? Mother
What relationship is Pebbles to you? Self
What relationship is Bambam to you? Brother
How old is Fred? 44
How old is Wilma? 42
How old is Pebbles? 23
How old is Bambam? 21
MY FAMILY
Name: Fred
Role: Father
Age: 44
Name: Wilma
Role: Mother
Age: 42
Name: Pebbles
Role: Self
Age: 23
Name: Bambam
Role: Brother
Age: 21
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.