Skip to main content

Java Help Arrays/ArrayLists

3 replies [Last post]
soccerguy41315
Offline
Joined: 2009-01-09

Anybody know how to complete this assignment? I am completely lost haha.
Any help would be greatly appreciated :)

Arrays

Mrs. Montez and Mr. Mufasa have decided to combine their classes in a joint project. To do this they will form partners with one student from Mrs. Montez’s class paired with one student from Mr. Mufasa’s. The students in Mrs. Montez’s class are Troy, Gabriella, Sharpay, Ryan, Chad, Taylor, and Kelsi. The students in Mr. Mufasa’s class are Simba, Scar, Nala, Pumbaa, Timon, Rafiki, and Zazu. They formed partners in the following way: They lined each of their classes up in the previous listed order. Then Mrs. Montez took her first student in line, Troy, and had Mr. Mufasa randomly choose one of his students. That student would then switch places in line with the first student so that the first students in each line would be partners. Then Gabriella would be randomly paired with a student who was moved to the second spot in Mr. Mufasa’s line. This is repeated until the last person in Mrs. Montez’s line is paired with the last person in Mr. Mufasa’s line. You are to write a program which will make the pairs and print them out. To do this you will need to write a Partners class with the following specs:

• Two private instance variables:
o String[] students which is an array which holds the names of Mrs. Montez’s students.
o ArrayList lions which holds the names of the students in Mr. Mufasa’s class.
• A constructor.
• A method public void readInClasses() which puts the names of the students in both classes into the appropriate array.
• A method private void swap(int i, int j) which switches the ith student in Mr. Mufasa’s line with the jth student in line.
• A method public void findPartner(int i) which randomly finds a partner for the ith student in Mrs. Montez’s line and moves that person in Mr. Mufasa’s line to the correct spot. .Don’t worry if you pick the same random number twice or more. Just go ahead and make the switch.
• A method public void matchStudents() which matches each student (using a loop) in Mrs. Montez’s class with a student in Mr. Mufasa’s class.
• A method public void printPartners() which prints out the partners as in the example below:

Troy - Zazu
Gabriella - Simba
Sharpay - Rafiki
Ryan - Pumbaa
Chad - Scar
Taylor - Nala
Kelsi - Timon
Press any key to continue...

Your program must run with the provided PartnersTest class. You may not make any changes to this class. Whenever possible, use method calls in your code. Do not rewrite code.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
walterln
Offline
Joined: 2007-04-17

Start with small steps, then if you're stuck ask a specific question. The assignment already gives some specific you should implement.
If you're stuck with an algorithm, solve it with pen and paper first.

soccerguy41315
Offline
Joined: 2009-01-09

class Partners {
private String[] students;
private ArrayList lions;
public Partners() {
lions = new ArrayList(14);
}
public void readInClasses() {
students[0] = new String("Troy");
students[1] = new String("Gabriella");
students[2] = new String("Sharpay");
students[3] = new String("Ryan");
students[4] = new String("Chad");
students[5] = new String("Taylor");
students[6] = new String("Kelsi");
lions.add("Simba");
lions.add("Scar");
lions.add("Nala");
lions.add("Pumbaa");
lions.add("Timon");
lions.add("Rafiki");
lions.add("Zazu");
}
private void swap(int i, int j) {
String firstStudent = lions.get(i);
String secondStudent = lions.get(j);
lions.set(i, secondStudent);
lions.set(j, firstStudent);
}
public void findPartner(int i) {

}
public void matchStudents() {

}
public void printPartners()

}

That is what I have done so far. I am stuck though, I am not too sure how to do the rest of it. Anybody wanna help out? I would greatly appreciate it! :D

walterln
Offline
Joined: 2007-04-17

Please post code between [ code ] [ /code ] tags without spaces.

For the findPartner method, look at the Java [url=http://java.sun.com/javase/6/docs/api/]API[/url] and search for random. Perhaps you'll find a useful class. If I read the requirement right, you can re-use your swap method here as well.