Use the following criteria to create a refernece data type u
Use the following criteria to create a refernece data type using Java.
Problem statement:
Create a reference data type that will be used by a college to store a persons
information. The information that needs to be stored are the persons name,
current address, permanent address, id number, date of birth, current age, year
the person entered the college system, and how long the person has been with
the college. Rather than ask for the current age, calculate it using the persons
date of birth and current year. Rather than ask for how long the user has been with
the college, calculate it using by using the year the person first entered the system
and the current year.
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.
In your test class:
Don\'t change anything in the test class.
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:



