We are about to switch to a new forum software. Until then we have removed the registration on this forum.
import java.util.Arrays.*;
int size=100;
Student[][] stud;
void setup()
{
size(500, 500);
int num=0;
for (int i=0; i<20; i++) {
for (int j=0; j<20; j++) {
num++;
int rand=(int)(random(1));
if (rand==1)
stud[i][j]=new Student("stud"+num, (int)(random(6)));
else
stud[i][j]=new Student("name only");
}
}
drawChart(stud);
// Your code goes here to create your Student[] array,
// Student[][] twoDarray and SeatingChart object.
// Your code to test the Student and SeatingChart class also goes here.
}
/** the drawChart method should display the seating chart data
* with students in the correct rows and columns
*/
void drawChart(Student[][] newSeatingChart)
{
for (int i=0; i<width/size; i++) {
for (int j=0; j<height/size; j++) {
stroke(0);
fill(255);
rect(i*size, j*size, size, size);
text("stud", i*size, j*size);
}
}
}
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;
}
/** Returns the name of this Student. */
public String getName()
{
return name;
}
/** Returns the number of times this Student has missed class. */
public int getAbsenceCount()
{
return absenceCount;
}
public String toString()
{
return name + " " + absenceCount + " ";
}
}
public class SeatingChart
{
/** seats[r][c] represents the Student in row r and column c in the classroom. */
private Student[][] seats;
/** Creates a seating chart with the given number of rows and columns from the students in
* studentList. Empty seats in the seating chart are represented by null.
* @ param rows the number of rows of seats in the classroom
* @ param cols the number of columns of seats in the classroom
* Precondition: rows > 0; cols > 0;
* rows * cols >= studentList.size()
* Postcondition:
* - Students appear in the seating chart in the same order as they appear
* in studentList, starting at seats[0][0].
* - seats is filled column by column from studentList, followed by any
* empty seats (represented by null).
* - studentList is unchanged.
*/
public SeatingChart(Student[] studentArray, int rows, int cols)
{
// your code here to initialize the seats array with the values passed in
seats=new Student[rows][cols];
}
public Student[][] getSeats() {
return seats;
}
/** 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)
{
int removed=0;
// your code goes here to removeAbsentStudents from seats
for (int i=0; i<seats[0].length; i++) {
for (int j=0; j<seats.length; j++) {
if (seats[i][j].absenceCount>=allowedAbsences)
removed++;
}
}
return removed;
}
/** 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()
{
// your code here
return " ";
}
}
here is the error
Exception in thread "Animation Thread" java.lang.NullPointerException
at seatingChartStudentVersion.setup(seatingChartStudentVersion.java:33)
at processing.core.PApplet.handleDraw(PApplet.java:2377)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
Answers
Please do not post code w/
/*
lest it gets all GREEN! :-& Replace all of them w//**
instead. *-:)@gotoloop thanks, I cant believe I forgot that XD
@gotoloop can you explain why
returns an error
Exception in thread "Animation Thread" java.lang.NullPointerException
it is in the setup method
figured out, the array was not full so returned that error
Each sub-array (2nd inner dimension) needs to be created.
Something like this
new Student[ROOMS][ALUMS];
instantiates both dimensions.However,
new Student[ROOMS][];
only creates the 1st outer dimension.@gotoloop I have a new error of a nullpointerexception
@gotoloop can you please correct my code
If I comment out
apcs.removeAbsentStudents(1, roster);
, your sketch works. :-\"ok, so it is something wrong with that?
@gotoloop I updated the code entirely
however, still a null pointer at the chart
Line Number?
Use println to find the var that's causing this
it helps in line 166
to insert -1
not sure why though
somehow, the array is smaller than you expect.
maybe because lines 22 and 23?