PersonTest.java Answers

Click here to go back to the questions
// ANSWERS

// (1) Think of a new property that a person can have and add
// it to the Person class.  You should be able to do this
// by copying the pattern of the other properties of the
// Person class.  Four changes to the program listing are
// needed:

// (1a) Add the name of the property and its type to the
// "properties" section of the class.
// 

// For the answer to this question let's suppose the new
// property we are going to add is "height", which measures
// the height in centimeters.

private int height;

// 
// (1b) Add an extra parameter to the the constructor so
// that the property gets initialised correctly.
// 

// ANSWER: The constructor will be changed to this:

public Person(String aName, int anAge, int aHeight)
{
   name   = aName;
   age    = anAge;
   height = aHeight;
}

// 
// (1c) Modify the main method so that the Luke Skywalker
// and Winston Peters objects get the appropriate value of
// the new property.
// 

// The main method might be changed to this:

public static void main(String[] args)
{
   Person ls = new Person("Luke Skywalker",34,180);
   Person wp = new Person("Winston Peters",48,173);

   ls.talk();
   wp.talk();
}

// 
// (1d) Modify the talk method so that the new property is
// printed to the screen when the talk method is called.
// 

// The "talk" method would be changed to this:

public void talk()
{
   System.out.println("Hi, my name's " + name);
   System.out.println("and my age is " + age);
   System.out.println("and my height is " + height);
   System.out.println();
}

// 
// (2) Modify the commentAboutAge method to print the
// message "old person" if the person's age is greater
// than 40.
// 

// ANSWER: The following code should be added inside the
// "commentAboutAge" method:

if (age > 40)
{
   System.out.println("old person");
}

// 
// (3) In the main method add two calls to commentAboutAge,
// one for Luke Skywalker and one for Winston Peters.
// 

// ANSWER: These two lines should be added towards the end
// of the main method:

ls.commentAboutAge();
wp.commentAboutAge();

// 
// (4) Write a method growOlder that adds one year to the
// person's age.  Make Winston Peters get older by one year
// by putting a call in the main method to do this.

// HINT: Look at the talk and the commentAboutAge methods to
// see how to define a method.
// 
// ANSWER: Here is how to define the method:

public void growOlder()
{
   age = age + 1;
}

// Alternatively, you can write "age++" or "++age" or
// "age+=1" in the place of "age=age+1".  These statements
// all do the same thing.

//  To make Winston Peters grow older, put the following
//  line into the main method:

wp.growOlder();

// 
// (5) Write a method giveKnighthood that adds the word
// "Sir" to the beginning of a person's name.  Then make
// Winston Peters become "Sir Winston Peters" by calling
// this method from the main method.

// HINT: This is much like question (4), except you are
// adding strings instead of numbers.
// 

public void giveKnighthood()
{
   name = "Sir " + name;
}

// To call the method, put

wp.giveKnighthood();

// in the main method.

// 
// (6) How is it possible to print the value of Luke
// Skywalker's age directly from inside the main method
// without calling the talk method?

// HINT: The talk method has a line of code to print the
// person's age.  Copy this code to the main method, but
// since you are in another class from the age property, you
// will need to mention the name of the object in front of
// the age property, so age becomes ls.age.

// Also, you will need to change the definition of the age
// property from private to public to get this change to
// work.  Why you need to do this will be covered in more
// detail in another tutorial.
// 
//
// ANSWER: The following code in the main method will print
// Luke Skywalker's age:

System.out.println("Luke Skywalker's age is " + ls.age);

// 
// (7) Delete the calls to commentAboutAge that you added in
// question (3), and instead put a call to commentAboutAge
// in the talk method.  Note that when you are calling a
// method of the same class you are in, you don't have to
// put the object name in front of the method.

// Your program should generate similar output to what it
// did before.
// 

// ANSWER: Simply add the following line to the end of the "talk"
// method:

commentAboutAge();

// 
// (8) Write a growOlderBy method which is similar to the
// growOlder method, except that it has an int parameter
// called years which says how many years the person should
// grow older by.  In the main method, place a call to
// growOlderBy to make Luke Skywalker age by ten years.
// 

public void growOlderBy(int years)
{
   age = age + years;
}

// To call this method, use the following line:

ls.growOlderBy(10);

//