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
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














