This site requires JavaScript, please enable it in your browser!
Greenfoot back
hello0209
hello0209 wrote ...

2020/6/2

Need help with programming a random seat generator.

hello0209 hello0209

2020/6/2

#
For my class, I have to programme a random class seat generator. I have already programmed the input of student names. I am having a hard time figuring out how to randomise the name so that it would create randomised groups and show on the screen
public class MyWorld extends World
{
    private Seat seat1;
    Seat[] seatsArray = {new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),
            new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),
            new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat(),new Seat()};
    private String UserInput ="start";
    private String NumberStudents = "30";
    boolean dataEntered = false;
    public MyWorld()
    {    
        // Create a new world with 1000x800 cells with a cell size of 1x1 pixels.
        super(1000, 800, 1); 
    }

    public void act()
    {
        askQuestions();
    }

    public void askQuestions()
    {
        if (!dataEntered)
        {
            NumberStudents = Greenfoot.ask("How many student names would you like to add?");
            int StudentCount = Integer.parseInt(NumberStudents);
            int i = 0;
            while (i < StudentCount)
            {
                UserInput = Greenfoot.ask("What student name would you like to add?"); 
                seatsArray[i].setSeatName(UserInput);
                i=i+1;
            }
            i =0;
            while (i < StudentCount)
            {
                addObject(seatsArray[i],getWidth()/10,getHeight()/4+40*i);
                i=i+1;
            }        
            dataEntered = true;
        }
    }
danpost danpost

2020/6/2

#
Would not shuffling the seats in the array be enough once all the names have been input?
hello0209 hello0209

2020/6/7

#
How would I do this? I have only started programming form two months ago The random generator has to generate different randomize the seat each time. Is it also possible to add constraints to the seats? Like if you didn't want a group of students to sit together or a group of students must sit together.
danpost danpost

2020/6/8

#
hello0209 wrote...
How would I do this? I have only started programming form two months ago
You can sort any Array object, array, using:
java.util.Collections.sort(java.util.Arrays.asList(array));
The random generator has to generate different randomize the seat each time.
The above will.
Is it also possible to add constraints to the seats? Like if you didn't want a group of students to sit together or a group of students must sit together.
It is possible. You will have to be a little inventive it programming that, however.
hello0209 hello0209

2020/6/8

#
Where would I add the code? I'm really sorry, I have completely no idea about coding.
danpost danpost

2020/6/8

#
hello0209 wrote...
Where would I add the code? I'm really sorry, I have completely no idea about coding.
After the array is filled.
You need to login to post a reply.