CSE 110 - Lab 6
What this Lab Is About:
Working with classes
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 variab.
CSE 110 - Lab 6 What this Lab Is About Working wi.docx
1. CSE 110 - Lab 6
What this Lab Is About:
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
2. // 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
//-->
3. // 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
4. }
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
}
5. 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
6. 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)
{
7. //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
8. 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(“<PersonObject.getName()> 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()> ”);
9. 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,
10. 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.
11. Last Step: Submit your lab by following the instructions below:
*****************************************************
*************************
Submit your Lab6.java file to the Submission Server. Go to the
Submission Server site,
https://courses.eas.asu.edu/cse110b/login , then click on Lab
Submissions in the left frame. The
dropdown box will start in Lab6, so you don’t have to change it
(you will for future labs). Click
on the browse button and find where you saved your Lab6.java
file (and not the Lab6.class file)
on your computer. Upload the file to the site and then click on
the Submit button.
Your file will be submitted and a screen will show up
displaying if your program compiled and
what your output is when run on some sample input (in this case
nothing).
You should then check to make sure that the actual file
submitted properly and is readable to the
grader. To do so click on Grades in the frame on the left of the
page and then click on the 0
underneath Lab6. You will again see that your program
compiled and the sample output, but you
12. should scroll down to the bottom of the screen and make sure
your file is readable as well.
https://courses.eas.asu.edu/cse110/login
https://courses.eas.asu.edu/cse110/login
https://courses.eas.asu.edu/cse110b/login