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
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;
private int currentYear=2016;
private int age;
private int timeincolg;
public Persons_Information() {
personName = \"\";
currentAdress = \"\";
permanentAdress = \"\";
idNumber = 0;
birthDate = \"\";
personAge = 0;
entryYear = 0;
totalYears = 0;
age=0;
timeincolg=0;
}
public Persons_Information (int atIdNumber) {
idNumber = atIdNumber;
personName = \"\";
currentAdress = \"\";
permanentAdress = \"\";
birthDate = \"\";
personAge = 0;
entryYear = 0;
totalYears = 0;
age=0;
timeincolg=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) {
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;
}
//toString
public int calculate_age() {
return currentYear-age;
}
public int calculate_colgyear() {
return currentYear-timeincolg;
}
public String toString()
{
return \"\ \ Name:\" + personName +
\"\ Current Adress:\" + currentAdress +
\"\ Permanent Adress:\" + permanentAdress +
\"\ ID Number:\" + idNumber +
\"\ Birth Date\" + birthDate +
\"\ Age:\" + personAge +
\"\ Entry Year:\" + entryYear +
\"\ Total Years in System:\" + totalYears+
\"\ Person age:\" + calculate_age()+
\"\ Total time in colg:\" + calculate_colgyear();
}
}
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
Students_Information.java :-
-----------------------------------------------
public class Students_Information extends Persons_Information{
private String major;
private double GPA = 0;
public Students_Information(){
}
public void setMajor(String major){
this.major = major;
}
public String getMajor(){
return this.major;
}
public void setGpa(double gpa){
this.GPA = gpa;
}
public double setGpa(){
return this.GPA;
}
@Override
public String toString() {
String studentInfo = \"Person is a Student\ \";
studentInfo = studentInfo+ \"\ \ Name:\" + personName +
\"\ Current Adress:\" + currentAdress +
\"\ Permanent Adress:\" + permanentAdress +
\"\ ID Number:\" + idNumber +
\"\ Birth Date\" + birthDate +
\"\ Age:\" + personAge +
\"\ Entry Year:\" + entryYear +
\"\ Total Years in System:\" + totalYears+
\"\ Person age:\" + calculate_age()+
\"\ Total time in colg:\" + calculate_colgyear() +
\"\ Student Major is : \" + major +
\"\ Student GPA is : \" + GPA ;
return studentInfo;
}
}
PersonTest.java :-
-------------------------------
import java.util.Scanner;
class PersonTest
{
//Main method
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
//Creating object
Students_Information student1 = new Students_Information();
//Reading and updating values
System.out.print(\"\ Enter Student ID: \");
student1.setIdNumber( Integer.parseInt(sc.nextLine()));
System.out.print(\"\ Enter Student Name: \");
student1.setPersonName(sc.nextLine());
System.out.print(\"\ Enter Current Address: \");
student1.setCurrentAdress(sc.nextLine());
System.out.print(\"\ Enter Permanent Address: \");
student1.setpermanentAdress(sc.nextLine());
System.out.print(\"\ Enter Birth Date: \");
student1.setBirthDate(sc.nextLine());
System.out.print(\"\ Enter Entry Year: \");
student1.setEntryYear(Integer.parseInt(sc.nextLine()));
System.out.print(\"\ Enter Student Major: \");
student1.setMajor(sc.nextLine());
System.out.print(\"\ Enter Student GPA: \");
student1.setGpa( Double.parseDouble(sc.nextLine()));
//Printing person 1 details
System.out.println(\"Student 1: \ \" + student1.toString());
}
}







