Problem Description: For this Lab you have to implement a class Person. A person
ID: 3671912 • Letter: P
Question
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.
Step 2: Declaring Class
Examining the problem, we need to create a Person class, declare Person class as follows
Inside the class, declare some data members: a string variable called firstname, a string variable called lastname, and an integer called birthYear.
Step 3: Defining the constructor
Remember the constructor assigns input data to the data members.
}
Step4: Supply the methods
A method called getName() to get the name of an object Person:
}
A method called getAge() to get the age of an object Person:
}
A method called getAddress() to get the age of an object Person:
}
A method called changeAddress(String addr) to change the address of an object Person:
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.
} }
In order to use the Person class variables and methods, you need to use the constructor to create an object Person.
-->
//declare a Scanner object -->
// store the input in the declared variables -->
//use the constructor
//to create a brand-new object Person -->
Step6: Calling Person class methods and display the output
The methods in the Person class will display the required outputs.
Step7: Calling Person class methods
Now, call the changeAddress(addr) method in order to update the address to the following address:
Step8: Display the output
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.
Explanation / Answer
import java.io.*; import java.util.*; public class Lab6 { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the first name of the person"); String fname = br.readLine(); System.out.println("Enter the last name of the person"); String lname = br.readLine(); System.out.println("Enter the address where the person lives"); String addr = br.readLine(); System.out.println("Enter the birth year of the person "); int birthYear = Integer.parseInt(br.readLine()); Person person1 = new Person(fname,lname,addr,birthYear); System.out.println(person1.getName()+" is "+person1.getAge(2016)+" years old in 2016 and will be"+person1.getAge(2026) +" years old in ten years."); } static class Person { String firstname; String lastname; String address; int birthYear; public Person(String firstname, String lastname, String address, int birthYear) { this.firstname = firstname; this.lastname = lastname; this.address = address; this.birthYear = birthYear; } public String getName() { return lastname; } public String getAddress() { return address; } public void changeAddress(String address) { this.address = address; } public int getBirthYear() { return birthYear; } public int getAge(int cYear){ return cYear-birthYear; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.