- (Person.java) Write a definition of class, Person. a) Four private instance va
ID: 3662530 • Letter: #
Question
- (Person.java) Write a definition of class, Person. a) Four private instance variables of type int, String, String, and int, which respectively represent the id, first name, last name, and age of a person. b) A default constructor and a constructor that initializes each variable to an appropriate value. c) Get and set methods for setting the value of each type, and getting the value of each type. d) toString() method.
(PersonProgram.java) Write a client program to do the following.
Declare and create an array of 5 objects of the class Person.
Prompt the user to enter the id, first name, last name, and age of each person to populate the array.
Output all 5 array objects.
Calculate and output the average age.
Output the person’s information if he/she is younger than the average age.
Find the youngest person and output his/her information.
[Repetition structures are required to process the array.]
Explanation / Answer
This below jave code is written as per given in problem statement.
1. Created Person class with four data members
2. Created Person deault and parameterised constructor to intialise the data members of class
3. Created setters and geetters for Person class.
4. Created toString method..
5 In Main Method cretaed array of 5 persons objects and read the data
6. Calculated the average age of all persons and print the above average age all persons..
See the below code for :
import java.util.*;
class Person
{
// Define four attributes of a person class
private int age;
private int Id;
private String firstName;
private String lastName;
// Person Default Constructor with default values
public Person()
{
age = 0;
id = 0;
firstName = " ";
lastName = " ";
}
// Person Constructor with assign the values to class members
public Person(int age, int id, String firstName, String lastName)
{
this.age = age;
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
// Setter methods for Person Class
void setAge(int age)
{
this.age = age;
}
void setId(int id)
{
this.id = id;
}
void setFirstName(String firstName)
{
this.firstName = firstName;
}
void setLasrName(String lastName)
{
this.lastName = lastName;
}
// getter methods for Person Class
int getAge()
{
return age;
}
int getId()
{
return id;
}
String getFirstName()
{
return firstName;
}
String getLasrName()
{
return lastName;
}
// toString Method to display the Person class data
public String toString()
{
return age+" "+id+" "firstName+" "+lastName;
}
}
public class PersonProgram
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Person obj[] = new Person[5];
int i = 0;
// Reading the Person Details
System.out.println("Enter the Person class data Members");
for(i = 0; i<5; i++)
{
System.out.println("Enter your Age");
int age=sc.nextInt();
obj[i].setAge(age);
System.out.println("Enter your Id");
int id = sc.nextInt();
obj[i].setId(id);
System.out.println("Enter your FirstName");
double fName=sc.next();
obj[i].setFirstName(fName);
System.out.println("Enter your LastName");
double LName=sc.next();
obj[i].setFirstName(LName);
// Print the Each Person Details
System.out.println(obj[i]);
}
// Calculating the Avearge age of all 5 persons
double avg = 0.0;
int sum = 0;
for(i =0 ; i<5;i++)
{
sum = sum + obj[i].getAge();
}
avg = sum /5;
// Print the age of persons greater than the avg age of all persons
for(i=0;i<5;i++)
{
if(obj[i].getAge() > avg)
System.out.println(obj[i].getAge());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.