import java.util.Arrays.*;
int size=100;
//Student[] stud;
//SeatingChart[][] seats;
ArrayList<Student> roster;
void setup()
{
size(500, 500);
roster = new ArrayList<Student>();
roster.add(new Student("Jin", 3));
roster.add(new Student("Alta", 11));
roster.add(new Student("Paul", 9));
roster.add(new Student("Piar", 1));
roster.add(new Student("Terra", 1));
roster.add(new Student("Ayako", 9));
roster.add(new Student("Glen", 2));
roster.add(new Student("Fran", 1));
roster.add(new Student("David", 4));
roster.add(new Student("Danny", 3));
SeatingChart apcs = new SeatingChart(roster, 5, 5);
apcs.removeAbsentStudents(6, roster);
apcs.drawChart(roster);
}
public class Student
{
private String name;
private int absenceCount;
public Student (String nm)
{
name = nm;
}
public Student (String nm, int count)
{
name = nm;
absenceCount = count;
}
public void setAbsenceCount(int ac)
{
absenceCount = ac;
}
public String getName()
{
return name;
}
public int getAbsenceCount()
{
return absenceCount;
}
public String toString()
{
return name + " " + absenceCount + " ";
}
}
public class SeatingChart
{
private Student[][] seats;
public SeatingChart(ArrayList<Student> studentList, int rows, int cols)
{
seats = new Student[rows][cols];
int row = 0, col = 0;
for (int i = 0; i < studentList.size(); i++)
{
seats[row][col] = studentList.get(i);
row++;
if (row == rows)
{
row = 0;
col++;
}
}
}
void drawChart(ArrayList<Student> newSeatingChart)
{
for (int i=0; i<6; i++) {
for (int j=0; j<6; j++) {
stroke(0);
fill(255);
rect(i*size, j*size, size, size);
fill(0);
stroke(0);
for (int k=0; k<newSeatingChart.size(); i++) {
text(newSeatingChart.get(i).name, i*size, j*size);
text(newSeatingChart.get(i).absenceCount, i*size+40, j*size);
}
}
}
}
/** Removes students who have more than a given number of absences from the
* seating chart, replacing those entries in the seating chart with null
* and returns the number of students removed.
* @param allowedAbsences an integer >= 0
* @return number of students removed from seats
* Postcondition:
* - All students with allowedAbsences or fewer are in their original positions in seat
* - No student in seats has more than allowedAbsences absences.
* - Entries without students contain null.
*/
public int removeAbsentStudents(int allowedAbsences, ArrayList<Student> l)
{
int removedCount = 0;
for (int r = 0; r < seats.length; r++)
{
for (int c = 0; c < seats[0].length; c++)
{
l.set(r, seats[r][c]);
if (seats[r][c] != null && seats[r][c].getAbsenceCount() > allowedAbsences)
{
seats[r][c] = null;
removedCount++;
}
}
}
return removedCount;
}
/** Rearrange students in the seating chart in alphabetical order
* with a new set of rows and columns
* and returns the new seating chart. The original seating chart
* is not affected.
* @param rows - may be different from the original number of rows
* @param col - may be different from the original number of columns
* Postcondition:
* - All students with be in the new seating chart
* - The original seating chart is the same
*/
public Student[][] rearrangedStudents(int rows, int cols)
{
Student[][] updatedList=new Student[rows][cols];
for (int i=0; i<seats[0].length; i++) {
for (int j=0; j<seats.length; j++) {
if (seats[i][j].name.charAt(0)<seats[i+1][j+1].name.charAt(0)) {
seats[i][j]=seats[i+1][j+1];
seats[i+1][j+1]=seats[i][j];
}
}
}
return updatedList;
}
public String toString()
{
return "";
}
}
Проблема в том, что после выполнения. Он отобразит заголовок класса ученика и выделит строку private String name ;. Я не уверен, почему это так, как класс. Я также правильно использовал его. как вы можете видеть с конструкторами. Так может, кто-то исправить этот код или сказать мне, как я должен это исправить?nullpointerexception при обработке при запуске этой программы
Пожалуйста, добавьте трассировку стека к вопросу. – KevinO
@KevinO Что такое трассировка стека? –
Примечание: используется редактор обработки, который не дает трассировку стека. Пользователь фактически предоставил всю имеющуюся у них информацию. Это не вопрос Java, это вопрос обработки. У обработки есть собственный редактор, и об этом мы и говорим. –