Exercise 1a        Name __________________        Score __/10

Modified: 

Use the following Java class definition for exercise questions:

  1. (1) What is the class name?    Person
       
     
  2. (1) List the accessor method(s)?    public int getAge()

     
  3. (1) List the mutator method(s)?    public void setName(String newName)
                                                      public void incrementAge()

     
  4. (3) For the object at right, the printPerson method can be called by:

        person2.printPerson();

    Give the Java constructor or method call(s) to:
     
    1. create the object person2.   
          Person person2 = new Person("George Bush", 57);

       
    2. change the name field of the person2 object to "George W. Bush".
          person2.setName("George W. Bush");
       
    3. increment the age field to 60.
          person2.incrementAge();
          person2.incrementAge();
          person2.incrementAge();
       
  5. (2) Write a complete accessor method for the name field.

             public String getName() {
                return name;
             }

     
  6. (2) Write a complete mutator method for the age field.

            public void setAge(int newAge) {
                age = newAge;
            }
 public class Person
 {
 private int age;
 private String name;
 public Person(String newName)
 {
       name = newName;
       age = 0;
 }
 public Person (String newName, int newAge)
 {
       name = newName;
       age = newAge;
 }
 public void setName(String newName)
 {
       name = newName;
 }
 public int getAge()
 {
       return age;
 }
 public void incrementAge()
 {
       age=age+1;
 }
 public void printPerson()
 {
       System.out.println("Name:" + name + " Age:" + age);
 }
 }