In your Person class Change the visibility of all fields fro
In your Person class:
Change the visibility of all fields from private to protected.
Change the visibility of the methods to calculate age and time in college from private to protected.
Create a Student class:
Inherit from the Person class.
Add fields to store:
Students major.
Students GPA.
Do not add any other fields.
Create a default constructor (it should not have any parameters) for the Student class
Create mutator and accessor methods for the major and GPA fields.
Override the toString() method from the Person class:
Include all of the same information found in the Person class toString() method.
Make sure you call the methods to calculate the persons age in years and their time in college in years (Note that these methods are in the Person class but, because you inherit from the Person class, you will be able to call them from the Student class).
Add an opening message stating the person is a Student.
Add the Students major to the string.
Add the Students GPA 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.
In your test class:
Create a Student object.
Ask the user for all of the information for the student:
ID Number
Name
Current Address
Permanent Address
Year of Birth
Year entered college
Major
GPA
Call the toString() method to display all of the information.
*** This is what I have so far
48 import java.text.DateFormat;
49
50 import java.text.SimpleDateFormat;
51
52 import java.time.LocalDate;
53
54 import java.time.format.DateTimeFormatter;
55
56 class Persons_Information
57
58 {
59
60 private String personName;
61
62 private String currentAdress;
63
64 private String permanentAdress;
65
66 private int idNumber;
67
68 private String birthDate;
69
70 private int personAge;
71
72 private int entryYear;
73
74 private int totalYears;
75
76 final int currentYear=2016;
77
78 public Persons_Information() {
79
80 personName = \"\";
81
82 currentAdress = \"\";
83
84 permanentAdress = \"\";
85
86 idNumber = 0;
87
88 birthDate = \"\";
89
90 personAge = 0;
91
92 entryYear = 0;
93
94 totalYears = 0;
95
96 }
97
98 public Persons_Information (int atIdNumber) {
99
100 idNumber = atIdNumber;
101
102 personName = \"\";
103
104 currentAdress = \"\";
105
106 permanentAdress = \"\";
107
108 birthDate = \"\";
109
110 personAge = 0;
111
112 entryYear = 0;
113
114 totalYears = 0;
115
116 }
117
118 //intput
119
120 public void setPersonName(String personName) {
121
122 this.personName = personName;
123
124 }
125
126 public void setCurrentAdress(String currentAdress) {
127
128 this.currentAdress = currentAdress;
129
130 }
131
132 public void setpermanentAdress(String permanentAdress) {
133
134 this.permanentAdress = permanentAdress;
135
136 }
137
138 public void setIdNumber(int id) {
139
140 idNumber = id;
141
142 }
143
144 public void setBirthDate(String birthDate) {
145
146 this.birthDate = birthDate;
147
148 }
149
150 public void setPersonAge(int pa) {
151
152 //CALCULATE THE CURRENT YEAR, MONTH AND DAY
153
154 //INTO SEPERATE VARIABLES
155
156 personAge = pa;
157
158 }
159
160 public void setEntryYear(int ey) {
161
162 entryYear = ey;
163
164 }
165
166 public void setTotalYears(int ty) {
167
168 totalYears = ty;
169
170 }
171
172 //output
173
174 public String getPersonName() {
175
176 return personName;
177
178 }
179
180 public String getCurrentAdress() {
181
182 return currentAdress;
183
184 }
185
186 public String getPermanentAdress() {
187
188 return permanentAdress;
189
190 }
191
192 public int getIdNumber() {
193
194 return idNumber;
195
196 }
197
198 public String getBirthDate() {
199
200 return birthDate;
201
202 }
203
204 public int getPersonAge() {
205
206 return personAge;
207
208 }
209
210 public int getEntryYear() {
211
212 return entryYear;
213
214 }
215
216 public int getTotalYears() {
217
218 return totalYears;
219
220 }
221
222 //Method To Calculate Age of a person
223
224 private int calculatePersonAge() {
225
226 //CALCULATE THE CURRENT YEAR, MONTH AND DAY
227
228 //INTO SEPERATE VARIABLES
229
230 int yearDOB = Integer.parseInt(birthDate.substring(0, 4));
231
232 DateFormat dateFormat = new SimpleDateFormat(\"YYYY\");
233
234 java.util.Date date = new java.util.Date();
235
236 int thisYear = Integer.parseInt(dateFormat.format(date));
237
238 dateFormat = new SimpleDateFormat(\"MM\");
239
240 date = new java.util.Date();
241
242 int thisMonth = Integer.parseInt(dateFormat.format(date));
243
244 dateFormat = new SimpleDateFormat(\"DD\");
245
246 date = new java.util.Date();
247
248 int thisDay = Integer.parseInt(dateFormat.format(date));
249
250 //CREATE AN AGE VARIABLE TO HOLD THE CALCULATED AGE
251
252 //TO START WILL – SET THE AGE EQUEL TO THE CURRENT YEAR MINUS THE YEAR
253
254 //OF THE DOB
255
256 int age = thisYear-yearDOB;
257
258 return age;
259
260 }
261
262 //Method To Calculate Duration of person in college
263
264 private int durationInCollege() {
265
266 //CALCULATE THE CURRENT YEAR
267
268 //INTO SEPERATE VARIABLES
269
270 DateFormat dateFormat = new SimpleDateFormat(\"YYYY\");
271
272 java.util.Date date = new java.util.Date();
273
274 int thisYear = Integer.parseInt(dateFormat.format(date));
275
276 //CREATE AN AGE VARIABLE TO HOLD THE CALCULATED AGE
277
278 //To Calculate Duration we will subtract current year from entry year
279
280 int totalYears = thisYear-entryYear;
281
282 return totalYears;
283
284 }
285
286 //toString
287
288 public String toString()
289
290 {
291
292 return \"\ \ Name:\" + personName +
293
294 \"\ Current Adress:\" + currentAdress +
295
296 \"\ Permanent Adress:\" + permanentAdress +
297
298 \"\ ID Number:\" + idNumber +
299
300 \"\ Birth Date\" + birthDate +
301
302 \"\ Age:\" + calculatePersonAge() +
303
304 \"\ Entry Year:\" + entryYear +
305
306 \"\ Total Years in System:\" + durationInCollege();
307
308 }
309
310 }
*** 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
Please find below the modified full code along with the output :
-----------------------------------------------------------
OUTPUT:
Enter Student Name: John Abraham
You entered: John Abraham
Enter Current Address: Mumbai
You entered: Mumbai
Enter Permanent Address: Delhi
You entered: Delhi
Enter ID number: 765
You entered: 765
Enter Birth Date: 1985-05-10
You entered: 1985-05-10
Enter Entry Year: 2000
You entered: 2000
Enter Student major: Computers
You entered: Computers
Enter Student GPA: 9.8
You entered: 9.8
Person 1:
The person is a Student.
Name:John Abraham
Current Adress:Mumbai
Permanent Adress:Delhi
ID Number:765
Birth Date1985-05-10
Age:31
Entry Year:2000
Total Years in System:16
Major: Computers
GPA: 9.8







