Arrays and ArrayLists of Objects12.0
Objects are essentially instances of some class blocks (or segments). In this regard, just like Primitive data types, objects can be organized into a collection such as an Array, an ArrayList, etc. The procedure involved in creating either an Array or an ArrayList of objects is quite similar to the process of creating an Array (or ArrayList) of Primitive data. Since an object is technically considered as a Non-Primitive data type in Java programming, the default initialization value present in a newly created Array or ArrayList of objects is null. Earlier in the past lesson (or chapter) herein, which focused on Arrays, we had already discussed about the fact that: in Java programming, the default initialization value for arrays of Non-Primitive data type such as String, Object, etc., is null. Similarly, in the lesson or chapter herein that focused on ArrayLists, we had also discussed about the fact that: every ArrayList possesses a default initialization value of null upon its creation or instantiation.

Hence, the following code snippets showcase the different approaches that can be adopted for either the creation of an Array of objects or an ArrayList of objects.
/**
 * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
 * @course ProgrammingLessons (proglessons).
 * @title Arrays and ArrayLists of Objects.
 * @author Dr. Bonaventure Chidube Molokwu.
 */

public class ArraysandArrayListsofObjects {
	
	public static void main(String[] args) {
		//Declaration and Instantiation of one-dimensional (1D) arrays - Approach 1
		<className>[] obj1;
		obj1 = new <className>[10];
		<className> obj2[];
		obj2 = new <className>[10];
		
		//Declaration and Instantiation of one-dimensional (1D) arrays - Approach 2
		<className>[] obj1 = new <className>[10];
		<className> obj2[] = new <className>[10];
		
		//Declaration and Instantiation of one-dimensional (1D) arrays - Approach 3
		<className>[] obj1 = {object1, object2, ..., objectn}; //Initialization values for 'obj1[]' array
		<className> obj2[] = {object1, object2, ..., objectn}; //Initialization values for 'obj2[]' array
	}
	
}
Code Snippet: Declaration and instantiation of one-dimensional (1D) array data structures

/**
 * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
 * @course ProgrammingLessons (proglessons).
 * @title Arrays and ArrayLists of Objects.
 * @author Dr. Bonaventure Chidube Molokwu.
 */

import java.util.ArrayList;

public class ArraysandArrayListsofObjects {
	
	public static void main(String[] args) {
		//Declaration and Instantiation of one-dimensional (1D) ArrayList - Approach 1
		ArrayList<<className>> obj1;
		obj1 = new ArrayList<<className>>();
		
		//Declaration and Instantiation of one-dimensional (1D) ArrayList - Approach 2
		ArrayList<<className>> obj1 = new ArrayList<<className>>();
	}
	
}
Code Snippet: Declaration and instantiation of one-dimensional (1D) ArrayList data structures

Array (or ArrayList) structure with its content(s) and index(es)
Caption: Simplified data structure of an Array or an ArrayList of objects comprising several cells/units labelled with their respective indexes



12.1Array of Objects and ArrayList of Objects
Since the default initialization value, with respect to every cell/unit of an Array of objects, is null; therefore, each cell or unit in the Array of objects has to be re-initialized to an instance of the object's class via a suitable Constructor of the object/class.
Working with an Array of objects
Caption: Working with an Array of objects

Exercise 1: With the aid of Object-Oriented Programming (OOP) concepts, implement a Java program that employs an Array of objects capable of storing the particulars of every student registered in a course.
/**
 * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
 * @course ProgrammingLessons (proglessons).
 * @title Arrays and ArrayLists of Objects.
 * @author Dr. Bonaventure Chidube Molokwu.
 */

public class Student { //"Container" class
	
	private String fName;
	private String lName;
	private int regNum;
	
	
	//DEFAULT Constructor
	public Student () {
		System.out.println("DEFAULT Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
	}
	
	//CUSTOM Constructor
	public Student (String firN, int regN) {
		this.fName = firN;
		this.regNum = regN;
		System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
	}
	
	
	//Method
	public String initStudent (String firN, String lasN, int regN) {
		this.fName = firN;
		this.lName = lasN;
		this.regNum = regN;
		
		return this.regNum + ": " + this.lName + ", " + this.fName;
	}

}
Container class: Designing and implementing an OOP class identified as "Student" class

/**
 * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
 * @course ProgrammingLessons (proglessons).
 * @title Arrays and ArrayLists of Objects.
 * @author Dr. Bonaventure Chidube Molokwu.
 */

public class StudentDriver { //"Driver" class

	public static void main(String[] args) {
		//Creating an Array of "Student" objects
		//In other words, the array is comprised of elements with each possessing a custom (data) type of "Student"
		Student[] obj1 = new Student[5];
		
		//Instantiating the first object in the Array of "Student" objects
		obj1[0] = new Student ("John Doe", 40005000);
		
		//Instantiating the second object in the Array of "Student" objects
		obj1[1] = new Student ();
		
		//Instantiating the fifth object in the Array of "Student" objects
		obj1[4] = new Student ("Jane Doe", 70001000);
		
		//Calling a method with respect to the second instantiated object
		String var1 = obj1[1].initStudent("Chidube", "Molokwu", 411111);
		System.out.println(var1);
		
		//Calling a method with respect to the fifth instantiated object
		String var2 = obj1[4].initStudent("Bonaventure", "Molokwu", 411111);
		System.out.println(var2);
	}

}
Driver class: Designing and implementing an OOP class which implements an Array of objects


Similarly, with respect to an ArrayList of objects, the default initialization value in every cell/unit of an ArrayList is null. In this regard, each cell or unit in the ArrayList of objects has to be re-initialized to an actual object (of a given class) via a suitable Constructor of the object/class.
Working with an ArrayList of objects
Caption: Working with an ArrayList of objects

Exercise 2: With the aid of Object-Oriented Programming (OOP) concepts, implement a Java program that uses an ArrayList of objects to store the particulars of every student registered in a course.
/**
 * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
 * @course ProgrammingLessons (proglessons).
 * @title Arrays and ArrayLists of Objects.
 * @author Dr. Bonaventure Chidube Molokwu.
 */

public class Student { //"Container" class
	
	private String fName;
	private String lName;
	private int regNum;
	
	
	//DEFAULT Constructor
	public Student () {
		System.out.println("DEFAULT Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
	}
	
	//CUSTOM Constructor
	public Student (String firN, int regN) {
		this.fName = firN;
		this.regNum = regN;
		System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
	}
	
	
	//Method
	public String initStudent (String firN, String lasN, int regN) {
		this.fName = firN;
		this.lName = lasN;
		this.regNum = regN;
		
		return this.regNum + ": " + this.lName + ", " + this.fName;
	}

}
Container class: Designing and implementing an OOP class identified as "Student" class

/**
 * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
 * @course ProgrammingLessons (proglessons).
 * @title Arrays and ArrayLists of Objects.
 * @author Dr. Bonaventure Chidube Molokwu.
 */

import java.util.ArrayList;

public class StudentDriver { //"Driver" class

	public static void main(String[] args) {
		//Creating an ArrayList that is comprised of "Student" objects
		ArrayList<Student> obj1 = new ArrayList<Student>();
		
		//Instantiating the first object of "Student" class, and adding it to the ArrayList 
		obj1.add(new Student("John Doe", 40005000));
		
		//Instantiating the second object of "Student" class, and adding it to the ArrayList 
		obj1.add(new Student());
		
		//Instantiating the third object of "Student" class, and adding it to the ArrayList 
		obj1.add(new Student("Jane Doe", 70001000));
		
		//Calling a method with respect to the second instantiated object
		String var1 = obj1.get(1).initStudent("Chidube", "Molokwu", 411111);
		System.out.println(var1);
		
		//Calling a method with respect to the third instantiated object
		String var2 = obj1.get(2).initStudent("Bonaventure", "Molokwu", 411111);
		System.out.println(var2);
	}

}
Driver class: Designing and implementing an OOP class which implements an ArrayList of objects



12.2Structure of an Array of Objects
Technically, an array is an abstract data structure. Thus, during the declaration and instantiation of an array, its variable-identifier holds an object-reference; and in turn, this object-reference points/links to the data elements or contents stored in the array structure (resident in primary memory).

In other words, Array Identifier —→ Array (Object) Reference —→ Array Elements/Contents.

Structure of an Array of objects
Caption: Structure of an Array of objects

Additionally, as earlier mentioned in the past lessons that focused on Arrays and ArrayLists, respectively; the concepts of Shallow Copy and Deep Copy also apply to an Array of objects as well as an ArrayList of objects. Since the default initialization value(s) present in an Array of objects and/or an ArrayList of objects is null; therefore, the following important points are noteworthy, viz:
  1. Within an Array of objects and/or an ArrayList of objects, it is possible for any object to hold or store a null value.
  2. Any cell or unit, within an Array of objects and/or an ArrayList of objects, which holds a null value can neither invoke any method of the object nor access any attribute/variable (or any constant) of the object.
  3. Invocation of a method and/or accessing an attribute/variable (or constant), with reference to a null-value object, will result in an Execution or Runtime error.
  4. For example, the following code snippet yields an Execution/Runtime error with respect to Java programming:
    Student obj1 = null;
    obj1.initStudent("Jane", "Doe", 40005000);

/**
 * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
 * @course ProgrammingLessons (proglessons).
 * @title Arrays and ArrayLists of Objects.
 * @author Dr. Bonaventure Chidube Molokwu.
 */

class Student { //"Container" class with package-private access modifier
	
	private String fName;
	private String lName;
	private int regNum;
	
	
	//DEFAULT Constructor
	public Student () {
		System.out.println("DEFAULT Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
	}
	
	//CUSTOM Constructor
	public Student (String firN, int regN) {
		this.fName = firN;
		this.regNum = regN;
		System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
	}
	
	
	//Method
	public String initStudent (String firN, String lasN, int regN) {
		this.fName = firN;
		this.lName = lasN;
		this.regNum = regN;
		
		return this.regNum + ": " + this.lName + ", " + this.fName;
	}

}


public class StudentDriver { //"Driver" class with public access modifier

	public static void main(String[] args) {
		//Creating an Array comprised of "Student" objects
		//NOTE: Each cell or unit in this Array of "Student" objects is, by default, initialized to null
		Student obj1[] = new Student[2];
		
		//Instantiating the first object in the Array of "Student" objects
		obj1[0] = new Student ("John Doe", 40005000);
		
		//Calling a method/function with reference to the second object in the Array of "Student" objects
		//NOTE: obj1[1] = null; because this is the default, since no instantiation was applied to obj1[1]
		String var1 = obj1[1].initStudent("Chidube", "Molokwu", 411111); //Throws Runtime error
	}

}
Container class and Driver class: Making reference to an object possessing a null value



12.3Practice Exercise
  1. Given the following Java program with respect to OOP paradigm, do you think that this program will compile successfully and execute/run successfully too? If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Arrays and ArrayLists of Objects.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
    
    class Student { //"Container" class with package-private access modifier
    	
    	private String fName;
    	private String lName;
    	private int regNum;
    	
    	
    	public Student () {
    		System.out.println("DEFAULT Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	public Student (String firN, int regN) {
    		this.fName = firN;
    		this.regNum = regN;
    		System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	
    	public String initStudent (String firN, String lasN, int regN) {
    		this.fName = firN;
    		this.lName = lasN;
    		this.regNum = regN;
    		
    		return this.regNum + ": " + this.lName + ", " + this.fName;
    	}
    
    }
    
    
    public class StudentDriver { //"Driver" class with public access modifier
    
    	public static void main(String[] args) {
    		Student obj1[][] = new Student[2][3];
    
    		obj1[0][0] = new Student ("John Doe", 10002000);
    		obj1[0][1] = new Student();
    		obj1[0][2] = new Student("Jane Doe", 30004000);
    		obj1[1][0] = new Student();
    		obj1[1][1] = new Student();
    		obj1[1][2] = new Student();
    		
    		String var1 = obj1[1][3].initStudent("Chidube", "Molokwu", 411111);
    		System.out.println(var1);
    	}
    
    }
    

  2. With respect to the following program in OOP paradigm, do you think that this program will compile successfully and execute/run successfully too? If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Arrays and ArrayLists of Objects.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
    
    class Student { //"Container" class with package-private access modifier
    	
    	private String fName;
    	private String lName;
    	private int regNum;
    	
    	
    	public Student () {
    		System.out.println("DEFAULT Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	public Student (String firN, int regN) {
    		this.fName = firN;
    		this.regNum = regN;
    		System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	
    	public String initStudent (String firN, String lasN, int regN) {
    		this.fName = firN;
    		this.lName = lasN;
    		this.regNum = regN;
    		
    		return this.regNum + ": " + this.lName + ", " + this.fName;
    	}
    
    }
    
    
    public class StudentDriver { //"Driver" class with public access modifier
    
    	public static void main(String[] args) {
    		Student obj1[][] = new Student[2][];
    
    		obj1[0][0] = new Student ("Johnson Doe", 50006000);
    		obj1[0][1] = new Student();
    		obj1[0][2] = new Student("Janet Doe", 70008000);
    		obj1[1][0] = new Student();
    		obj1[1][1] = new Student();
    		obj1[1][2] = new Student();
    		
    		String var1 = obj1[1][2].initStudent("Bonaventure", "Molokwu", 422222);
    		System.out.println(var1);
    	}
    
    }
    

  3. Do you think that the program below will compile successfully and execute/run successfully? If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Arrays and ArrayLists of Objects.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
    
    class Student { //"Container" class with package-private access modifier
    	
    	private String fName;
    	private String lName;
    	private int regNum;
    	
    	
    	public Student (String firN, int regN) {
    		this.fName = firN;
    		this.regNum = regN;
    		System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	
    	public String initStudent (String firN, String lasN, int regN) {
    		this.fName = firN;
    		this.lName = lasN;
    		this.regNum = regN;
    		
    		return this.regNum + ": " + this.lName + ", " + this.fName;
    	}
    	
    	public void outVals () {
    		System.out.println(this.regNum + " (" + this.fName + ", " + this.lName + ")");
    	}
    
    }
    
    
    public class StudentDriver { //"Driver" class with public access modifier
    
    	public static void main(String[] args) {
    		Student obj1[][] = new Student[2][2];
    
    		obj1[0][0] = new Student ("John Doe", 10002000);
    		obj1[0][1] = obj1[0][0];
    		obj1[1][0] = obj1[0][1];
    		obj1[1][1] = obj1[1][0];
    		
    		String var1 = obj1[0][0].initStudent("Alpha", "Bravo", 411111);
    		System.out.println(var1);
    		
    		obj1[1][1].outVals();
    	}
    
    }
    

  4. Do you think that the program below will compile successfully and execute/run successfully? If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Arrays and ArrayLists of Objects.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
    
    class Student { //"Container" class with package-private access modifier
    	
    	private String fName;
    	private String lName;
    	private int regNum;
    	
    	
    	public Student (String firN, int regN) {
    		this.fName = firN;
    		this.regNum = regN;
    		System.out.println("Constructor 1: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	public Student (Student obj) {
    		this.fName = obj.fName;
    		this.lName = obj.lName;
    		this.regNum = obj.regNum;
    		System.out.println("Constructor 2: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	
    	public String initStudent (String firN, String lasN, int regN) {
    		this.fName = firN;
    		this.lName = lasN;
    		this.regNum = regN;
    		
    		return this.regNum + ": " + this.lName + ", " + this.fName;
    	}
    	
    	public void outVals () {
    		System.out.println(this.regNum + " (" + this.fName + ", " + this.lName + ")");
    	}
    
    }
    
    
    public class StudentDriver { //"Driver" class with public access modifier
    
    	public static void main(String[] args) {
    		Student obj1[][] = new Student[2][2];
    
    		obj1[0][0] = new Student ("Janet Johnson", 90001000);
    		obj1[0][1] = obj1[0][0];
    		obj1[1][0] = obj1[0][1];
    		obj1[1][1] = new Student (obj1[1][0]);
    		
    		String var1 = obj1[0][0].initStudent("Charlie", "Delta", 711111);
    		System.out.println(var1);
    		
    		obj1[1][1].outVals();
    	}
    
    }
    

  5. Given the following Java program with respect to OOP paradigm, do you think that this program will compile successfully and execute/run successfully too? If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Arrays and ArrayLists of Objects.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
    
    import java.util.ArrayList;
    
    class Student { //"Container" class with package-private access modifier
    	
    	private String fName;
    	private String lName;
    	private int regNum;
    	
    	
    	public Student () {
    		System.out.println("DEFAULT Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	public Student (String firN, int regN) {
    		this.fName = firN;
    		this.regNum = regN;
    		System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	
    	public String initStudent (String firN, String lasN, int regN) {
    		this.fName = firN;
    		this.lName = lasN;
    		this.regNum = regN;
    		
    		return this.regNum + ": " + this.lName + ", " + this.fName;
    	}
    
    }
    
    
    public class StudentDriver { //"Driver" class with public access modifier
    
    	public static void main(String[] args) {
    		ArrayList<ArrayList<Student>> obj1 = new ArrayList<ArrayList<Student>>();
    		
    		obj1.add(new ArrayList<Student>());
    		obj1.get(0).add(new Student ("Alpha Bravo", 10002000));
    		obj1.get(0).add(new Student ("Charlie Delta", 30004000));
    		obj1.get(0).add(new Student ());
    		obj1.get(0).add(new Student ());
    		obj1.add(new ArrayList<Student>());
    		obj1.get(1).add(new Student ());
    		obj1.get(1).add(new Student ());
    		obj1.get(1).add(new Student ("Zulu Tango", 50006000));
    		
    		String var1 = obj1.get(1).get(1).initStudent("Chidube", "Molokwu", 411111);
    		System.out.println(var1);
    		
    		String var2 = obj1.get(1).get(0).initStudent("Papa", "Yankee", 211111);
    		System.out.println(var2);		
    
    		String var3 = obj1.get(1).get(3).initStudent("Mike", "Quebec", 611111);
    		System.out.println(var3);
    	}
    
    }
    

  6. With respect to the following program in OOP paradigm, do you think that this program will compile successfully and execute/run successfully too? If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Arrays and ArrayLists of Objects.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
    
    import java.util.ArrayList;
    
    class Student { //"Container" class with package-private access modifier
    	
    	private String fName;
    	private String lName;
    	private int regNum;
    	
    	
    	public Student () {
    		System.out.println("DEFAULT Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	public Student (String firN, int regN) {
    		this.fName = firN;
    		this.regNum = regN;
    		System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	
    	public String initStudent (String firN, String lasN, int regN) {
    		this.fName = firN;
    		this.lName = lasN;
    		this.regNum = regN;
    		
    		return this.regNum + ": " + this.lName + ", " + this.fName;
    	}
    
    }
    
    
    public class StudentDriver { //"Driver" class with public access modifier
    
    	public static void main(String[] args) {
    		ArrayList<ArrayList<Student>> obj1 = new ArrayList<ArrayList<Student>>();
    		
    		obj1.add(new ArrayList<Student>());
    		obj1.get(0).add(new Student ("Alpha Bravo", 10002000));
    		obj1.get(0).add(new Student ("Charlie Delta", 30004000));
    		obj1.get(0).add(new Student ());
    		obj1.get(0).add(new Student ());
    		obj1.add(new ArrayList<Student>());
    		obj1.get(1).add(new Student ());
    		obj1.get(1).add(new Student ());
    		obj1.get(1).add(new Student ("Zulu Tango", 50006000));
    		
    		String var1 = obj1.get(1).get(0).initStudent("India", "Oscar", 311111);
    		System.out.println(var1);
    		
    		String var2 = obj1.get(1).get(1).initStudent("Papa", "Yankee", 211111);
    		System.out.println(var2);		
    
    		String var3 = obj1.get(0).get(3).initStudent("Mike", "Quebec", 811111);
    		System.out.println(var3);
    	}
    
    }
    

  7. Do you think that the program below will compile successfully and execute/run successfully? If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Arrays and ArrayLists of Objects.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
    
    import java.util.ArrayList;
    
    class Student { //"Container" class with package-private access modifier
    	
    	private String fName;
    	private String lName;
    	private int regNum;
    	
    	
    	public Student (String firN, int regN) {
    		this.fName = firN;
    		this.regNum = regN;
    		System.out.println("CUSTOM Constructor: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	
    	public String initStudent (String firN, String lasN, int regN) {
    		this.fName = firN;
    		this.lName = lasN;
    		this.regNum = regN;
    		
    		return this.regNum + ": " + this.lName + ", " + this.fName;
    	}
    	
    	public void outVals () {
    		System.out.println(this.regNum + " (" + this.fName + ", " + this.lName + ")");
    	}
    
    }
    
    
    public class StudentDriver { //"Driver" class with public access modifier
    
    	public static void main(String[] args) {
    		ArrayList<ArrayList<Student>> obj1 = new ArrayList<ArrayList<Student>>();
    		
    		obj1.add(new ArrayList<Student>());
    		obj1.get(0).add(new Student ("Alpha Bravo", 10002000));
    		obj1.get(0).add(obj1.get(0).get(0));
    		obj1.get(0).add(obj1.get(0).get(1));
    		obj1.get(0).add(obj1.get(0).get(2));
    		obj1.add(new ArrayList<Student>());
    		obj1.get(1).add(obj1.get(0).get(3));
    		obj1.get(1).add(obj1.get(1).get(0));
    		obj1.get(1).add(obj1.get(1).get(1));
    		
    		String var1 = obj1.get(0).get(0).initStudent("India", "Oscar", 311111);
    		System.out.println(var1);
    		
    		obj1.get(1).get(2).outVals();
    	}
    
    }
    

  8. Do you think that the program below will compile successfully and execute/run successfully? If yes, then state the exact screen output(s). If otherwise, then expantiate on the bug(s) as well as the type of error/bug herein.
    /**
     * @copyright © www.proglessons.com 2024. ALL RIGHTS RESERVED.
     * @course ProgrammingLessons (proglessons).
     * @title Arrays and ArrayLists of Objects.
     * @author Dr. Bonaventure Chidube Molokwu.
     */
    
    import java.util.ArrayList;
    
    class Student { //"Container" class with package-private access modifier
    	
    	private String fName;
    	private String lName;
    	private int regNum;
    	
    	
    	public Student (String firN, int regN) {
    		this.fName = firN;
    		this.regNum = regN;
    		System.out.println("Constructor 1: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	public Student (Student obj) {
    		this.fName = obj.fName;
    		this.lName = obj.lName;
    		this.regNum = obj.regNum;
    		System.out.println("Constructor 2: (" + this.regNum + " -> " + this.fName + ", " + this.lName + ")");
    	}
    	
    	
    	public String initStudent (String firN, String lasN, int regN) {
    		this.fName = firN;
    		this.lName = lasN;
    		this.regNum = regN;
    		
    		return this.regNum + ": " + this.lName + ", " + this.fName;
    	}
    	
    	public void outVals () {
    		System.out.println(this.regNum + " (" + this.fName + ", " + this.lName + ")");
    	}
    
    }
    
    
    public class StudentDriver { //"Driver" class with public access modifier
    
    	public static void main(String[] args) {
    		ArrayList<ArrayList<Student>> obj1 = new ArrayList<ArrayList<Student>>();
    		
    		obj1.add(new ArrayList<Student>());
    		obj1.get(0).add(new Student ("Alpha Bravo", 10002000));
    		obj1.get(0).add(obj1.get(0).get(0));
    		obj1.get(0).add(obj1.get(0).get(1));
    		obj1.get(0).add(obj1.get(0).get(2));
    		obj1.add(new ArrayList<Student>());
    		obj1.get(1).add(obj1.get(0).get(3));
    		obj1.get(1).add(obj1.get(1).get(0));
    		obj1.get(1).add(new Student (obj1.get(1).get(1)));
    		
    		String var1 = obj1.get(0).get(0).initStudent("India", "Oscar", 311111);
    		System.out.println(var1);
    		
    		obj1.get(1).get(2).outVals();
    	}
    
    }
    

  9. TRUE or FALSE: It is impossible for a cell (or unit), within an Array of objects, to contain null?

  10. TRUE or FALSE: Only a linear (or one-dimensional) ArrayList of objects is feasible in Java programming?

Sponsored Ads