Problem Description: For this Lab you have to implement a class Person. A person
ID: 3673171 • 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
}
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
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
Explanation / Answer
//////////////////////////////////////////////////// Person1.java ////////////////////////////////////////////////////
import java.util.*;
public class Person1 {
// INSTANCE VARIABLES
private String firstname; // instance attribute for the person's name
private String lastname; // instance attribute for the person's age
private String address;
private int birthYear; // instance attribute for the person's gender, possible values: Person.MALE, Person.FEMALE
private int age;
// CONSTRUCTORS
public Person1 (String firstname, String lastname, String address , int birthYear) {
this.firstname = firstname;
this.lastname = lastname;
this.address = address;
this.birthYear = birthYear;
}
// GETTERS
public String getName() {
return lastname;
}
public int getAge( int currentYear) {
return currentYear-birthYear;
}
public String getAddress(){
return address;
}
// SETTERS
public void setName(String lastname) {
this.lastname = lastname;
}
public void setAge(int birthYear) {
this.birthYear = birthYear;
}
public void setAddress (String address) {
this.address = address;
}
public String changeAddress(String addr)
{
address = addr;
return address;
}
}
////////////////////////////////////////// Lab6.java /////////////////////////////////////////////
import java.util.Scanner;
public class Lab6 {
public static void main(String[] args)
{
int birthYear;
String firstname,lastname,address;
Scanner in = new Scanner(System.in);
System.out.println("Enter the first name of the person");
firstname = in.nextLine();
System.out.println("Enter the last name of the person");
lastname = in.nextLine();
System.out.println("Enter the address where the person lives");
address = in.nextLine();
System.out.println("Enter the birth year of the person ");
birthYear = in.nextInt();
Person1 PersonObject = new Person1(firstname,lastname,address, birthYear);
System.out.println(PersonObject.getName()+ " is "+ PersonObject.getAge(2016)+ " years old in 2016 and will be " + PersonObject.getAge(2026) + " years old in ten years.");
System.out.println(PersonObject.getName() +" lives in "+ PersonObject.getAddress()+".");
PersonObject.changeAddress(" 72 E University Dr Tempe, AZ ");
System.out.println(PersonObject.getName() +" has moved to a new location: " + PersonObject.getAddress()+"." );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.