67 128
Assignments Done
99,3%
Successfully Done
In November 2018

Answer to Question #60896 in Java | JSP | JSF for Asish gupta

Question #60896
In a class room everyone is very friendly and has bonded with others in a short span of time. During the exams, students will sit along with their friends and will write the exams, which actually resulted in the finding that only a few members in the batch are good at studies and others are not. After getting several complaints from the staff members, the Principal has agreed to change the sitting pattern during the exams for which she has formed a committee. Using a spy, committee was able to get a list of close friends for all the students in the class. Now using this list they want to identify two groups of people such that a person in one group must not be a friend to any other in the same group. Your task is to help the committee
Expert's answer
Student.java:
package sittingpattern;

public class Student {
private int id;
private String name;

public Student(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return id + " " + name;
}
}


SittingPattern.java:
package sittingpattern;

import java.util.ArrayList;
import java.util.List;

public class SittingPattern {
public static void main(String[] args) {
Student st1 = new Student(1, "Bob");
Student st2 = new Student(2, "Anna");
Student st3 = new Student(3, "Frank");
Student st4 = new Student(4, "Elena");
Student st5 = new Student(5, "Kim");

Student[] studentsList = new Student[5];
studentsList[0] = st1;
studentsList[1] = st2;
studentsList[2] = st3;
studentsList[3] = st4;
studentsList[4] = st5;

ArrayList<Student>[] studentsFriends = new ArrayList[5];
for(int i = 0; i < studentsFriends.length; i++) {
studentsFriends[i] = new ArrayList<>();
}

studentsFriends[0].add(st2);
studentsFriends[0].add(st3);

studentsFriends[1].add(st1);
studentsFriends[2].add(st1);

studentsFriends[3].add(st5);
studentsFriends[4].add(st4);

List<Student> firstGroup = new ArrayList<>();
List<Student> secondGroup = new ArrayList<>();

boolean isInFirstGroup;

for(int index = 0; index < studentsFriends.length; index++) {
isInFirstGroup = false;
Student mainStudent = studentsList[index];
if(!firstGroup.contains(mainStudent) && !secondGroup.contains(mainStudent)) {
firstGroup.add(mainStudent);
isInFirstGroup = true;
}
for(int innerIndex = 0; innerIndex < studentsFriends[index].size(); innerIndex++) {
if (!firstGroup.contains(studentsFriends[index].get(innerIndex))) {
if(!secondGroup.contains(studentsFriends[index].get(innerIndex))) {
if(isInFirstGroup) {
secondGroup.add(studentsFriends[index].get(innerIndex));
}
else {
firstGroup.add(studentsFriends[index].get(innerIndex));
}
}
}
}
}

System.out.println("First Group");
System.out.println(firstGroup);

System.out.println("/nSecond Group");
System.out.println(secondGroup);


}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions