In your Person class Add a constant field to store the curre

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.

*** This is what I have so far

48 class Persons_Information
49 {
50 private String personName;
51 private String currentAdress;
52 private String permanentAdress;
53 private int idNumber;
54 private String birthDate;
55 private int personAge;
56 private int entryYear;
57 private int totalYears;
58
59 public Persons_Information() {
60 personName = \"\";
61 currentAdress = \"\";
62 permanentAdress = \"\";
63 idNumber = 0;
64 birthDate = \"\";
65 personAge = 0;
66 entryYear = 0;
67 totalYears = 0;
68 }
69
70 public Persons_Information (int atIdNumber) {
71 idNumber = atIdNumber;
72 personName = \"\";
73 currentAdress = \"\";
74 permanentAdress = \"\";
75 birthDate = \"\";
76 personAge = 0;
77 entryYear = 0;
78 totalYears = 0;
79 }
80
81 //intput
82
83 public void setPersonName(String personName) {
84 this.personName = personName;
85 }
86
87 public void setCurrentAdress(String currentAdress) {
88 this.currentAdress = currentAdress;
89 }
90
91
92 public void setpermanentAdress(String permanentAdress) {
93 this.permanentAdress = permanentAdress;
94 }
95
96 public void setIdNumber(int id) {
97 idNumber = id;
98 }
99
100 public void setBirthDate(String birthDate) {
101 this.birthDate = birthDate;
102 }
103
104 public void setPersonAge(int pa) {
105 personAge = pa;
106 }
107
108 public void setEntryYear(int ey) {
109 entryYear = ey;
110 }
111
112 public void setTotalYears(int ty) {
113 totalYears = ty;
114 }
115
116 //output
117
118 public String getPersonName() {
119 return personName;
120 }
121
122 public String getCurrentAdress() {
123 return currentAdress;
124 }
125
126 public String getPermanentAdress() {
127 return permanentAdress;
128 }
129
130 public int getIdNumber() {
131 return idNumber;
132 }
133 public String getBirthDate() {
134 return birthDate;
135 }
136
137 public int getPersonAge() {
138 return personAge;
139 }
140
141 public int getEntryYear() {
142 return entryYear;
143 }
144
145 public int getTotalYears() {
146 return totalYears;
147 }
148
149 //toString
150
151 public String toString()
152 {
153 return \"\ \ Name:\" + personName +
154 \"\ Current Adress:\" + currentAdress +
155 \"\ Permanent Adress:\" + permanentAdress +
156 \"\ ID Number:\" + idNumber +
157 \"\ Birth Date\" + birthDate +
158 \"\ Age:\" + personAge +
159 \"\ Entry Year:\" + entryYear +
160 \"\ Total Years in System:\" + totalYears;
161 }
162 }
163

*** 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.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:

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site