Question: LE 7.1 Create a separate class called Family. This class will have NO main(). Insert a default constructor in the Family class. This is a
LE 7.1
Create a separate class called Family. This class will have NO main().
Insert a default constructor in the Family class. This is a method that is empty and its header looks like this: public NameOfClass()
Except, for the main(), take all the other methods in LE 6.2 and put them in the Family class.
Transfer the class variables from LE 6.2 to Family.
Strip the word static from the class variables and the method headers in the Family class.
Cut and paste the main() from LE 6.2 into your program file for LE 7.1.Create an object of the family class.
Format: NameOfClass objectName = new NameOfClass();
Use that object for the method calls in the main().
Format: objectName.methodName()
Make sure both classes are in the same directory.
To run both programs, you must run the class with the main().
-----------------------------------------------
Here is LE62
import java.util.Scanner;
public class LE62 { public static int arraySize(Scanner inp) { System.out.printf("How many family members in your immediate family including yourself?"); return Integer.parseInt(inp.next()); }//End public static int arraySize public static String[] setNames(int size, Scanner input) { String[] familyNames = new String[size]; for (int i = 0; i < size; i++) { System.out.printf("Enter family member %d : ", (i + 1)); familyNames[i] = input.next(); }//End for return familyNames; }//End public static String[] setNames public static String[] setRoles(String[] familyNames, Scanner input) { 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] = input.next(); }//End For return familyRoles; }//End public static String[] setRoles public static Integer[] setAges(String[] familyNames, Scanner input) { 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(input.next()); } return familyAges; }//End public static Integer public static void printFamilyInfo(String[] familyNames, String[] familyRoles, Integer[] familyAges) { System.out.printf("%nMY FAMILY%n"); for (int i = 0; i < familyNames.length; i++) { System.out.printf("%nName: %s", familyNames[i]); System.out.printf("%nRole: %s", familyRoles[i]); System.out.printf("%nAge: %d%n", familyAges[i]); }//End for }//End public static void printFamilyInfo public static void main(String[] args) { Scanner input = new Scanner(System.in); int size = arraySize(input); String[] familyNames = setNames(size, input); printFamilyInfo(familyNames, setRoles(familyNames, input), setAges(familyNames, input)); System.exit(0); }//End Main }//End Class
-------Sample out put--------------
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? 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
