Answered step by step
Verified Expert Solution
Question
1 Approved Answer
For this assignment you will create & submit 3 classes: 1. Family 2. Friend 3. FriendsAndFamily (Main Class) Begin by creating a class named
For this assignment you will create & submit 3 classes: 1. Family 2. Friend 3. FriendsAndFamily (Main Class) Begin by creating a class named Family (This class will not have a main method) Below is the UML diagram for the class. Family - name: String - relation: String + Family(String n, String r) + toString(): String In the Family class: Data fields: name, is a global (instance) variable of type String relation, is a global (instance) variable of type String Constructor: There is no default constructor for this class The constructor will take two arguments (String n and String r) Use the "private = public" for each variable within the constructor o i.e. name = n Method: toString ( ) o Returns a neatly formatted String as seen in the images on the last page of this document (For FAMILY) Next, create a class named Friend (This class will not have a main method) Below is the UML diagram for the class. - name: String - timeKnown: int Friend + Friend(String n, int tk) + validateTimeKnown (int tk) : int +toString(): String In the Friend class: Data fields: name, is a global (instance) variable of type String timeKnown, is a global (instance) variable of type int Constructor: There is no default constructor for this class The constructor will take two arguments (String n and int tk) Use the "private = public" for the name variable within the constructor Assign the timeKnown variable to the validate Time Known method, passing tk Methods: Remember, refer to the UML diagram for arguments, public/private and return types validate TimeKnown ( ) Returns an integer value for timeKnown variable based on tk argument passed If the users defined argument is < 0 we assign timeKnown to 00 Otherwise, timeKnown is what the user passes to the method call toString() Returns a neatly formatted String as seen in the images on the last page of this document (For FRIEND) See next page for FriendsAndFamily class information & instructions Now, create a main class named FriendsAndFamily (This class will have a main method) Below is the UML diagram for the class. - Note: The instructions, variables and general information pertaining to the main method is not presented in the UML but will be below. Now, create a main class named FriendsAndFamily (This class will have a ma method) Below is the UML diagram for the class. - Note: The instructions, variables and general information pertaining to the main metho not presented in the UML but will be below. FriendsAndFamily + getSize(Scanner sc): Object[] + friendOrFamily(Scanner sc): int + getName (Scanner sc, int t) : String + getRelation(Scanner sc, String n) : String + getTimeKnown(Scanner sc, String n): int + display(Object[] faf) : void As these methods are part of the main class, you can assume all of the above methods are static In the FriendsAndFamily class: 1. In the main method, create the following variables: A. Scanner B. Object Array named friendsAndFamily (Declare but do not assign) C. Friend object (Declare but do not assign) i.e. -> Friend fri: D. Family object (Declare but do not assign) E. String name F. String relation G. int timeKnown H. int type 2. Write the following functionality for each of the methods: Remember to check the UML for return type and parameters A. getSize() I. II. Create integer size Use a validation loop to ensure size is positive III. Create an object array based on user-defined size variable IV. Return the array B. friendOrFamily ( ) I. II. Ask user if they are entering a friend or family member (String) Determine which String is entered (case does not matter) this information will be used with the type variable created earlier: If "friend" return 0 If "family" return 1 Otherwise, return 2 C. getName() I. Using the t variable passed, use a control structure to determine: + If t is 0 prompt for friend input If t is 1 prompt for family input Otherwise, end the program with a System.exit(0) II. Read in the given name using .next() III. Clear the buffer if needed (.nextLine()) IV. Return the given name D. getRelation ( ) I. Prompt user for their relation to given name II. Return relation variable (String) E. getTimeKnown ( ) I. II. Prompt user the length they have known their given friend Return integer variable for time known F. display ( ) I. Using an enhanced for loop, loop through the given object array printing the toString for each iteration 3. Back in the main method, use a for loop pertaining to the length of the object array, within this loop: A. Assign type to the method friend OrFamily B. If type is 0 then: I. II. Assign name to getName passing the appropriate variables Assign timeKnown to getTimeKnown passing the appropriate variables III. Assign fri to a new Friend object passing the appropriate variables IV. Assign the ith element of the array to fri C. If type is 1 then: I. II. Repeat the above, but instead of getTimeKnown call the relation method. Assign fam to a new Family object (not fri) III. Assign the ith element of the array to fam 4. Outside of the loop call the display method passing the object array. Images of programming Validate size of family is > 0 Note, negative year will result in 0 toString() method for friend class Output - CIT244 FA20 AC01 (run) x run: (Warning: When prompted for choice of "friend" or "family", anything other than friend or family will end the program) How many friends & family do you wish to enter? --> 0 How many friends & family do you wish to enter? --> 3 Is this a friend or family memember? --> FriEND Enter the name of this friend --> Tom How long have you known Tom --> -45 Is this a friend or family memember? --> FamiLY Enter the name of this family member --> Jane Enter your realtion to Jane --> First Cousin Is this a friend or family memember? --> Friend Enter the name of this friend --> Jake How long have you known Jake --> 12 Tom and you have you been friends for year(s). Case insensitive Notice different prompt shown for friend / family toString() method for friend class toString() method for family class Tom and you have you been friends for 0 year(s). Jane is part of your family, the realtionship is, First Cousin. Jake and you have you been friends for 12 year(s). BUILD SUCCESSFUL (total time: 26 seconds) Invalid input behavior run: (Warning: When prompted for choice of "friend" or "family", anything other than friend or family will end the program) How many friends & family do you wish to enter? --> 5 Is this a friend or family memember? --> family Enter the name of this family member --> Greg Enter your realtion to Greg --> father Is this a friend or family memember? --> pizza Invalid input. Program ending. BUILD SUCCESSFUL (total time: 47 seconds)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started