Untitled
unknown
java
a month ago
2.9 kB
1
Indexable
Never
package seating; import java.util.*; public class Seating { public static void main(String[] args) { List<Student> roster = new ArrayList<Student>(); roster.add(new Student("Karen",3)); roster.add(new Student("Liz",1)); roster.add(new Student("Paul",4)); roster.add(new Student("Lester",1)); roster.add(new Student("Henry",5)); roster.add(new Student("Renee",9)); roster.add(new Student("Glen",2)); roster.add(new Student("Fran",6)); roster.add(new Student("David",1)); roster.add(new Student("Danny",3)); SeatingChart chart = new SeatingChart(roster,3,4); chart.print(); System.out.println("num removed = " + chart.removeAbsentStudents(4)); chart.print(); } } class SeatingChart { private Student[][] seats; SeatingChart(List<Student> studentList,int rows,int cols) { seats = new Student[rows][cols]; int studentIndex = 0; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (studentIndex < studentList.size()) { seats[r][c] = studentList.get(studentIndex); studentIndex++; } else { seats[r][c] = null; } } } } public int removeAbsentStudents(int allowedAbsences) { int removedCount = 0; for (int r = 0; r < seats.length; r++) { for (int c = 0; c < seats[r].length; c++) { Student currentStudent = seats[r][c]; if (currentStudent != null && currentStudent.getAbsenceCount() > allowedAbsences) { seats[r][c] = null; // Remove the student from the seating chart removedCount++; // Increment the number of removed students } } } return removedCount; // Return the total number of removed students } public void print() { for (int rowsIndex=0;rowsIndex<seats.length;rowsIndex++) { for (int colsIndex=0;colsIndex<seats[rowsIndex].length;colsIndex++) { if (seats[rowsIndex][colsIndex] != null) System.out.print(seats[rowsIndex][colsIndex].getName()); else System.out.print("EMPTY"); System.out.print(" "); } System.out.println(""); } } } class Student { private String name; private int absenceCount; Student(String _name,int _absenceCount) { name = _name; absenceCount = _absenceCount; } public String getName() { return(name); } public int getAbsenceCount() { return(absenceCount); } }
Leave a Comment