I have two classes: classroom
and School
. I want to write a method in the School
class that calls public void setTeacherName(String newTeacherName)
from the classroom
class.
Here’s my classroom
class:
public class classroom {
private String classRoomName;
private String teacherName;
public void setClassRoomName(String newClassRoomName) {
classRoomName = newClassRoomName;
}
public String returnClassRoomName() {
return classRoomName;
}
public void setTeacherName(String newTeacherName) {
teacherName = newTeacherName;
}
public String returnTeacherName() {
return teacherName;
}
}
And my School
class:
import java.util.ArrayList;
public class School {
private ArrayList<classroom> classrooms;
private String classRoomName;
private String teacherName;
public School() {
classrooms = new ArrayList<classroom>();
}
public void addClassRoom(classroom newClassRoom, String theClassRoomName) {
classrooms.add(newClassRoom);
classRoomName = theClassRoomName;
}
// How do I write a method to add a teacher to the classroom using
// the classroom parameter and the teacher’s name?
}
How can I properly call setTeacherName
from classroom
within School
?
The simplest way is to iterate through the classrooms list, find the correct classroom, and set the teacher’s name:
public void assignTeacherToClassroom(String classRoomName, String teacherName) {
for (classroom cls : classrooms) {
if (cls.returnClassRoomName().equals(classRoomName)) {
cls.setTeacherName(teacherName);
System.out.println("Teacher " + teacherName + " assigned to " + classRoomName);
return;
}
}
System.out.println("Classroom " + classRoomName + " not found!");
}
Pros:
- Simple and easy to understand.
- Works well when classrooms are stored in a list.
Cons:
- Performance can be slow if the list is large.
Instead of iterating through a list every time, let’s use a HashMap<String, classroom> for quick access.
Modify your School class to store classrooms in a HashMap:
import java.util.HashMap;
public class School {
private HashMap<String, classroom> classroomMap;
public School() {
classroomMap = new HashMap<>();
}
public void addClassRoom(classroom newClassRoom, String theClassRoomName) {
classroomMap.put(theClassRoomName, newClassRoom);
}
public void assignTeacherToClassroom(String classRoomName, String teacherName) {
classroom cls = classroomMap.get(classRoomName);
if (cls != null) {
cls.setTeacherName(teacherName);
System.out.println("Teacher " + teacherName + " assigned to " + classRoomName);
} else {
System.out.println("Classroom " + classRoomName + " not found!");
}
}
}
Pros:
Cons:
- Uses more memory due to the HashMap.
You can also directly pass a classroom object to the method.
public void assignTeacherToClassroom(classroom cls, String teacherName) {
if (cls != null) {
cls.setTeacherName(teacherName);
System.out.println("Teacher " + teacherName + " assigned to " + cls.returnClassRoomName());
} else {
System.out.println("Invalid classroom reference!");
}
}
Usage Example:
public static void main(String[] args) {
School mySchool = new School();
classroom mathClass = new classroom();
mathClass.setClassRoomName("Math101");
mySchool.addClassRoom(mathClass, "Math101");
mySchool.assignTeacherToClassroom(mathClass, "Mrs. Williams");
System.out.println("Teacher for Math101: " + mathClass.returnTeacherName());
}
Pros:
Cons:
- Requires an already-referenced classroom object.