Randomize Groups

To randomly assign students to groups that have different sizes, we have here a Student object and a Group object, and some starter code to test them.

We would like to add to the tester to make sure the methods are working well, and eventually print the randomly assigned groups two ways:

  1. The student list showing which group each student is in
  2. The group list showing who is in each group

The format to make the wiki know who is in group is

@A: gabriela,  jamesb
@B: jackw, andrews

You have a choice in your approach. You could go through the students, randomly choose a group, and if the group is not full, add the student to the group, or else you could go through the groups, randomly choose a student, and if the student is not in a group, assign the student to that group.

Student.java

public class Student
{
    // instance variables - replace the example below with your own
    private String name;
    private boolean hasGroup;
    private String groupName;


    /**
     * Constructor for objects of class Student
     */
    public Student(String name)
    {
        this.name=name;
        hasGroup=false;
        groupName="";
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name=name;
    }
    public boolean inGroup()
    {
        return this.hasGroup;
    }

    public void setGroupName(String groupName)
    {
        this.groupName=groupName;
        hasGroup=true;
    }
    public void removeFromGroup()
    {
        this.groupName="";
        hasGroup=false;
    }
    public String getGroupName()
    {
        return this.groupName;
    }
    public String toString()
    {
        String result =  name;
        if (inGroup() )
            result+=" is in group "+groupName;
        return result;
    }

}

Group.java

public class Group
{
    private String name;
    private int max;
    private int size;
    private Student[] members;

    public Group(String name)
    {
        this.name=name;
        this.max=3;
        members = new Student[max];
    }
    public Group(String name, int max)
    {
       this.max=max;
       this.name=name;
       members = new Student[max];
    }

    public void setMax(int max)
    {
        this.max=max;
        members = new Student[max];
    }
    public boolean isFull()
    {
        return size >= max;
    }
    public int getMax() {
        return max;
    }
    public int getSize()
    {
        return size;
    }
    public void remove(Student member)
    {
       int i=0;
       while (!members[i].equals(member) && i<max )
       {
           i++;
       }
       if (i>=max) return;
       for( int j=i; j<max-1; j++)
       {
           members[j]=members[j+1];
       }
       members[i]=null;


    }
    public void add(Student member)
    {
        if (this.isFull() ) 
            return;
        this.members[size] = member;
        size++;

    }
}

GroupTester.java

public class GroupTester
{
    public static void main (String[] args)
    {
        Group[] groups = new Group[13];
        groups[0]=new Group("A",2);
        groups[1]=new Group("B",2);
        groups[2]=new Group("C",2);
        groups[3]=new Group("D",2);
        groups[4]=new Group("E",3);
        groups[5]=new Group("F",3);
        groups[6]=new Group("G",2);
        groups[7]=new Group("H",2);
        groups[8]=new Group("I",2);
        groups[9]=new Group("J",2);
        groups[10]=new Group("K",2);
        groups[11]=new Group("L",2);
        groups[12]=new Group("M",2);

        String[] names = {"nicka",
        "gabriela",
        "shawntb",
        "jamesb",
        "martinb",
        "lorenzod",
        "ryand",
        "alexig",
        "spencerg",
        "christiang",
        "junh",
        "minjuneh",
        "cyrush",
        "colink",
        "anthonyk",
        "christopherl",
        "joshl",
        "evanl",
        "phillipm",
        "christianm",
        "ajayn",
        "andrewp",
        "andrews",
        "ryans",
        "michaelv",
        "bowenw",
        "jackw",
        "cameronw" };

        Student[] students = new Student[names.length];
        for (int i=0; i< students.length; i++)
        {
            students[i]=new Student(names[i]);
        }
        /// your code here

    }
}