Explore BrainMass
Share

Explore BrainMass

    Java implementation of Person and ThreeFriends classes

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Create two classes, a main class and an object class. The object class will be called Person and will collect data about individual people. The main class will be called ThreeFriends and will instantiate three Person objects to represent three people who are friends and loading data into these objects. The user will enter the data that needs to be loaded into each object. Once all the objects have been loaded with data the program will print out the information about the three people in order of oldest to youngest person by extracting it from the objects.

    The Person class must have the following characteristics:

    Data fields:
    firstName (String) - The person's first name
    middleInitial (String) - The person's middle initial
    lastName (String) - The person's last name
    age (int) - The person's age

    Methods:
    Person( String fName, String mInitial, String lName ) - A constructor that loads the name data into the new object.

    String getFullName() - A method that puts the three components of the name (last name, first name, middle initial) together into the form "Last, First I." and returns it as a string.

    Setters and getters to set and get all of the data fields. You will not use all of them in this program but you implement them all. The setAge() setter must perform a sanity check on the age that is being set. If it is negative or greater than 110 it will set the age to -1. The special "age" of -1 means that an invalid age was entered. You will not display any message when the setter detects this. To generate the final output you will detect the age of -1 and print "INVALID" instead of the negative number in your output as shown in the sample run below.

    When ordering the final outputs you can treat the -1 invalid age indicators just like any other age so they will come last since -1 is less than any valid age. If there are two ages that are the same the first person entered should be the first person listed in the group of same ages.

    The following is a sample run of the program. Your program should exactly duplicate this sample I/O format and should work correctly for any valid input. User input is shown in blue bold text:

    Enter the data for the three friends below.

    First friend
    First name: George
    Middle initial: M
    Last name: Jones
    Age: 25

    Second friend
    First name: Mary
    Middle initial: L
    Last name: Smith
    Age: 28

    Third friend
    First name: Omar
    Middle initial: J
    Last name: Backus
    Age: 74

    The three friends from oldest to youngest are:
    Backus, Omar J. Age: 74
    Smith, Mary L. Age: 28
    Jones, George M. Age: 25

    Here is a second run that illustrates the response to an invalid age:
    Enter the data for the three friends below.

    First friend
    First name: Mary
    Middle initial: L
    Last name: Smith
    Age: -5

    Second friend
    First name: Omar
    Middle initial: J
    Last name: Backus
    Age: 134

    Third friend
    First name: George
    Middle initial: M
    Last name: Jones
    Age: 25

    The three friends from oldest to youngest are:
    Jones, George M. Age: 25
    Smith, Mary L. Age: INVALID
    Backus, Omar J. Age: INVALID

    © BrainMass Inc. brainmass.com October 9, 2019, 11:39 pm ad1c9bdddf
    https://brainmass.com/computer-science/java/java-implementation-person-three-friends-classes-263341

    Attachments

    Solution Preview

    This solution requires creating two Java classes. The Person class contains data members for each of the attributes of a person as specified in the posting. Setters and getters are ...

    Solution Summary

    This solution implements the Person and ThreeFriends classes. It provides an explanation of how the program works.

    $2.19