Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Person Class / * * * Person Class * Class contains: First name, Last name, and age * / public class Person { / /

Person Class
/**
* Person Class
* Class contains: First name, Last name, and age
*/
public class Person {
//Attributes/fields
private String firstName;
private String lastName;
private int personAge;
/**************************************
* Constructors
**************************************/
//Constructors (Default)
public Person()
{
firstName = "None";
lastName = "None";
personAge =0;
}
//Constructors (Populates data with arguments)
public Person(String pFirst, String pLast, int pAge)
{
setFirstName(pFirst);
setLastName(pLast);
setPersonAge(pAge);
}
/**************************************
* Getters/Setters
**************************************/
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public int getPersonAge(){
return personAge;
}
public void setPersonAge(int personAge){
this.personAge = personAge;
}
/**************************************
* Methods
**************************************/
public String PrintData()
{
String myReturn ="";
myReturn += "Name: "+ getFirstName()+""+ getLastName();
myReturn +=" Age: "+ getPersonAge();
return myReturn;
}
}// END Person Class
The provided Java class, Person, models a person and their attributes such as first name, last name, and age.
Attributes/fields: The class has three private instance variables or attributes - firstName, lastName, and personAge. They are private to ensure data encapsulation, which means they can be accessed or modified only through methods within the Person class itself.
Constructors: The class provides two constructors:
Default constructor (public Person()): This constructor initializes the firstName and lastName fields to "None" and the personAge to 0.
Parameterized constructor (public Person(String pFirst, String pLast, int pAge)): This constructor uses parameters to initialize the firstName, lastName, and personAge fields. It uses the provided setter methods to set the values.
Getters/Setters: These are methods that allow outside code to safely access (get) or modify (set) the private instance variables. This is a common practice in object-oriented programming to enforce encapsulation. In this class, there are getter and setter methods for each of the three private attributes.
Methods:
PrintData(): This method returns a String which includes the person's first name, last name, and age. It uses the getter methods to access the private instance variables.
In summary, this class represents a simple model of a person. It demonstrates several key principles of object-oriented programming, such as encapsulation (using private fields and public getter/setter methods), constructors to initialize objects, and methods to represent behaviors or actions associated with the object (in this case, printing the person's data).
Driver Program
/*
* Code writes a file and reads the data into an ArrayList of the Person object
*/
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File; // Import for File Object
import java.io.IOException; // Import for IO Exceptions
import java.io.FileWriter; // Import the FileWriter class
import java.io.FileNotFoundException; // Import this class to handle errors
public class LabDriver {
public static void main(String[] args){
//****************************************************************
//Variables we will use in the program
//****************************************************************
ArrayList Students = new ArrayList();
String firstName;
String lastName;
int age;
int index =0; //index for the ArrayList
//****************************************************************
//Lets create our Data File
//here we will use a try/catch in the event something goes wrong
//****************************************************************
try {
//Create an instance of the File Writer and give it the filename
FileWriter myWriter = new FileWriter("Person.txt");
//Write our data to the file
myWriter.write("Kevin Roark 29
"
+ "Sam Read 21
"
+ "Sally Smith 34
"
+ "Bart Simpson 14");
//close the file we opened - MUST BE DONE
myWriter.close();
//Message that all was done
System.out.println("Successfully wrote to the file.");
}
catch (IOException e)
{
//An issue happened - Message user
System.out.println("An error occurred.");
e.printStackTrace();
}
//****************************************************************
//Now lets read the file into our ArrayList of object Person
//********************************************
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions