Problem Description: For this Lab you have to implement a class Person. A person has a fistname, lastname, address, and birthday year. Supply a constructor and the following methods: getName(), getAge(), getAddress, and changeAddress(),.
Step 1: Getting Started Create a class called Lab6. Be sure to name your file Lab6.java. Lab Documentation:
At the beginning of each programming assignment you must have a comment block with the following information:
/*-------------------------------------------------------------------------
// AUTHOR: your name
// FILENAME: title of the source file
// SPECIFICATION: description of the program
// FOR: CSE 110- Lab #6
// TIME SPENT: how long it took you to complete the assignment //-----------------------------------------------------------*/
Step 2: Declaring Class Examining the problem, we need to create a Person class, declare Person class as follows
class Person {
}
Inside the class, declare some data members: a string variable called firstname, a string variable called lastname, and an integer called birthYear.
// declare some variables of different types:
// an string called firstname
//-->
// a string called lastname
//-->
// a string called address
//-->
// an int called birthYear
//-->
Step 3: Defining the constructor Remember the constructor assigns input data to the data members.
public Person (String fname, String lname, String
addr, int year)
{
// write the segment of code
//that assigns input data to the data members
}
Step4: Supply the methods
A method called getName() to get the name of an object Person:
public String getName()
{
// write a line of code
//that returns the last name
}
A method called getAge() to get the age of an object Person:
public int getAge(int currentYear)
{
// write a segment of code
//that returns the age
}
A method called getAddress() to get the age of an object Person:
public String getAddress()
{
// write a line of code
//that returns the address
}
A method called changeAddress(String addr) to change the address of an object Person:
public String changeAddress(String addr)
{
// write the segment of code
//that updates the address
}
Step4: Calling the constructor of Person class from the main method
In order to use Person class variables and methods we need to have a runner function. For any class in java, main method will be the running method.
You class Lab6 will have the main method.
public class Lab6{
public static void main(String[] args)
{
}
}
In order to use the Person class variables and methods, you need to use the constructor to create an object Person.
public class Lab6{
public static void main(String[] args)
{
//declare variables where you will store
//inputs from user
-->
// declare a Scanner object
-->
//prompt the user for inputs
//firstname, lastname, address, birthyear
-->
// store the input in the declared variables
-->
//use the constructor
//to create a brand-new object Person
-->
Hint: Do not forget to import the Scanner package at the very top of your program: Import java.util.Scanner
Step6: Calling Person class methods and display the output
The methods in the Person class will display the required outputs.
//Call the getName() method in order to print the
//lastname of the object Person you just created.
//Call the getAge(currentYear) method in order to
//print the age.
--> System.out.println(“is
< PersonObject.getAge(2016)> years old in 2016 and will
be < PersonObject>.getAge(2026)> years old in ten
years.“)
//Call the getAddres() method in order to
//print the address.
--> System.out.println(“< PersonObject.getName()> lives
in < PersonObject.getAddress()> ”);
Step7: Calling Person class methods
Now, call the changeAddress(addr) method in order to update the address to the following address:
< PersonObject>.changeAddress(72 E University Dr Tempe, AZ);
Step8: Display the output
System.out.println(“< PersonObject.getName()> has moved to a new
location: < PersonObject.getAddress()>
Sample Output
Below is an example of what your output should roughly look like when this lab is completed. All text in bold represents user input.
Sample Run 1:
Enter the first name of the person John
Enter the last name of the person Mann
Enter the address where the person lives E Rural Rd Tempe, AZ.
Enter the birth year of the person 1991
Mann is 24 years old in 2016 and will be 34 years old in ten years. Mann lives in E Rural Rd
Tempe, AZ.
Mann has moved to a new location: 72 E University Dr Tempe, AZ
Sample Run 2:
Enter the first name of the person Ben
Enter the last name of the person Freeman
Enter the address where the person lives 700 Pelham Rd Jacksonville, Florida.
Enter the birth year of the person 1981
Freeman is 34 in 2016 and will be 44 in ten years. Freeman lives in 700 Pelham Rd
Jacksonville, Florida.
Freeman has moved to a new location: 72 E University Dr Tempe, AZ.