In your Person class Add a constant field to store the curre
In your Person class:
Add a constant field to store the current year (review the Week 4 Lesson if you aren\'t sure how to create a constant) :
Assign a value to the current year field when it is declared (do not add it to the constructors).
Add fields to store a person\'s age in years and how many years they have been with the college:
Do not add mutator or accessor methods for these fields.
Set the visibility modifier to private for these fields.
Modify both constructors to set default values for both fields.
Add processing actions:
Add a method to calculate a person\'s age in years only.
Add a method to calculate how long a person has been in college in years only.
Make both methods private.
Do not take any input or display any output in these methods.
Modify the toString() method:
Call the method to calculate the person\'s age.
Call the method to calculate how long the person has been in college.
Add the person\'s age to the String.
Add the person\'s time in college to the String.
Add output messages for each field so that the user knows what they are looking at.
Format the string using the new line escape sequence \ so that each field and it\'s message is on a separate line.
*** This is what I have so far
48 class Persons_Information
49 {
50 private String personName;
51 private String currentAdress;
52 private String permanentAdress;
53 private int idNumber;
54 private String birthDate;
55 private int personAge;
56 private int entryYear;
57 private int totalYears;
58
59 public Persons_Information() {
60 personName = \"\";
61 currentAdress = \"\";
62 permanentAdress = \"\";
63 idNumber = 0;
64 birthDate = \"\";
65 personAge = 0;
66 entryYear = 0;
67 totalYears = 0;
68 }
69
70 public Persons_Information (int atIdNumber) {
71 idNumber = atIdNumber;
72 personName = \"\";
73 currentAdress = \"\";
74 permanentAdress = \"\";
75 birthDate = \"\";
76 personAge = 0;
77 entryYear = 0;
78 totalYears = 0;
79 }
80
81 //intput
82
83 public void setPersonName(String personName) {
84 this.personName = personName;
85 }
86
87 public void setCurrentAdress(String currentAdress) {
88 this.currentAdress = currentAdress;
89 }
90
91
92 public void setpermanentAdress(String permanentAdress) {
93 this.permanentAdress = permanentAdress;
94 }
95
96 public void setIdNumber(int id) {
97 idNumber = id;
98 }
99
100 public void setBirthDate(String birthDate) {
101 this.birthDate = birthDate;
102 }
103
104 public void setPersonAge(int pa) {
105 personAge = pa;
106 }
107
108 public void setEntryYear(int ey) {
109 entryYear = ey;
110 }
111
112 public void setTotalYears(int ty) {
113 totalYears = ty;
114 }
115
116 //output
117
118 public String getPersonName() {
119 return personName;
120 }
121
122 public String getCurrentAdress() {
123 return currentAdress;
124 }
125
126 public String getPermanentAdress() {
127 return permanentAdress;
128 }
129
130 public int getIdNumber() {
131 return idNumber;
132 }
133 public String getBirthDate() {
134 return birthDate;
135 }
136
137 public int getPersonAge() {
138 return personAge;
139 }
140
141 public int getEntryYear() {
142 return entryYear;
143 }
144
145 public int getTotalYears() {
146 return totalYears;
147 }
148
149 //toString
150
151 public String toString()
152 {
153 return \"\ \ Name:\" + personName +
154 \"\ Current Adress:\" + currentAdress +
155 \"\ Permanent Adress:\" + permanentAdress +
156 \"\ ID Number:\" + idNumber +
157 \"\ Birth Date\" + birthDate +
158 \"\ Age:\" + personAge +
159 \"\ Entry Year:\" + entryYear +
160 \"\ Total Years in System:\" + totalYears;
161 }
162 }
163
*** This is my test class
7 import java.util.Scanner;
8
9 class PersonTest
10 {
11 //Main method
12 public static void main(String args[])
13 {
14 Scanner sc = new Scanner(System.in);
15
16 //Creating object
17 Persons_Information person1 = new Persons_Information();
18 //Reading and updating values
19 System.out.print(\"\ Enter Person Name: \");
20 person1.setPersonName(sc.nextLine());
21 System.out.println(\"You entered: \" + person1.getPersonName());
22
23 System.out.print(\"\ Enter Current Address: \");
24 person1.setCurrentAdress(sc.nextLine());
25 System.out.println(\"You entered: \" + person1.getCurrentAdress());
26
27 System.out.print(\"\ Enter Permanent Address: \");
28 person1.setpermanentAdress(sc.nextLine());
29 System.out.println(\"You entered: \" + person1.getPermanentAdress());
30
31 System.out.print(\"\ Enter ID number: \");
32 person1.setIdNumber(sc.nextInt());
33 System.out.println(\"You entered: \" + person1.getIdNumber());
34
35 sc.nextLine();
36
37 System.out.print(\"\ Enter Birth Date: \");
38 person1.setBirthDate(sc.nextLine());
39 System.out.println(\"You entered: \" + person1.getBirthDate());
40
41 System.out.print(\"\ Enter Entry Year: \");
42 person1.setEntryYear(sc.nextInt());
43 System.out.println(\"You entered: \" + person1.getEntryYear());
44
45 //Printing person 1 details
46 System.out.println(\"\ Person 1: \ \" + person1.toString());
47
48 System.out.println(\"______Creating the Person#2 Object______\");
49
50 System.out.print(\"\ Enter ID number: \");
51 int id=sc.nextInt();
52 Persons_Information pi2=new Persons_Information(id);
53 System.out.println(\"You Entered Person Id :\"+pi2.getIdNumber());
54
55 sc.nextLine();
56
57 //Reading and updating values
58 System.out.print(\"\ Enter Person Name: \");
59 pi2.setPersonName(sc.nextLine());
60 System.out.println(\"You entered: \" + pi2.getPersonName());
61
62 System.out.print(\"\ Enter Current Address: \");
63 pi2.setCurrentAdress(sc.nextLine());
64 System.out.println(\"You entered: \" + pi2.getCurrentAdress());
65
66 System.out.print(\"\ Enter Permanent Address: \");
67 pi2.setpermanentAdress(sc.nextLine());
68 System.out.println(\"You entered: \" + pi2.getPermanentAdress());
69
70
71 System.out.print(\"\ Enter Birth Date: \");
72 pi2.setBirthDate(sc.nextLine());
73 System.out.println(\"You entered: \" + pi2.getBirthDate());
74
75 System.out.print(\"\ Enter Entry Year: \");
76 pi2.setEntryYear(sc.nextInt());
77 System.out.println(\"You entered: \" + pi2.getEntryYear());
78
79 //Printing person 2 details
80 System.out.println(\"\ Person 1: \ \" + pi2.toString());
81 }
82 }
Solution
//PersonTest.java
import java.util.Scanner;
class PersonTest
{
//Main method
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//Creating object
Persons_Information person1 = new Persons_Information();
//Reading and updating values
System.out.print(\"\ Enter Person Name: \");
person1.setPersonName(sc.nextLine());
System.out.println(\"You entered: \" + person1.getPersonName());
System.out.print(\"\ Enter Current Address: \");
person1.setCurrentAdress(sc.nextLine());
System.out.println(\"You entered: \" + person1.getCurrentAdress());
System.out.print(\"\ Enter Permanent Address: \");
person1.setpermanentAdress(sc.nextLine());
System.out.println(\"You entered: \" + person1.getPermanentAdress());
System.out.print(\"\ Enter ID number: \");
person1.setIdNumber(sc.nextInt());
System.out.println(\"You entered: \" + person1.getIdNumber());
sc.nextLine();
System.out.print(\"\ Enter Birth Date(format::YYYY-MM-DD): \");
person1.setBirthDate(sc.nextLine());
System.out.println(\"You entered: \" + person1.getBirthDate());
System.out.print(\"\ Enter Entry Year: \");
person1.setEntryYear(sc.nextInt());
System.out.println(\"You entered: \" + person1.getEntryYear());
//Printing person 1 details
System.out.println(\"\ Person 1: \ \" + person1.toString());
System.out.println(\"______Creating the Person#2 Object______\");
System.out.print(\"\ Enter ID number: \");
int id=sc.nextInt();
Persons_Information pi2=new Persons_Information(id);
System.out.println(\"You Entered Person Id :\"+pi2.getIdNumber());
sc.nextLine();
//Reading and updating values
System.out.print(\"\ Enter Person Name: \");
pi2.setPersonName(sc.nextLine());
System.out.println(\"You entered: \" + pi2.getPersonName());
System.out.print(\"\ Enter Current Address: \");
pi2.setCurrentAdress(sc.nextLine());
System.out.println(\"You entered: \" + pi2.getCurrentAdress());
System.out.print(\"\ Enter Permanent Address: \");
pi2.setpermanentAdress(sc.nextLine());
System.out.println(\"You entered: \" + pi2.getPermanentAdress());
System.out.print(\"\ Enter Birth Date: \");
pi2.setBirthDate(sc.nextLine());
System.out.println(\"You entered: \" + pi2.getBirthDate());
System.out.print(\"\ Enter Entry Year: \");
pi2.setEntryYear(sc.nextInt());
System.out.println(\"You entered: \" + pi2.getEntryYear());
//Printing person 2 details
System.out.println(\"\ Person 1: \ \" + pi2.toString());
}
}
//PersonInformation.java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class Persons_Information
{
private String personName;
private String currentAdress;
private String permanentAdress;
private int idNumber;
private String birthDate;
private int personAge;
private int entryYear;
private int totalYears;
final int currentYear=2016;
public Persons_Information() {
personName = \"\";
currentAdress = \"\";
permanentAdress = \"\";
idNumber = 0;
birthDate = \"\";
personAge = 0;
entryYear = 0;
totalYears = 0;
}
public Persons_Information (int atIdNumber) {
idNumber = atIdNumber;
personName = \"\";
currentAdress = \"\";
permanentAdress = \"\";
birthDate = \"\";
personAge = 0;
entryYear = 0;
totalYears = 0;
}
//intput
public void setPersonName(String personName) {
this.personName = personName;
}
public void setCurrentAdress(String currentAdress) {
this.currentAdress = currentAdress;
}
public void setpermanentAdress(String permanentAdress) {
this.permanentAdress = permanentAdress;
}
public void setIdNumber(int id) {
idNumber = id;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public void setPersonAge(int pa) {
//CALCULATE THE CURRENT YEAR, MONTH AND DAY
//INTO SEPERATE VARIABLES
personAge = pa;
}
public void setEntryYear(int ey) {
entryYear = ey;
}
public void setTotalYears(int ty) {
totalYears = ty;
}
//output
public String getPersonName() {
return personName;
}
public String getCurrentAdress() {
return currentAdress;
}
public String getPermanentAdress() {
return permanentAdress;
}
public int getIdNumber() {
return idNumber;
}
public String getBirthDate() {
return birthDate;
}
public int getPersonAge() {
return personAge;
}
public int getEntryYear() {
return entryYear;
}
public int getTotalYears() {
return totalYears;
}
//Method To Calculate Age of a person
private int calculatePersonAge() {
//CALCULATE THE CURRENT YEAR, MONTH AND DAY
//INTO SEPERATE VARIABLES
int yearDOB = Integer.parseInt(birthDate.substring(0, 4));
DateFormat dateFormat = new SimpleDateFormat(\"YYYY\");
java.util.Date date = new java.util.Date();
int thisYear = Integer.parseInt(dateFormat.format(date));
dateFormat = new SimpleDateFormat(\"MM\");
date = new java.util.Date();
int thisMonth = Integer.parseInt(dateFormat.format(date));
dateFormat = new SimpleDateFormat(\"DD\");
date = new java.util.Date();
int thisDay = Integer.parseInt(dateFormat.format(date));
//CREATE AN AGE VARIABLE TO HOLD THE CALCULATED AGE
//TO START WILL – SET THE AGE EQUEL TO THE CURRENT YEAR MINUS THE YEAR
//OF THE DOB
int age = thisYear-yearDOB;
return age;
}
//Method To Calculate Duration of person in college
private int durationInCollege() {
//CALCULATE THE CURRENT YEAR
//INTO SEPERATE VARIABLES
DateFormat dateFormat = new SimpleDateFormat(\"YYYY\");
java.util.Date date = new java.util.Date();
int thisYear = Integer.parseInt(dateFormat.format(date));
//CREATE AN AGE VARIABLE TO HOLD THE CALCULATED AGE
//To Calculate Duration we will subtract current year from entry year
int totalYears = thisYear-entryYear;
return totalYears;
}
//toString
public String toString()
{
return \"\ \ Name:\" + personName +
\"\ Current Adress:\" + currentAdress +
\"\ Permanent Adress:\" + permanentAdress +
\"\ ID Number:\" + idNumber +
\"\ Birth Date\" + birthDate +
\"\ Age:\" + calculatePersonAge() +
\"\ Entry Year:\" + entryYear +
\"\ Total Years in System:\" + durationInCollege();
}
}









