0

This is possibly a simple problem but I am having issues. I have 3 classes. A Student class that contains a setmethod:

public boolean setName(String fname)
{
        this.name = fname;
        return true;
}

A TestClass with a main that passes a string to the setmethod

static Student action;

public static void main(String[] args)
{
        action.setName("John");
}

and a Classroom class that contains an add student method.

public boolean add(Student newStudent)
    {
            ???
            return true;
    }

I know how to create and add an object to an array list but I am confused as to how to do it using 3 separate classes. My array list init is:

List<Student> studentList = new ArrayList<Student>();

How would I associate the attributes(name in this case) being set in the Student class to a new object being created in the Classroom class?

1
  • 1
    all you need to do is create a new Student() (which is already passed on to you in that add method) and add it to your studentList Commented Mar 14, 2013 at 2:36

3 Answers 3

2

I think you should follow the principle of least surprise i.e., make sure the methods you create do exactly what you need them to. In your example, your setName and add methods return a boolean for some reason. Typically, setter methods don't return booleans, unless you're doing some sort of a DB insert like operation and want to make sure that your object was actually inserted.

Also, a typical idiom would be to create the controller object (i.e., TestClass) in the static main method and then initialize whatever is necessary in its constructor or by calling methods on the created TestClass object inside the main method itself.

Here's a solution.

public class TestClass {    
    private Classroom c;

    public TestClass() {
        c = new Classroom();
        private Student s = new Student();
        s.setName("John");
        c.add(s);
    }

    public static void main(String[] args)
    {
        new TestClass();
    }
}

public Classroom {
    private List<Student> studentList;

    public Classroom() {
         studentList = new ArrayList<Student>();
    }

    public boolean add(Student newStudent) {
         studentList.add(newStudent);
         return true; //not sure why you're returning booleans
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help this explained a lot.
1

Your student class looks good, your classroom class should contain a list of students, and ways to add/remove/list students. Your test class should create new students which you can then add to your classroom.

1 Comment

What parameter do I pass to the add method from my main. classroomAction.add(??)
1

I assume you want a Test Class which is a test event like midterm or final, and you want to put Student and ClassRoom in the Test Class.

So you get three classes, and they're all related. If this is the case you want, then you can do this. (This is a very simplified version!!)

class Test{
    String name;
    HashMap<ClassRoom, ArrayList<Student> > roomMap;
    // ... other functions
}


// you can use ClassRoom as key and Student list as value.
// A ClassRoom key will return a value which is a Student list containg students who are going to take a test in that room.
 public static void main(String[] args) {
    Test test = new Test();
    test.name = "MidTerm";
    test.roomMap = new HashMap<ClassRoom, ArrayList<Student> >();
    ArrayList<Student> students = new ArrayList<Student>();
    students.add(new Student("John"));
    students.add(new Student("Mark"));
    ClassRoom room = new Room("R123");
    test.roomMap.put(room, student);

    // If there are a lot of test, then you could manage test in an ArrayList in your main.
    ArrayList<Test> testList = new ArrayList<Test> ();
    testList.add(test);
}

Maybe you could give more details of your requirement.

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.