java - implementing a method from one class to another? -


i making program airplane seating arrangements class , ended making 2 tostring methods when run program tostring method in airplane class making not work specifically:

str= str + seats[i][j].tostring(); 

i believe deleting tostring method in seat class , somehow putting airplane class tostring method fix problem or make simpler. what's wrong?

airplane class:

public class airplane  {   private seat [ ] [ ] seats;   public static final int first_class = 1;   public static final int economy = 2;   private static final int fc_rows = 5;   private static final int fc_cols = 4;   private static final int economy_rows = 5;   private static final int economy_cols = 6;    public airplane()    {     seats  = new seat[fc_rows][economy_cols];    }   public string tostring()   {     string str = "";     (int i=0; i<fc_rows; i++) {       (int j=0; j<economy_cols; j++)        {         str= str + seats[i][j].tostring();       }       str = str + "\n";     }     return str;   }     } 

seat class:

public class seat  {   private int seattype;   private boolean isreserved;   public static final int window = 1;   public static final int aisle = 2;   public static final int center = 3;    public seat(int inseattype)   {     seattype = inseattype;     isreserved = false;   }   public int getseattype()   {     return seattype;   }    public void reserveseat()   {     isreserved = true;   }   public boolean isavailable()   {     if (!isreserved)     {       return true;     }     else return false;    }   public string tostring()   {     if(isreserved == false)     {       return "*";     }     else return "";   } } 

  • in seat.tostring should print " " not "".
  • you're array fc_rows economy_cols, you're not creating seats. should have 2 arrays (one fc, 1 economy), since fc_rows != economy_rows.
  • you aren't creating seats in constructor. use nested loop create them, otherwise nullpointerexception. creating array doesn't create objects contained in array.
    • when you're creating seats in airplane constructor, use if statements figure out if seat supposed window, aisle, etc.

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -