Difference between revisions of "Person class lab assignment"
From WLCS
m (Reverted edits by Ysukivewug (talk) to last revision by Admin) |
|||
Line 1: | Line 1: | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
== Person class == | == Person class == | ||
Create a Person class using the following specifications: | Create a Person class using the following specifications: | ||
Line 43: | Line 35: | ||
− | + | <source lang="java"> | |
Person p = new Person(); | Person p = new Person(); | ||
System.out.println(p); //automatically calls the toString() method | System.out.println(p); //automatically calls the toString() method | ||
− | + | </source> | |
Latest revision as of 13:49, 29 November 2010
Person class
Create a Person class using the following specifications:
Attributes:
- first name
- last name
- birthdate
- id number
- height
- weight
- hair color
- eye color
Methods:
- Create a default constructor
- Create a specific constructor
- Create all the setters (mutators) for the listed attributes
- Create all the getters (accessors) for the listed attributes
Starting Template:
- Using PersonTestMain.java
- Create an instance of Person using the default constructor.
- Print out all the attributes using the getter methods.
- Change the attributes to different values by using the setter methods.
- Print out the attributes again.
toString() method
- Using the example below in your main() method, print out your Person variable:
Person p = new Person();
System.out.println(p); //automatically calls the toString() method
- What did you see when you tried to print the Person variable?
- Add a new method named toString() to your class
- toString() should return a String
- toString() should not have any parameters
- Create a String in the method that contains all your attributes
- return the String
- Now run your main() method again. What do you see?