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
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class Persons_Information
{
protected String personName;
protected String currentAdress;
protected String permanentAdress;
protected int idNumber;
protected String birthDate;
protected int personAge;
protected int entryYear;
protected 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
protected 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
protected 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();
}
}
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: ");
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());
}
}
public class Student extends Persons_Information {
private String major;
private double GPA;
public Student() {
// TODO Auto-generated constructor stub
super();
}
/**
* @return the major
*/
public String getMajor() {
return major;
}
/**
* @param major
* the major to set
*/
public void setMajor(String major) {
this.major = major;
}
/**
* @return the gPA
*/
public double getGPA() {
return GPA;
}
/**
* @param gPA
* the gPA to set
*/
public void setGPA(double gPA) {
GPA = gPA;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "person is a Student  " + super.toString() + " Major:"
+ getMajor() +
" GPA " + getGPA();
}
}
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student student = new Student();
System.out.print(" Enter Person Name: ");
student.setPersonName(sc.nextLine());
System.out.println("You entered: " + student.getPersonName());
System.out.print(" Enter Current Address: ");
student.setCurrentAdress(sc.nextLine());
System.out.println("You entered: " + student.getCurrentAdress());
System.out.print(" Enter Permanent Address: ");
student.setpermanentAdress(sc.nextLine());
System.out.println("You entered: " + student.getPermanentAdress());
System.out.print(" Enter ID number: ");
student.setIdNumber(sc.nextInt());
System.out.println("You entered: " + student.getIdNumber());
sc.nextLine();
System.out.print(" Enter Birth Date: ");
student.setBirthDate(sc.nextLine());
System.out.println("You entered: " + student.getBirthDate());
System.out.print(" Enter Entry Year: ");
student.setEntryYear(sc.nextInt());
System.out.println("You entered: " + student.getEntryYear());
System.out.print(" Enter GPA: ");
student.setGPA(sc.nextDouble());
System.out.println("You entered: " + student.getGPA());
System.out.print(" Enter Major: ");
student.setMajor(sc.next());
System.out.println("You entered: " + student.getMajor());
// Printing student details
System.out.println(" Person 1:  " + student.toString());
}
}
OUTPUT:
Enter Person Name: Srinivas
You entered: Srinivas
Enter Current Address: Visakhapatnam
You entered: Visakhapatnam
Enter Permanent Address: Visakhapatnam
You entered: Visakhapatnam
Enter ID number: 22554
You entered: 22554
Enter Birth Date: 1966
You entered: 1966
Enter Entry Year: 2016
You entered: 2016
Enter GPA: 3.2
You entered: 3.2
Enter Major: IT
You entered: IT
Person 1:
person is a Student
Name:Srinivas
Current Adress:Visakhapatnam
Permanent Adress:Visakhapatnam
ID Number:22554
Birth Date1966
Age:50
Entry Year:2016
Total Years in System:0
Major:IT
GPA 3.2

More Related Content

PDF
In your Person classAdd a constant field to store the current yea.pdf
PDF
I need help making these adjustments to my class Persons_Informati.pdf
PDF
So Far I have these two classes but I need help with my persontest c.pdf
PDF
please help with java questionsJAVA CODEplease check my code and.pdf
PDF
Can someone help me with this code When I run it, it stops after th.pdf
PDF
Consider the classes below- Here is the code for the ParentChildRelati.pdf
PPTX
Java script
PDF
Overview You are tasked with writing a program called Social Security.pdf
In your Person classAdd a constant field to store the current yea.pdf
I need help making these adjustments to my class Persons_Informati.pdf
So Far I have these two classes but I need help with my persontest c.pdf
please help with java questionsJAVA CODEplease check my code and.pdf
Can someone help me with this code When I run it, it stops after th.pdf
Consider the classes below- Here is the code for the ParentChildRelati.pdf
Java script
Overview You are tasked with writing a program called Social Security.pdf

Similar to In your Person classChange the visibility of all fields from priv.pdf (20)

PDF
define a class name Employee whose objects are records for employee..pdf
PDF
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
PDF
C programs Set 3
PPT
Csphtp1 07
PDF
#include iostream #include fstream #include vector #incl.pdf
PDF
Java questionI am having issues returning the score sort in numeri.pdf
PPT
FP 201 - Unit4 Part 2
PPTX
Structure & union
PDF
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
PDF
PDF
I Have the following Java program in which converts Date to Words an.pdf
PDF
Polygon.javapublic class Polygon { private int numSides; priva.pdf
PDF
Polygon.javapublic class Polygon { private int numSides; priva.pdf
PDF
OOP_Activity 1_ An Activty about code.pdf
PPTX
Howard type script
PPTX
TypeScript by Howard
PDF
Sql server query collection
DOCX
Student DATABASE MANAGeMEnT SysTEm
PDF
package hw1-public class Runner { public static void main(String-- arg (1).pdf
PDF
the code is as the following- package hw1-public class Runner { public.pdf
define a class name Employee whose objects are records for employee..pdf
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
C programs Set 3
Csphtp1 07
#include iostream #include fstream #include vector #incl.pdf
Java questionI am having issues returning the score sort in numeri.pdf
FP 201 - Unit4 Part 2
Structure & union
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
I Have the following Java program in which converts Date to Words an.pdf
Polygon.javapublic class Polygon { private int numSides; priva.pdf
Polygon.javapublic class Polygon { private int numSides; priva.pdf
OOP_Activity 1_ An Activty about code.pdf
Howard type script
TypeScript by Howard
Sql server query collection
Student DATABASE MANAGeMEnT SysTEm
package hw1-public class Runner { public static void main(String-- arg (1).pdf
the code is as the following- package hw1-public class Runner { public.pdf
Ad

More from aristogifts99 (20)

PDF
Let A be the set of all n x n matrices with det 1. Is A closed under.pdf
PDF
is not one of the stages of a project life cycle 6. Which of the fol.pdf
PDF
In your opinion what are the biggest threats to a network in terms of.pdf
PDF
From the trial balance of Howards Cleaners, prepare the following f.pdf
PDF
How would you help Black become an effective supervisorSolution.pdf
PDF
How do bacteria package their DNA by supercoiling the DNA by using.pdf
PDF
How did the company Facebook get its start and how has it changed .pdf
PDF
For each of the lymphatic and immune systems explain how one (1) com.pdf
PDF
Find the monthly payment, R, needed to have a sinking fund accumulat.pdf
PDF
Explain whats going on in this state diagram.Solution When you.pdf
PDF
Exercise 3- Understanding balance sheet accounts Below are a few acco.pdf
PDF
Explain the cause of incompatible blood transfusion.SolutionThe.pdf
PDF
Epithelial Tissues Using no more than 12 propositions, name, classify.pdf
PDF
Describe Milgram’s controversial research on obedience, and discuss .pdf
PDF
Can two people with lobed ears have a child with attracted ears if t.pdf
PDF
Can Bacteria carry out phagocytosis ExplainSolutionBacteria .pdf
PDF
About half of the paid lobbyists in Washington are former government.pdf
PDF
Compare the development of two imperial powers. What factors inspire.pdf
PDF
An industry is likely to have a low beta if theSolutionAn.pdf
PDF
An early attempt to understand how the packing of DNA into chromatin.pdf
Let A be the set of all n x n matrices with det 1. Is A closed under.pdf
is not one of the stages of a project life cycle 6. Which of the fol.pdf
In your opinion what are the biggest threats to a network in terms of.pdf
From the trial balance of Howards Cleaners, prepare the following f.pdf
How would you help Black become an effective supervisorSolution.pdf
How do bacteria package their DNA by supercoiling the DNA by using.pdf
How did the company Facebook get its start and how has it changed .pdf
For each of the lymphatic and immune systems explain how one (1) com.pdf
Find the monthly payment, R, needed to have a sinking fund accumulat.pdf
Explain whats going on in this state diagram.Solution When you.pdf
Exercise 3- Understanding balance sheet accounts Below are a few acco.pdf
Explain the cause of incompatible blood transfusion.SolutionThe.pdf
Epithelial Tissues Using no more than 12 propositions, name, classify.pdf
Describe Milgram’s controversial research on obedience, and discuss .pdf
Can two people with lobed ears have a child with attracted ears if t.pdf
Can Bacteria carry out phagocytosis ExplainSolutionBacteria .pdf
About half of the paid lobbyists in Washington are former government.pdf
Compare the development of two imperial powers. What factors inspire.pdf
An industry is likely to have a low beta if theSolutionAn.pdf
An early attempt to understand how the packing of DNA into chromatin.pdf
Ad

Recently uploaded (20)

PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
International_Financial_Reporting_Standa.pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
IGGE1 Understanding the Self1234567891011
PPTX
Virtual and Augmented Reality in Current Scenario
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
My India Quiz Book_20210205121199924.pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
LDMMIA Reiki Yoga Finals Review Spring Summer
202450812 BayCHI UCSC-SV 20250812 v17.pptx
International_Financial_Reporting_Standa.pdf
B.Sc. DS Unit 2 Software Engineering.pptx
Cambridge-Practice-Tests-for-IELTS-12.docx
Chinmaya Tiranga quiz Grand Finale.pdf
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
IGGE1 Understanding the Self1234567891011
Virtual and Augmented Reality in Current Scenario
Unit 4 Computer Architecture Multicore Processor.pptx
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Hazard Identification & Risk Assessment .pdf
Computer Architecture Input Output Memory.pptx
Uderstanding digital marketing and marketing stratergie for engaging the digi...
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
My India Quiz Book_20210205121199924.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
FORM 1 BIOLOGY MIND MAPS and their schemes

In your Person classChange the visibility of all fields from priv.pdf

  • 1. 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.
  • 2. *** 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 = "";
  • 3. 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
  • 4. 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
  • 5. 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 }
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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());
  • 10. 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());
  • 11. 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 import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.format.DateTimeFormatter; class Persons_Information { protected String personName; protected String currentAdress; protected String permanentAdress; protected int idNumber; protected String birthDate; protected int personAge; protected int entryYear; protected int totalYears; final int currentYear = 2016;
  • 12. 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; }
  • 13. 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; }
  • 14. // Method To Calculate Age of a person protected 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 protected 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() {
  • 15. 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(); } } 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: "); person1.setBirthDate(sc.nextLine()); System.out.println("You entered: " + person1.getBirthDate()); System.out.print(" Enter Entry Year: "); person1.setEntryYear(sc.nextInt());
  • 16. 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()); } } public class Student extends Persons_Information { private String major; private double GPA; public Student() { // TODO Auto-generated constructor stub super(); }
  • 17. /** * @return the major */ public String getMajor() { return major; } /** * @param major * the major to set */ public void setMajor(String major) { this.major = major; } /** * @return the gPA */ public double getGPA() { return GPA; } /** * @param gPA * the gPA to set */ public void setGPA(double gPA) { GPA = gPA; } @Override public String toString() { // TODO Auto-generated method stub return "person is a Student " + super.toString() + " Major:" + getMajor() + " GPA " + getGPA(); } } import java.util.Scanner;
  • 18. public class StudentTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Student student = new Student(); System.out.print(" Enter Person Name: "); student.setPersonName(sc.nextLine()); System.out.println("You entered: " + student.getPersonName()); System.out.print(" Enter Current Address: "); student.setCurrentAdress(sc.nextLine()); System.out.println("You entered: " + student.getCurrentAdress()); System.out.print(" Enter Permanent Address: "); student.setpermanentAdress(sc.nextLine()); System.out.println("You entered: " + student.getPermanentAdress()); System.out.print(" Enter ID number: "); student.setIdNumber(sc.nextInt()); System.out.println("You entered: " + student.getIdNumber()); sc.nextLine(); System.out.print(" Enter Birth Date: "); student.setBirthDate(sc.nextLine()); System.out.println("You entered: " + student.getBirthDate()); System.out.print(" Enter Entry Year: "); student.setEntryYear(sc.nextInt()); System.out.println("You entered: " + student.getEntryYear()); System.out.print(" Enter GPA: "); student.setGPA(sc.nextDouble()); System.out.println("You entered: " + student.getGPA()); System.out.print(" Enter Major: "); student.setMajor(sc.next()); System.out.println("You entered: " + student.getMajor()); // Printing student details System.out.println(" Person 1: " + student.toString()); } } OUTPUT: Enter Person Name: Srinivas You entered: Srinivas
  • 19. Enter Current Address: Visakhapatnam You entered: Visakhapatnam Enter Permanent Address: Visakhapatnam You entered: Visakhapatnam Enter ID number: 22554 You entered: 22554 Enter Birth Date: 1966 You entered: 1966 Enter Entry Year: 2016 You entered: 2016 Enter GPA: 3.2 You entered: 3.2 Enter Major: IT You entered: IT Person 1: person is a Student Name:Srinivas Current Adress:Visakhapatnam Permanent Adress:Visakhapatnam ID Number:22554 Birth Date1966 Age:50 Entry Year:2016 Total Years in System:0 Major:IT GPA 3.2