Add the following methods to the Person class: Update the given PersonClient.java file as specified in the to do comment

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Add the following methods to the Person class: Update the given PersonClient.java file as specified in the to do comment

Post by answerhappygod »

Add the following methods to the Person class:
Update the given PersonClient.java file as specified in the todo comments. This class is being used to test the methods in yourPerson class.
import java.util.Scanner;
/*** Client code for Person class** @author* @author*/public class PersonClient {/** Maximum number of people */public static final int MAX_PEOPLE = 5;/*** Creates Person objects and then prints their contents** @param args command line arguments (not used)*/public static void main(String[] args) {Scanner console = new Scanner(System.in);Person[] people = getPeople(console);System.out.println();for (Person person : people) {System.out.println(person.getFullName() + " is " + person.getAge()+ " years old.");}
for(int i = 0; i < people.length; i++) {//TODO: all the people in the array had a birthday today!// call a method on each element to increase the person'sage.}for (Person person : people) {System.out.println(person.getReverseName() + " is now " +person.getAge() + " years old.");}}/*** Sets up Array of Person objects** @param console Scanner for console* @return Array of Person objects with input from console*/public static Person[] getPeople(Scanner console) {Person[] people = new Person[MAX_PEOPLE];for (int i = 0; i < people.length; i++) {//TODO: complete this for loop to prompt the user for// the first name, middle initial, last name, and age of 5poeple.// Use separate prompts for each value. You can assume user willenter an integer for age,// and a single character for middle initial// Create and a Person object, assign it to an array element andset its fields//Hint: people = new Person(); then call the setters on thepeople element}return people;}
}
public class Person {
private String first_name, last_name;
private char middle_initial;
private int age;
public String getFirstName()
{
return first_name;
}
public String getLastName()
{
return last_name;
}
public char getMiddleInitial()
{
return middle_initial;
}
public int getAge()
{
return age;
}
public void setFirstName(String fn)
{
first_name = fn;
}
public void setLastName(String ln)
{
last_name = ln;
}
public void setMiddleInitial(char mi)
{
middle_initial=mi;
}
public void setAge(int age)
{
this.age = age;
}
public void birthday()
{
age++;
}
public String getFullName()
{
String str = String.format("%s %c.%s",first_name,middle_initial,last_name);
return str;
}
public String getReverseName()
{
String str = String.format("%s, %s%c.", last_name,first_name,middle_initial);
return str;
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply