PersonTest.java Questions

Instructions

  1. With the left mouse button pressed, drag the mouse across the contents of the program listing that is shown below. This will select the program listing. Then choose Copy from the Edit menu of your web browser to copy the program listing to the clipboard.
  2. Start your text editor and create a new file called PersonTest.java. Choose Paste to paste the contents of the program listing from the clipboard into the new file.
  3. Save the file to disk and begin answering the questions that are shown below.
  4. When you need to look at the model answer, click here.
  5. Click here to go back to the tutorials menu.

Program listing

// PersonTest.java
// Copyright (C) 1998-2016 Davin Pearson
// Website: http://davin.50webs.com/java-tutorials

class Person
{
   // Properties of the class...
   private String name;
   private int    age;

   // Constructor of the class...
   public Person(String aName, int anAge)
   {
      name = aName;
      age  = anAge;
   }

   // Methods of the class...
   public void talk()
   {
      System.out.println("Hi, my name's " + name);
      System.out.println("and my age is " + age);
      System.out.println();
   }
   public void commentAboutAge()
   {
      if (age < 5)
      {
         System.out.println("baby");
      }
      if (age == 5)
      {
         System.out.println("time to start school");
      }
   }
}

class PersonTest
{
   // The main method is the point of entry into the program...
   public static void main(String[] args)
   {
      Person ls = new Person("Luke Skywalker",34);
      Person wp = new Person("Winston Peters",48);

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

// QUESTIONS

// (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.
// 
// (1b) Add an extra parameter to the the constructor so
// that the property gets initialised correctly.
// 
// (1c) Modify the main method so that the Luke Skywalker
// and Winston Peters objects get the appropriate value of
// the new property.
// 
// (1d) Modify the talk method so that the new property is
// printed to the screen when the talk method is called.
// 
// (2) Modify the commentAboutAge method to print the
// message "old person" if the person's age is greater
// than 40.
// 
// (3) In the main method add two calls to commentAboutAge,
// one for Luke Skywalker and one for Winston Peters.
// 
// (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.
// 
// (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.
// 
// (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.
// 
// (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.
// 
// (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.
//