69 799
Assignments Done
Successfully Done
In February 2019

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
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;

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

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<>();




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)) {
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) {
else {

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

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


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!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions