Creating a Java Classroom Class with Array Initialization
Constructor with Array Initialization
Write a Java program to create a class called Classroom with instance variables className and students (an array of strings). Implement a parameterized constructor that initializes these variables. Print the values of the variables.
Sample Solution:
Java Code:
Classroom.java
// Define the Classroom class
public class Classroom {
// Private instance variables
private String className;
private String[] students;
// Parameterized constructor that initializes className and students
public Classroom(String className, String[] students) {
// Initialize instance variables with provided parameters
this.className = className;
this.students = students;
}
// Method to print the values of className and students
public void printClassroom() {
System.out.println("Class Name: " + className);
System.out.print("Students: ");
for (String student : students) {
System.out.print(student + " ");
}
System.out.println();
}
// Main method to test the Classroom class
public static void main(String[] args) {
// Create an array of student names
String[] studentArray = {"Andrine", "Ruslan", "Martin"};
// Create a Classroom object using the parameterized constructor
Classroom classroom = new Classroom("Science 101", studentArray);
// Print the values of the instance variables
classroom.printClassroom();
}
}
Output:
Class Name: Science 101 Students: Andrine Ruslan Martin
Explanation:
- Define the Classroom class:
- The Classroom class is defined with the keyword class.
- Private instance variables:
- The Classroom class has two private instance variables: className (a String) and students (an array of String).
- Parameterized constructor that initializes className and students:
- The parameterized constructor Classroom(String className, String[] students) is defined.
- Inside the constructor:
- Initialize className: The instance variable className is initialized with the provided parameter.
- Initialize students: The instance variable students is initialized with the provided array of student names.
- Method to print the values of className and students:
- The printClassroom method is defined to print the values of the instance variables className and students.
- It prints the class name and iterates over the students array to print each student's name.
- Main method:
- The main method is defined to test the Classroom class.
- Create an array of student names: An array of String called studentArray is created with some student names.
- Create a Classroom object: A Classroom object (classroom) is created using the parameterized constructor, passing the class name and the array of student names as arguments.
- Print the values: The printClassroom method is called on the classroom object to print the values of the instance variables.
Note on Constructors:
In the above exercise, the constructors for the Classroom class work by:
- Parameterized Constructor: The constructor Classroom(String className, String[] students) initializes the instance variables with the provided parameters, including an array of strings.
- Array Initialization: By initializing the students array within the constructor, the class can handle multiple student names as part of its state.
- Encapsulation: The use of a constructor to initialize instance variables ensures that the Classroom class is properly encapsulated, with its state (class name and students) set at the time of object creation. This approach promotes data integrity and clear object initialization.
Java Code Editor:
Improve this sample solution and post your code through Disqus.
Java Constructor Previous: Creating a Java Point Class with Overloaded Constructors.
Java Constructor Next: Implementing the Singleton Pattern in Java.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics