SlideShare a Scribd company logo
1 of 12
Download to read offline
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
//PersonTest.java
import java.util.Scanner;
class PersonTest
{
//Main method
publicstaticvoid 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(format::YYYY-MM-DD): ");
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());
}
}
//PersonInformation.java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
importjava.time.LocalDate;
importjava.time.format.DateTimeFormatter;
class Persons_Information
{
private String personName;
private String currentAdress;
private String permanentAdress;
privateint idNumber;
private String birthDate;
privateint personAge;
privateint entryYear;
privateint totalYears;
finalint 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
publicvoid setPersonName(String personName) {
this.personName = personName;
}
publicvoid setCurrentAdress(String currentAdress) {
this.currentAdress = currentAdress;
}
publicvoid setpermanentAdress(String permanentAdress) {
this.permanentAdress = permanentAdress;
}
publicvoid setIdNumber(int id) {
idNumber = id;
}
publicvoid setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
publicvoid setPersonAge(int pa) {
//CALCULATE THE CURRENT YEAR, MONTH AND DAY
//INTO SEPERATE VARIABLES
personAge = pa;
}
publicvoid setEntryYear(int ey) {
entryYear = ey;
}
publicvoid setTotalYears(int ty) {
totalYears = ty;
}
//output
public String getPersonName() {
return personName;
}
public String getCurrentAdress() {
return currentAdress;
}
public String getPermanentAdress() {
return permanentAdress;
}
publicint getIdNumber() {
return idNumber;
}
public String getBirthDate() {
return birthDate;
}
publicint getPersonAge() {
return personAge;
}
publicint getEntryYear() {
return entryYear;
}
publicint getTotalYears() {
return totalYears;
}
//Method To Calculate Age of a person
privateint 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();
intthisMonth = Integer.parseInt(dateFormat.format(date));
dateFormat = new SimpleDateFormat("DD");
date = new java.util.Date();
intthisDay = 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
privateint 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();
}
}

More Related Content

Similar to In your Person classAdd a constant field to store the current yea.pdf

Can someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdfCan someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdf
Amansupan
 
please help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdfplease help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdf
arishmarketing21
 
Here is the code for youimport java.util.; class HeightOfChild.pdf
Here is the code for youimport java.util.; class HeightOfChild.pdfHere is the code for youimport java.util.; class HeightOfChild.pdf
Here is the code for youimport java.util.; class HeightOfChild.pdf
ANJANEYAINTERIOURGAL
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdf
aplolomedicalstoremr
 
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdfThe solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
aparnatiwari291
 
Hi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfHi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdf
fashioncollection2
 
Polygon.javapublic class Polygon { private int numSides; priva.pdf
Polygon.javapublic class Polygon { private int numSides; priva.pdfPolygon.javapublic class Polygon { private int numSides; priva.pdf
Polygon.javapublic class Polygon { private int numSides; priva.pdf
apnafreez
 
Polygon.javapublic class Polygon { private int numSides; priva.pdf
Polygon.javapublic class Polygon { private int numSides; priva.pdfPolygon.javapublic class Polygon { private int numSides; priva.pdf
Polygon.javapublic class Polygon { private int numSides; priva.pdf
apnafreez
 
I need help for my next project due next tuesday can you help me in .pdf
I need help for my next project due next tuesday can you help me in .pdfI need help for my next project due next tuesday can you help me in .pdf
I need help for my next project due next tuesday can you help me in .pdf
amritashinfosalys
 
I have already done the first 2 parts(factorial and fibonacci) also .pdf
I have already done the first 2 parts(factorial and fibonacci) also .pdfI have already done the first 2 parts(factorial and fibonacci) also .pdf
I have already done the first 2 parts(factorial and fibonacci) also .pdf
udit652068
 
Overview You are tasked with writing a program called Social Security.pdf
Overview You are tasked with writing a program called Social Security.pdfOverview You are tasked with writing a program called Social Security.pdf
Overview You are tasked with writing a program called Social Security.pdf
info961251
 

Similar to In your Person classAdd a constant field to store the current yea.pdf (20)

Can someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdfCan someone help me with this code When I run it, it stops after th.pdf
Can someone help me with this code When I run it, it stops after th.pdf
 
TypeScript by Howard
TypeScript by HowardTypeScript by Howard
TypeScript by Howard
 
Howard type script
Howard   type scriptHoward   type script
Howard type script
 
Type script by Howard
Type script by HowardType script by Howard
Type script by Howard
 
please help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdfplease help with java questionsJAVA CODEplease check my code and.pdf
please help with java questionsJAVA CODEplease check my code and.pdf
 
Here is the code for youimport java.util.; class HeightOfChild.pdf
Here is the code for youimport java.util.; class HeightOfChild.pdfHere is the code for youimport java.util.; class HeightOfChild.pdf
Here is the code for youimport java.util.; class HeightOfChild.pdf
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdf
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdfThe solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
 
Hi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfHi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdf
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Java script
Java scriptJava script
Java script
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
Polygon.javapublic class Polygon { private int numSides; priva.pdf
Polygon.javapublic class Polygon { private int numSides; priva.pdfPolygon.javapublic class Polygon { private int numSides; priva.pdf
Polygon.javapublic class Polygon { private int numSides; priva.pdf
 
Polygon.javapublic class Polygon { private int numSides; priva.pdf
Polygon.javapublic class Polygon { private int numSides; priva.pdfPolygon.javapublic class Polygon { private int numSides; priva.pdf
Polygon.javapublic class Polygon { private int numSides; priva.pdf
 
I need help for my next project due next tuesday can you help me in .pdf
I need help for my next project due next tuesday can you help me in .pdfI need help for my next project due next tuesday can you help me in .pdf
I need help for my next project due next tuesday can you help me in .pdf
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptx
 
I have already done the first 2 parts(factorial and fibonacci) also .pdf
I have already done the first 2 parts(factorial and fibonacci) also .pdfI have already done the first 2 parts(factorial and fibonacci) also .pdf
I have already done the first 2 parts(factorial and fibonacci) also .pdf
 
Overview You are tasked with writing a program called Social Security.pdf
Overview You are tasked with writing a program called Social Security.pdfOverview You are tasked with writing a program called Social Security.pdf
Overview You are tasked with writing a program called Social Security.pdf
 

More from arihanthtextiles

I need help writing the methods for the song and playList class in J.pdf
I need help writing the methods for the song and playList class in J.pdfI need help writing the methods for the song and playList class in J.pdf
I need help writing the methods for the song and playList class in J.pdf
arihanthtextiles
 
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdfExplain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
arihanthtextiles
 
Epithelial tissues that are only one cell thick are called _____ Epi.pdf
Epithelial tissues that are only one cell thick are called _____  Epi.pdfEpithelial tissues that are only one cell thick are called _____  Epi.pdf
Epithelial tissues that are only one cell thick are called _____ Epi.pdf
arihanthtextiles
 
Clostridium difficile is a bacteria currently making headlines. It h.pdf
Clostridium difficile is a bacteria currently making headlines. It h.pdfClostridium difficile is a bacteria currently making headlines. It h.pdf
Clostridium difficile is a bacteria currently making headlines. It h.pdf
arihanthtextiles
 
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdfYou cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
arihanthtextiles
 
You have constructed a new reporter gene to help you find novel zebr.pdf
You have constructed a new reporter gene to help you find novel zebr.pdfYou have constructed a new reporter gene to help you find novel zebr.pdf
You have constructed a new reporter gene to help you find novel zebr.pdf
arihanthtextiles
 
A new species of sea horse has been discovered and is being sequenced.pdf
A new species of sea horse has been discovered and is being sequenced.pdfA new species of sea horse has been discovered and is being sequenced.pdf
A new species of sea horse has been discovered and is being sequenced.pdf
arihanthtextiles
 

More from arihanthtextiles (20)

I need help writing the methods for the song and playList class in J.pdf
I need help writing the methods for the song and playList class in J.pdfI need help writing the methods for the song and playList class in J.pdf
I need help writing the methods for the song and playList class in J.pdf
 
How do autopolyploidy and allopolyploidy differ Only autopolyploidy.pdf
How do autopolyploidy and allopolyploidy differ  Only autopolyploidy.pdfHow do autopolyploidy and allopolyploidy differ  Only autopolyploidy.pdf
How do autopolyploidy and allopolyploidy differ Only autopolyploidy.pdf
 
How do you find the order of growth for the following recurrenceT.pdf
How do you find the order of growth for the following recurrenceT.pdfHow do you find the order of growth for the following recurrenceT.pdf
How do you find the order of growth for the following recurrenceT.pdf
 
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdfExplain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
Explain SEAD, SAR and SPAAR routing protocols in more details.So.pdf
 
Dynamic routing protocols are used toa) establish paths real ti.pdf
Dynamic routing protocols are used toa) establish paths real ti.pdfDynamic routing protocols are used toa) establish paths real ti.pdf
Dynamic routing protocols are used toa) establish paths real ti.pdf
 
Evaluate expression without a calculator. Write answer in radians..pdf
Evaluate expression without a calculator. Write answer in radians..pdfEvaluate expression without a calculator. Write answer in radians..pdf
Evaluate expression without a calculator. Write answer in radians..pdf
 
Epithelial tissues that are only one cell thick are called _____ Epi.pdf
Epithelial tissues that are only one cell thick are called _____  Epi.pdfEpithelial tissues that are only one cell thick are called _____  Epi.pdf
Epithelial tissues that are only one cell thick are called _____ Epi.pdf
 
Describe in five sentences how the TLR system is used to fight HSV-1 .pdf
Describe in five sentences how the TLR system is used to fight HSV-1 .pdfDescribe in five sentences how the TLR system is used to fight HSV-1 .pdf
Describe in five sentences how the TLR system is used to fight HSV-1 .pdf
 
Daunte is frustrated about managements recent decision to merge hi.pdf
Daunte is frustrated about managements recent decision to merge hi.pdfDaunte is frustrated about managements recent decision to merge hi.pdf
Daunte is frustrated about managements recent decision to merge hi.pdf
 
Concern over the weather associated with El Nino has increased intere.pdf
Concern over the weather associated with El Nino has increased intere.pdfConcern over the weather associated with El Nino has increased intere.pdf
Concern over the weather associated with El Nino has increased intere.pdf
 
Clostridium difficile is a bacteria currently making headlines. It h.pdf
Clostridium difficile is a bacteria currently making headlines. It h.pdfClostridium difficile is a bacteria currently making headlines. It h.pdf
Clostridium difficile is a bacteria currently making headlines. It h.pdf
 
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdfYou cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
You cloned an EcoRI digested DNA fragment into a unique EcoRI site o.pdf
 
You have constructed a new reporter gene to help you find novel zebr.pdf
You have constructed a new reporter gene to help you find novel zebr.pdfYou have constructed a new reporter gene to help you find novel zebr.pdf
You have constructed a new reporter gene to help you find novel zebr.pdf
 
Write a Java program that will incorporate either a sequential searc.pdf
Write a Java program that will incorporate either a sequential searc.pdfWrite a Java program that will incorporate either a sequential searc.pdf
Write a Java program that will incorporate either a sequential searc.pdf
 
Answer only with excel instructions.Assume that 96 of ticket hold.pdf
Answer only with excel instructions.Assume that 96 of ticket hold.pdfAnswer only with excel instructions.Assume that 96 of ticket hold.pdf
Answer only with excel instructions.Assume that 96 of ticket hold.pdf
 
A new species of sea horse has been discovered and is being sequenced.pdf
A new species of sea horse has been discovered and is being sequenced.pdfA new species of sea horse has been discovered and is being sequenced.pdf
A new species of sea horse has been discovered and is being sequenced.pdf
 
Which of the following is the mechanism of action for Herceptin Her.pdf
Which of the following is the mechanism of action for Herceptin  Her.pdfWhich of the following is the mechanism of action for Herceptin  Her.pdf
Which of the following is the mechanism of action for Herceptin Her.pdf
 
Which of these statements best describes an element An element is c.pdf
Which of these statements best describes an element  An element is c.pdfWhich of these statements best describes an element  An element is c.pdf
Which of these statements best describes an element An element is c.pdf
 
What is the general goal for descriptive statistics How is the goal.pdf
What is the general goal for descriptive statistics  How is the goal.pdfWhat is the general goal for descriptive statistics  How is the goal.pdf
What is the general goal for descriptive statistics How is the goal.pdf
 
What effects are shown on this graph (assume that all effects where.pdf
What effects are shown on this graph (assume that all effects where.pdfWhat effects are shown on this graph (assume that all effects where.pdf
What effects are shown on this graph (assume that all effects where.pdf
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 

In your Person classAdd a constant field to store the current yea.pdf

  • 1. 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 = "";
  • 2. 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) {
  • 3. 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 }
  • 4. 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 {
  • 5. 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());
  • 6. 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 }
  • 7. Solution //PersonTest.java import java.util.Scanner; class PersonTest { //Main method publicstaticvoid 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(format::YYYY-MM-DD): "); 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______");
  • 8. 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()); } } //PersonInformation.java import java.text.DateFormat; import java.text.SimpleDateFormat; importjava.time.LocalDate; importjava.time.format.DateTimeFormatter; class Persons_Information { private String personName; private String currentAdress; private String permanentAdress; privateint idNumber;
  • 9. private String birthDate; privateint personAge; privateint entryYear; privateint totalYears; finalint 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 publicvoid setPersonName(String personName) { this.personName = personName; } publicvoid setCurrentAdress(String currentAdress) { this.currentAdress = currentAdress; } publicvoid setpermanentAdress(String permanentAdress) { this.permanentAdress = permanentAdress; } publicvoid setIdNumber(int id) {
  • 10. idNumber = id; } publicvoid setBirthDate(String birthDate) { this.birthDate = birthDate; } publicvoid setPersonAge(int pa) { //CALCULATE THE CURRENT YEAR, MONTH AND DAY //INTO SEPERATE VARIABLES personAge = pa; } publicvoid setEntryYear(int ey) { entryYear = ey; } publicvoid setTotalYears(int ty) { totalYears = ty; } //output public String getPersonName() { return personName; } public String getCurrentAdress() { return currentAdress; } public String getPermanentAdress() { return permanentAdress; } publicint getIdNumber() { return idNumber; } public String getBirthDate() { return birthDate; } publicint getPersonAge() { return personAge; } publicint getEntryYear() {
  • 11. return entryYear; } publicint getTotalYears() { return totalYears; } //Method To Calculate Age of a person privateint 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(); intthisMonth = Integer.parseInt(dateFormat.format(date)); dateFormat = new SimpleDateFormat("DD"); date = new java.util.Date(); intthisDay = 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 privateint 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;
  • 12. 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(); } }