ArrayTest2.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 ArrayTest2.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

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

class Human {

   // Properties of the class...
   public String name;

   // Constructor of the class...
   public Human(String aName) {
      name = aName;
   }

   // Methods of the class...
   public String toString() {
      return "I am a human and my name is " + name;
   }
}

class ArrayTest2 {

   public static void main(String[] args) {
      ArrayTest2 x = new ArrayTest2();
      x.doStuff();
   }

   public void doStuff() {

      // insert your code here:

   }
}

// QUESTIONS:

// (1) In the doStuff method, define an array of three Human
// objects called threesome.

// HINT: This is almost identical to what you did to create
// the double array in question (2) of ArrayTest.java,
// except that the class Human appears in place of the type
// double.
// 
// (2) Create three new Human objects with names Angus,
// Brian and Charles and assign them to the threesome array.

// HINT: Unlike question (3) of ArrayTest.java, you can't
// use a loop for this.

// 
// (3) In the "doStuff" methodm, add a "for" loop to print
// out the three Human objects using each object's toString
// method.
//