how to transfer methods over to another class

i have 2 classes. one is an extends. how can i move the superclass' method "move()" to the subclass

Edit: I have used super but it doesn't transfer the method and sits still.

Answers

  • edited February 2016

    Keyword extends already transfers all parents class' members to its "heir" subclass.
    If method move() belongs to the class being extended, it's gonna be accessed by its now subclass too.

  • edited February 2016

    @gotoloop im confused. the subclass doesn't need the move method defined?

     /**Name: 
     * Date:
     * Description: Starfield design... moving stars and 1 Oddball */
    //ArrayList<NormalParticle> particles=new ArrayList<NormalParticle>();
    //ArrayList<OddballParticle> particles=new ArrayList<OddballParticle>();
    ArrayList<Particle> particles=new ArrayList<Particle>();
    public void setup()
    {
      size(800, 800);
      /**for (int i=0; i<20; i++) {
       particles.add(new JumboParticle());
       }*/
      /**for (int i=0; i<20; i++) {
       particles.add(new NormalParticle());
       }*/
      /**for (int i=0; i<20; i++) {
       particles.add(new OddBallParticle());
       }*/
      for (int i=0; i<5; i++) {
        particles.add(new JumboParticle());
      }
      for (int i=0; i<40; i++) {
        particles.add(new NormalParticle());
      }
      for (int i=0; i<15; i++) {
        particles.add(new OddballParticle());
      }
    }
    
    public void draw()
    {
      background(255);
      for (int i=0; i<particles.size (); i++) {
        particles.get(i).move();
        particles.get(i).show();
      }
    }
    
    public interface Particle
    {
      public void move();
      public void show();
    }
    
    
    public class NormalParticle implements Particle // inheritance here
    {
      double x, y;
      color c;
      double speed;
      double angle;
      public NormalParticle() {
        x=(double)(random(10, width-10));
        y=(double)(random(10, height-10));
        speed=(double)(random(25));
        angle=(double)(random(360));
        c=color(random(255), random(255), random(255));
      }
      /**public void setX(double m) {
        x=m;
      }*/
      public void show() {
        fill(c);
        noStroke();
        ellipse((float)(x), (float)(y), (float)(10), (float)(10));
      }
      public void move() {
        if ((x<width-5&&x>5)&&(y<height-5&&y>5))
          angle=-angle;
        x+=(double)(cos((float)(angle*speed)));
        y+=(double)(sin((float)(angle*speed)));
      }
    }
    
    
    public class OddballParticle extends NormalParticle implements Particle // extends a class and implements an interface
    {
      double x, y;
      color c;
      double speed;
      double angle;
      public OddballParticle() {
        x=(double)(random(10, width-10));
        y=(double)(random(10, height-10));
        speed=(double)(random(25));
        angle=(double)(random(360));
        c=color(random(255), random(255), random(255));
      }
      public void show() {
        fill(c);
        noStroke();
        ellipse((float)(x), (float)(y), (float)(25), (float)(10));
      }
      public void move() {
        if ((x<width-5&&x>5)&&(y<height-5&&y>5))
          angle=-angle;
        x+=(double)(cos((float)(angle*speed)));
        y+=(double)(sin((float)(angle*speed)));
      }
    }
    
    public class JumboParticle extends NormalParticle implements Particle // extends a class and implements an interface 
    {
      double x, y;
      color c;
      double speed;
      double angle;
      public JumboParticle() {
        x=(double)(random(10, width-10));
        y=(double)(random(10, height-10));
        speed=(double)(random(25));
        angle=(double)(random(360));
        c=color(random(255), random(255), random(255));
      }
      public void show() {
        fill(c);
        noStroke();
        ellipse((float)(x), (float)(y), (float)(50), (float)(50));
      }
      public void move() {
       if ((x<width-5&&x>5)&&(y<height-5&&y>5))
       angle=-angle;
       x+=(double)(cos((float)(angle*speed)));
       y+=(double)(sin((float)(angle*speed)));
       }
    }
    
  • edited February 2016 Answer ✓

    An important tip: Do not use /*. Rather use /** in order to avoid all green posts! :-&

    Since you've finally posted your code now, I can see that you've gone beyond simple inheritance, but it's also using polymorphism on both show() & move() methods!

    That is, you @Override both inherited methods by re-implementing them in subclasses OddballParticle & JumboParticle: http://docs.Oracle.com/javase/8/docs/api/java/lang/Override.html

    Seems like both polymorphed move() methods' implementations are exactly the same as the original inherited version. You can simply erase them from both subclasses, as they're redundant. :-\"

    Now both polymorphic method show() have diff. implementations from the original 1.
    So you should keep them. But it's a good advise to annotate them w/ @ Override: O:-)

    @ Override void show() {}

    However I see a probable bug that you're also re-declaring inherited fields! :-SS

    double x, y;
    double speed, angle;
    color c;
    

    They already exist in the original parent class NormalParticle.
    You're simply shadowing them inside your subclasses! #-o
    That is, w/ such configuration, there are, for example, 2 fields x: this.x & super.x! ~:>

    When dealing w/ extends & implements we gotta pay attention to what already exists in the whole inheritance chain lest we end up w/ buggy redundant behavior! =;

  • edited February 2016

    @gotoloop thank you very much for your explanation. however, after erasing both move methods from the subclasses, they no longer move...

  • edited February 2016

    Have you also removed your field redeclarations???
    What may be happening is that Java doesn't know which 1 to use! 8-}

  • edited February 2016

    While methods are overridden, fields are overshadowed instead! L-)
    If a method happens to be static, it's also overshadowed when re-implemented inside a subclass! 8-X

  • i just removed them from the subclasses.... what should i have done?

  • Post your update code so we can see how it is going now...

  • edited February 2016
    /**Name:
     * Date: 
     * Description: Starfield design... moving stars and 1 Oddball */
    //ArrayList<NormalParticle> particles=new ArrayList<NormalParticle>();
    //ArrayList<OddballParticle> particles=new ArrayList<OddballParticle>();
    ArrayList<Particle> particles=new ArrayList<Particle>();
    public void setup()
    {
      size(800, 800);
      /**for (int i=0; i<20; i++) {
       particles.add(new JumboParticle());
       }*/
      /**for (int i=0; i<20; i++) {
       particles.add(new NormalParticle());
       }*/
      /**for (int i=0; i<20; i++) {
       particles.add(new OddBallParticle());
       }*/
      for (int i=0; i<5; i++) {
        particles.add(new JumboParticle());
      }
      for (int i=0; i<40; i++) {
        particles.add(new NormalParticle());
      }
      for (int i=0; i<15; i++) {
        particles.add(new OddballParticle());
      }
    }
    
    public void draw()
    {
      background(255);
      for (int i=0; i<particles.size (); i++) {
        particles.get(i).move();
        particles.get(i).show();
      }
    }
    
    public interface Particle
    {
      public void move();
      public void show();
    }
    
    
    public class NormalParticle implements Particle // inheritance here
    {
      double x, y;
      color c;
      double speed;
      double angle;
    
      public NormalParticle() {
        x=(double)(random(10, width-10));
        y=(double)(random(10, height-10));
        speed=(double)(random(25));
        angle=(double)(random(360));
        c=color(random(255), random(255), random(255));
      }
      /**public void setX(double m) {
        x=m;
      }*/
    
      public void show() {
        fill(c);
        noStroke();
        ellipse((float)(x), (float)(y), (float)(10), (float)(10));
      }
    
      public void move() {
        if ((x<width-5&&x>5)&&(y<height-5&&y>5))
          angle=-angle;
        x+=(double)(cos((float)(angle*speed)));
        y+=(double)(sin((float)(angle*speed)));
      }
    }
    
    
    public class OddballParticle extends NormalParticle implements Particle // extends a class and implements an interface
    {
      double subx, suby;
      color subc;
      double subspeed;
      double subangle;
    
      public OddballParticle() {
        subx=(double)(random(10, width-10));
        suby=(double)(random(10, height-10));
        subspeed=(double)(random(25));
        subangle=(double)(random(360));
        subc=color(random(255), random(255), random(255));
      }
    
      @ Override public void show() {
        fill(c);
        noStroke();
        ellipse((float)(subx), (float)(suby), (float)(25), (float)(10));
      }
    
      public void move() {
        if ((subx<width-5&&subx>5)&&(suby<height-5&&suby>5))
          subangle=-subangle;
        subx+=(double)(cos((float)(subangle*subspeed)));
        suby+=(double)(sin((float)(subangle*subspeed)));
      }
    }
    
    public class JumboParticle extends NormalParticle implements Particle // extends a class and implements an interface 
    {
      private double subx, suby;
      private color subc;
      private double subspeed;
      private double subangle;
    
      public JumboParticle() {
        subx=(double)(random(10, width-10));
        suby=(double)(random(10, height-10));
        subspeed=(double)(random(25));
        subangle=(double)(random(360));
        subc=color(random(255), random(255), random(255));
      }
    
      @ Override public void show() {
        fill(c);
        noStroke();
        ellipse((float)(subx), (float)(suby), (float)(50), (float)(50));
      }
    
      public void move() {
        if ((subx<width-5&&subx>5)&&(suby<height-5&&suby>5))
          subangle=-subangle;
        subx+=(double)(cos((float)(subangle*subspeed)));
        suby+=(double)(sin((float)(subangle*subspeed)));
      }
    }
    

    i removed the move methods and that didn't work. What do I do about the move methods?

  • @gotoloop what do you want me to do with the move methods exactly?

  • edited February 2016

    Your code post is half green! :@) And it seems like you've completely skipped what I had already warned about @: https://forum.Processing.org/two/discussion/comment/60953/#Comment_60953

    Have you also removed your field redeclarations???
    What may be happening is that Java doesn't know which 1 to use! 8-}

    I suggest you to re-read what you had marked as answer: https://forum.Processing.org/two/discussion/14779/#Comment_60951

    However I see a probable bug that you're also re-declaring inherited fields! :-SS

    They already exist in the original parent class NormalParticle.
    You're simply shadowing them inside your subclasses! #-o
    That is, w/ such configuration, there are, for example, 2 fields x: this.x & super.x! ~:>

    When dealing w/ extends & implements we gotta pay attention to what already exists in the whole inheritance chain lest we end up w/ buggy redundant behavior! =;

  • edited February 2016 Answer ✓

    well, first of all maybe you don't need a parents class' and "heir" subclass. For smaller projects, it's ok to have just normal classes. When they are similar in some ways, who cares?

    Then, you could look at interface which is useful when you want to have different classes in one ArrayList: https://forum.processing.org/two/discussion/4551/arraylist-with-different-object-types#latest

    And then you can look at extends - the help is pretty straight forward :

    https://processing.org/reference/extends.html

    Allows a new class to inherit the methods and data fields (variables and constants) from an existing class. In code, state the name of the new class, followed by the keyword extends and the name of the base class. The concept of inheritance is one of the fundamental principles of object oriented programming.

    Note that in Java, and therefore also Processing, you cannot extend a class more than once. Instead, see implements.

    • to extend means to build something (B) upon something else that is already there (A).

    So class B builds upon A. Therefore everything that is in A automatically is in B and can be used that way.

    Here is an example: A is class Dot, B is DrawDot :

    DrawDot dd1 = new DrawDot(50, 80);
    
    void setup() { 
      size(200, 200);
      background(100);
    } 
    
    void draw() {
      background(100); 
      dd1.display();
    } 
    
    class Dot { 
      int xpos, ypos;
    
      void display() {
        ellipse(xpos, ypos, 200, 200);
      }
    } 
    
    class DrawDot extends Dot {
      DrawDot(int x, int y) {
        xpos = x;
        ypos = y;
      }
    }
    

    as you can see, although dd1 is of class DrawDot it can use display() from class Dot as if display() were part of DrawDot. So extends copies all methods from Dot into DrawDot if you want to put it that way.

    Best, Chrisir ;-)

  • (i have fixed the comments)

  • edited February 2016

    @chrisir @gotoloop I'm still confused. I have tried using the super.move() method in a sub class. it is not working...

  • edited February 2016

    for instance,

        ArrayList<Animal> animals;   
        Zookeeper zookeeper;
    
    void setup()
    {
      size(600, 600);
      zookeeper = new Zookeeper(width/2, height - 20, color(0, 100, 0));
      // create or initializa your array or ArrayList of animals
      animals=new ArrayList<Animal>();
      animals.add(new Elephant(width-10, height/2, color(random(255)), 15, 10, width-100, width-10, height/2-60, height-10));
      // create and add the first set of animals - the deer to your array or ArrayList
      // create apes and elephants and add them to the array or ArrayList
    }
    
    void draw()
    {
      background(127);
      int stx = 0;
    
      // loop over all the animals in your array or ArrayList and call
      // each animals move and display methods in 2 different loops...
    
    
      // draw lines around animal cages
      stroke(0);
      strokeWeight(5);
    
      // note... loop over each animal in your list of animals and draw the cage...
      // note:  .... should be the arraylist name and index
      for (int i = 0; i < animals.size(); i++)
      {
        line(animals.get(i).getXMin(), animals.get(i).getYMin(), animals.get(i).getXMin(), animals.get(i).getYMax());  // left
        line(animals.get(i).getXMin(), animals.get(i).getYMin(), animals.get(i).getXMax(), animals.get(i).getYMin());  // top
        line(animals.get(i).getXMin(), animals.get(i).getYMax(), animals.get(i).getXMax(), animals.get(i).getYMax());  // bottom
        line(animals.get(i).getXMax(), animals.get(i).getYMin(), animals.get(i).getXMax(), animals.get(i).getYMax());  // right
      }
    
      noStroke();
      for (int i = 0; i < animals.size(); i++)
      {
        animals.get(i).display();
      }
      animals.get(0).display();
      animals.get(0).move();
      zookeeper.move();
      zookeeper.display();
    }
    
    
    public abstract class Animal
    {
      private int x, y;
      private color c;
      private int xspeed, yspeed;
      private int xmin, xmax, ymin, ymax;
    
      public Animal(int xpos, int ypos, color ncol, int xspd, int yspd, 
        int xminn, int xmaxx, int yminn, int ymaxx)
      {
        x = xpos;
        y = ypos;
        c = ncol;
        xspeed = xspd;
        xmin = xminn;
        xmax = xmaxx;
        ymin = yminn;
        ymax = ymaxx;
        yspeed = yspd;
      }
      void move()
      {
        //if (x > width) x = 0;
        //else x += xspeed;
        int xmove = (int) random(-1. * xspeed, xspeed);
        int ymove = (int) random(-1. * yspeed, yspeed);
    
        if (zookeeper.getX() < xmax && zookeeper.getX() > xmin &&
          zookeeper.getY() < ymax && zookeeper.getY() > ymin)  // zookeeper is in cage
        {   // so move toward the zookeeper
          if (x - zookeeper.getX() < 0) xmove = xspeed;
          else xmove = -1*xspeed;
    
          if (y - zookeeper.getY() < 0) ymove = yspeed;
          else ymove = -1*yspeed;
        }
    
        if (x + xmove < xmax-20 && x + xmove > xmin+20) x += xmove;
        if (y + ymove < ymax-20 && y + ymove > ymin+20) y += ymove;
      }
    
      public abstract void display();
    
      public int getX() { 
        return x;
      }
      public int getY() { 
        return y;
      }
      public int getXSpeed() { 
        return xspeed;
      }
      public color getColor() { 
        return c;
      }
      public int getXMin() { 
        return xmin;
      }
      public int getXMax() { 
        return xmax;
      }
      public int getYMin() { 
        return ymin;
      }
      public int getYMax() { 
        return ymax;
      }
    }  
    
    class Elephant extends Animal {
      private int x, y;
      private color c;
      private int xspeed, yspeed;
      private int xmin, xmax, ymin, ymax;
      public Elephant(int xpos, int ypos, color ncol, int xspd, int yspd, 
        int xminn, int xmaxx, int yminn, int ymaxx) {
        super(xpos, ypos, ncol, xspd, yspd, xminn, xmaxx, yminn, ymaxx);
      }
      void display() {
        c=color(255);
        fill(255);
        ellipse(x,y,25,28);
        rect(x-5,y,5,5);
        rect(x+5,y,5,5);
        line(x-5,y,x-8,y-5);
      }
    }
    // Write your 4 subclasses of the Animal class here. You will need to
    // call super in your constructor and you will need to write your own 
    // display() method as defined in the super class.
    
    
    
    
    class Zookeeper
    {
      int x, y;
      color c;
      int xspeed, yspeed;
    
      public Zookeeper(int xpos, int ypos, color ncol)
      {
        x = xpos;
        y = ypos;
        c = ncol;
        xspeed = 10;
        yspeed = 10;
      }
    
      public int getX() { 
        return x;
      }
      public int getY() { 
        return y;
      }
    
      void move()
      {
        if (keyPressed)
          if (key == 'w') y -= 10;
          else if (key == 'a') x -= 10;
          else if (key == 's') y += 10;
          else if (key == 'd') x += 10;
      }
    
      void display()
      {
        ellipseMode(CENTER);
        fill(c);
        rectMode(CENTER);
        fill(c);
        rect(x, y, 10, 20);
        ellipseMode(CENTER);
        ellipse(x, y-12, 7, 7);
        rect(x-4, y+10, 2, 20);  // legs
        rect(x+4, y+10, 2, 20);
        rect(x-7, y, 2, 13);  // arms
        rect(x+7, y, 2, 13);
      }
    }
    

    what I found, after executing, it does not do the forced x,y positions. but the x,y vars are private so are not inherited. what should I change about this to use move() @gotoloop ?

  • The 3 Java access keywords: private, protected & public, don't have any effect on inheritance.

    I've already told you that re-declaration is über bad.
    When extending from some class, pay close attention about what's already in there.

    For example, if field called xspeed exists in the parent class, why declaring another 1 in the subclass? It's already being inherited after all!

    In short, inheritance demands careful planning. :-B

  • @gotoloop what should I do to improve the above code? I am not understanding.

  • edited February 2016
    • Well, you can start at getting rid of anything redundant.
    • You've got a base class called Animal.
    • Every class which extends that automatically inherits all of its members.
    • So we don't repeat them inside the class which inherits from it.
    • Unless we really wanna @ Override some inherited method or, god forbid it, overshadow some inherited field!
    • Seems to me you haven't grasp what is regular inheritance concept in OOP.
    • Perhaps some reading about OOP basics may give you some extra insight:

    http://docs.Oracle.com/javase/tutorial/java/concepts/index.html
    http://docs.Oracle.com/javase/tutorial/java/javaOO/index.html

  • this is homework

    see lines 137-139 e.g.

    maybe just speak to your teacher

  • @chrisir @gotoloop that is the issue, she doesn't offer help I solved most of it but, I have a new issue a null pointer exception

    import java.util.Arrays.*;
    int size=100;
    //Student[] stud;
    //SeatingChart[][] seats;
    ArrayList<Student> roster;
    void setup()
    {
      //stud=new Student[50];
      //stud[0]=new Student ("Jin");
      //stud[1]=new Student ( "Alta" );
      //stud[2]=new Student ( "Rosan");
      //stud[3]=new Student ( "Helena");
      //stud[4]=new Student ( "Jeanice");
      //stud[5]=new Student ( "Noah");
      //stud[6]=new Student ( "Collen" );
      //stud[7]=new Student ("Jerold"); 
      //stud[8]=new Student ( "Petra");
      //stud[9]=new Student ( "Araceli" );
      //stud[10]=new Student ( "Eunic");
      //stud[11]=new Student ( "Tosh");
      //stud[12]=new Student ("Denny");
      //stud[13]=new Student ( "Jesus");
      //stud[14]=new Student ( "Peg");
      //stud[15]=new Student ( "Fawn");
      //stud[16]=new Student ("Raylene");
      //stud[17]=new Student (  "Reyes");
      //stud[18]=new Student ( "Ayako");
      //stud[19]=new Student ( "Clifford");
      //stud[20]=new Student ( "Cara"); 
      //stud[21]=new Student (  "Elsy");
      //stud[22]=new Student ( "Maple");   
      //stud[23]=new Student ( "Kemberly");
      //stud[24]=new Student ( "Stephen"); 
      //stud[25]=new Student ( "Laurette");
      //stud[26]=new Student ("Shantelle"); 
      //stud[27]=new Student (  "Tommye");
      //stud[28]=new Student ( "Marylin");
      //stud[29]=new Student ( "Tess");
      //stud[30]=new Student (  "Letisha");
      //stud[31]=new Student ( "Georgetta"); 
      //stud[32]=new Student ( "Lorri"); 
      //stud[33]=new Student (  "Tabetha");
      //stud[34]=new Student ( "Camila");
      //stud[35]=new Student ( "Leif");
      //stud[36]=new Student ( "Billye");
      //stud[37]=new Student ( "Amparo");
      //stud[38]=new Student ( "Mariela");
      //stud[39]=new Student ( "Maritza"); 
      //stud[40]=new Student ( "Eliseo"); 
      //stud[41]=new Student ( "Torrie"); 
      //stud[42]=new Student (  "Pia"); 
      //stud[43]=new Student ( "Karisa" );
      //stud[44]=new Student ( "Bethel"); 
      //stud[45]=new Student ( "Minna"); 
      //stud[46]=new Student ( "Ling"); 
      //stud[47]=new Student ( "Randi"); 
      //stud[48]=new Student ( "Terra"); 
      //stud[49]=new Student ( "Nicholle");
      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, 3, 4);
      apcs.removeAbsentStudents(1,roster);
      apcs.drawChart(roster);
    
      //int num=0;
      //for (int i=0; i<20; i++) {
      //  for (int j=0; j<20; j++) {
      //    num++;
      //    int rand=(int)(random(2));
      //    println(rand);
      //    if (rand==1)
      //      stud[i][j]=new Student("stud"+num, (int)(random(6)));
      //    else
      //      stud[i][j]=new Student("name only");
      //  }
      //}
      //stud[0][0]=new Student("Student");
      //stud[0][1]=new Student("Marks", 2);
      //stud[0][2]=new Student("Joe");
      //stud[0][3]=new Student("Jacobs");
      //stud[0][4]=new Student("Hailey");
      //stud[0][5]=new Student("Hailey");
    
      //stud[1][0]=new Student("Joe");
      //stud[1][1]=new Student("Jacob");
      //stud[1][2]=new Student("Hailey");
      //stud[1][3]=new Student("Mark");
      //stud[1][4]=new Student("Joe");
      //stud[1][5]=new Student("Joe");
    
      //stud[2][0]=new Student("Joe");
      //stud[2][1]=new Student("Jacob");
      //stud[2][2]=new Student("Hailey");
      //stud[2][3]=new Student("Mark");
      //stud[2][4]=new Student("Joe");
      //stud[2][5]=new Student("Joe");
    
      //stud[3][0]=new Student("Joe");
      //stud[3][1]=new Student("Jacob");
      //stud[3][2]=new Student("Hailey");
      //stud[3][3]=new Student("Mark");
      //stud[3][4]=new Student("Joe");
      //stud[3][5]=new Student("Joe");
    
      //stud[4][0]=new Student("Joe");
      //stud[4][1]=new Student("Jacob");
      //stud[4][2]=new Student("Hailey");
      //stud[4][3]=new Student("Mark");
      //stud[4][4]=new Student("Joe");
      //stud[4][5]=new Student("Joe");
    
      //stud[5][0]=new Student("Joe");
      //stud[5][1]=new Student("Jacob");
      //stud[5][2]=new Student("Hailey");
      //stud[5][3]=new Student("Mark");
      //stud[5][4]=new Student("Joe");
      //stud[5][5]=new Student("Joe");
    
      //drawChart(seats);
    
      // 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
     */
    
    
    
    
    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(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);
        //    text(newSeatingChart[i][j].name, i*size, j*size);
        //    text(newSeatingChart[i][j].absenceCount, i*size+40, j*size);
        //  }
        //}
        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);
            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 "";
      }
    }
    
  • Please don't post a question in multiple places - it splits the answers up and causes extra work.

  • Please clear your code of old code lines

    Also, /** seems not to work - replace with /*

  • Which like causes the error? What does it say?

This discussion has been closed.