Problem In your Person class Add a constant field to store t
Problem:
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.
As per the instructions, currentYear should be a constant. Also, the calculate methods should be private. You have fields to store the results of the calculations (age and timeincolg). Try using them rather than returning a value from the calculation methods.
When I run the program the age and years in system display 0\'s. As a suggestion, try calling the calculate methods in the toString() method before the return statement. This way, the values for age and timeincolg will be set. Then use those fields in the String rather than calling the methods.
*** This is what I have so far
48 class Persons_Information {
49
50 private String personName;
51
52 private String currentAdress;
53
54 private String permanentAdress;
55
56 private int idNumber;
57
58 private String birthDate;
59
60 private int personAge;
61
62 private int entryYear;
63
64 private int totalYears;
65
66 private int currentYear=2016;
67
68 private int age;
69
70 private int timeincolg;
71
72 public Persons_Information() {
73
74 personName = \"\";
75
76 currentAdress = \"\";
77
78 permanentAdress = \"\";
79
80 idNumber = 0;
81
82 birthDate = \"\";
83
84 personAge = 0;
85
86 entryYear = 0;
87
88 totalYears = 0;
89
90 age=0;
91
92 timeincolg=0;
93
94 }
95
96 public Persons_Information (int atIdNumber) {
97
98 idNumber = atIdNumber;
99
100 personName = \"\";
101
102 currentAdress = \"\";
103
104 permanentAdress = \"\";
105
106 birthDate = \"\";
107
108 personAge = 0;
109
110 entryYear = 0;
111
112 totalYears = 0;
113
114 age=0;
115
116 timeincolg=0;
117
118 }
119
120 //intput
121
122 public void setPersonName(String personName) {
123
124 this.personName = personName;
125
126 }
127
128 public void setCurrentAdress(String currentAdress) {
129
130 this.currentAdress = currentAdress;
131
132 }
133
134 public void setpermanentAdress(String permanentAdress) {
135
136 this.permanentAdress = permanentAdress;
137
138 }
139
140 public void setIdNumber(int id) {
141
142 idNumber = id;
143
144 }
145
146 public void setBirthDate(String birthDate) {
147
148 this.birthDate = birthDate;
149
150 }
151
152 public void setPersonAge(int pa) {
153
154 personAge = pa;
155
156 }
157
158 public void setEntryYear(int ey) {
159
160 entryYear = ey;
161
162 }
163
164 public void setTotalYears(int ty) {
165
166 totalYears = ty;
167
168 }
169
170 //output
171
172 public String getPersonName() {
173
174 return personName;
175
176 }
177
178 public String getCurrentAdress() {
179
180 return currentAdress;
181
182 }
183
184 public String getPermanentAdress() {
185
186 return permanentAdress;
187
188 }
189
190 public int getIdNumber() {
191
192 return idNumber;
193
194 }
195
196 public String getBirthDate() {
197
198 return birthDate;
199
200 }
201
202 public int getPersonAge() {
203
204 return personAge;
205
206 }
207
208 public int getEntryYear() {
209
210 return entryYear;
211
212 }
213
214 public int getTotalYears() {
215
216 return totalYears;
217
218 }
219
220 //toString
221
222 public int calculate_age() {
223
224 return currentYear-age;
225
226 }
227
228 public int calculate_colgyear() {
229
230 return currentYear-timeincolg;
231
232 }
233
234 public String toString()
235
236 {
237
238 return \"\ \ Name:\" + personName +
239
240 \"\ Current Adress:\" + currentAdress +
241
242 \"\ Permanent Adress:\" + permanentAdress +
243
244 \"\ ID Number:\" + idNumber +
245
246 \"\ Birth Date\" + birthDate +
247
248 \"\ Age:\" + personAge +
249
250 \"\ Entry Year:\" + entryYear +
251
252 \"\ Total Years in System:\" + totalYears+
253
254 \"\ Person age:\" + calculate_age()+
255
256 \"\ Total time in colg:\" + calculate_colgyear();
257
258 }
259
260 }
Solution
import java.util.*;
import java.lang.*;
import java.io.*;
class Person
{
private String name;
private String currentAddress;
private String permanentAddress;
private int idNumber;
private String DOB;
private static final int currentYear =2016;
private int yearEntered;
private int age;
private int yearsInCollege;
public Person() //default constructor
{
}
public Person(int id) //constructor with id
{
idNumber =id;
}
private int calculateAge()
{
int birthyear = Integer.parseInt(DOB.substring(6));
age = currentYear- birthyear; //calculate age of person
return age;
}
private int calculateYearsInCollege()
{
yearsInCollege= currentYear - yearEntered;
return yearsInCollege;
}
public void setName(String n) //get and set methods
{
name = n;
}
public String getName()
{
return name;
}
public void setCurrentAddress(String ca)
{
currentAddress = ca;
}
public String getCurrentAddress()
{
return currentAddress;
}
public void setPermanentAddress(String pa)
{
permanentAddress=pa;
}
public String getPermanentAddress()
{
return permanentAddress;
}
public void setIDNumber(int id)
{
idNumber =id;
}
public int getIDNumber()
{
return idNumber;
}
public void setDOB(String dob)
{
DOB = dob;
}
public String getDOB()
{
return DOB;
}
public int getCurrentYear()
{
return currentYear;
}
public void setYearEntered(int ye)
{
yearEntered = ye;
}
public int getYearEntered()
{
return yearEntered;
}
public String toString() //toString() method
{
return \"\ Name:\"+ name + \"\ Current Address :\"+ currentAddress +\"\ Permanent Address :\"+ permanentAddress +\"\ ID Number:\"+ idNumber +\"\ Date of Birth:\"+ DOB+\"\ Current Year :\"+ currentYear+\"\ Year Entered in College:\"+ yearEntered+\"\ The age of person = \"+calculateAge()+\"\ The person is in college for \"+calculateYearsInCollege()+\" years\";
}
}
class PersonTest
{
public static void main (String[] args) throws java.lang.Exception
{
Person person1 = new Person(); //person 1
Scanner stdin = new Scanner(System.in);
String name = stdin.next();
String ca= stdin.next();
String pa =stdin.next();
int id = stdin.nextInt();
String dob = stdin.next();
int cy = stdin.nextInt();
int ye =stdin.nextInt();
person1.setName(name);
person1.setCurrentAddress(ca);
person1.setPermanentAddress(pa);
person1.setIDNumber(id);
person1.setDOB(dob);
person1.setYearEntered(ye);
System.out.println(person1.getName());
System.out.println(person1.getCurrentAddress());
System.out.println(person1.getPermanentAddress());
System.out.println(person1.getIDNumber());
System.out.println(person1.getDOB());
System.out.println(person1.getCurrentYear());
System.out.println(person1.getYearEntered());
System.out.println(person1.toString());
}
}
output:






