16

Currently I have two classes. a classroom class and a School class. I would like to write a method in the School class to call public void setTeacherName(String newTeacherName) from the classroom class.

classroom.java

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;
    }
}

School.java

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 to write a method to add a teacher to the classroom by using the
    // classroom parameter
    // and the teachers name
}  

5 Answers 5

25

You should capitalize names of your classes. After doing that do this in your school class,

Classroom cls = new Classroom();
cls.setTeacherName(newTeacherName);

Also I'd recommend you use some kind of IDE such as eclipse, which can help you with your code for instance generate getters and setters for you. Ex: right click Source -> Generate getters and setters

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't really answer the question. Perhaps it should be a comment on the original question instead of an answer? It's not only valid advice but sound advice as well, though.
@jaydel : There are a good deal of suggestions here, true, but the code part perfectly captures what (I imagine) most people to be looking for.
7

Try this :

public void addTeacherToClassRoom(classroom myClassRoom, String TeacherName)
{
    myClassRoom.setTeacherName(TeacherName);
}

Comments

5
class A{
  public void methodA(){
    new B().methodB();
    //or
    B.methodB1();
  }
}

class B{
  //instance method
  public void methodB(){
  }
  //static method
  public static  void methodB1(){
  }
}

Comments

5

in School,

public void addTeacherName(classroom classroom, String teacherName) {
    classroom.setTeacherName(teacherName);
}

BTW, use Pascal Case for class names. Also, I would suggest a Map<String, classroom> to map a classroom name to a classroom.

Then, if you use my suggestion, this would work

public void addTeacherName(String className, String teacherName) {
    classrooms.get(className).setTeacherName(teacherName);
}

Comments

4

Instead of using this in your current class setClassRoomName("aClassName"); you have to use classroom.setClassRoomName("aClassName");

You have to add the class' and at a point like

yourClassNameWhereTheMethodIs.theMethodsName();

I know it's a really late answer but if someone starts learning Java and randomly sees this post he knows what to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.