Purchase Solution

Java implementation of Person and ThreeFriends classes

Not what you're looking for?

Ask Custom Question

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

Purchase this Solution

Solution Summary

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

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 ...

Purchase this Solution


Free BrainMass Quizzes
Javscript Basics

Quiz on basics of javascript programming language.

C# variables and classes

This quiz contains questions about C# classes and variables.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.