Link to home
Start Free TrialLog in
Avatar of chinamox
chinamox

asked on

Cannot make a static reference to the non-static field...

How do I stop getting the following error message:

'Cannot make a static reference to the non-static field Elevator.passengers'

__CODE__

package cscie160.hw2;


public class Elevator {
      int capacity = 10;
      int num_floors = 7;
      int passengers = 3;
      int current_floor = 0;
      static int cycle_count = 0;
      int print_floor = 0;
      boolean dir_up = true;
      int [] req_per_floor = {0,0,0,0,0,0,0};
      int [] pass_to_floor ={0,2,1,0,0,0,0};
/** Main Method
  * @param args
  *
*/                  
      public static void main(String[] args){
      Elevator myElevator = new Elevator();
                  
            
                  System.out.println(myElevator);            
                  myElevator.move();                  
            
      }
      
      
            //#4 move method
      public void move(){
            while (cycle_count <= 2){
            if(dir_up == true){
            current_floor = current_floor +1;
            stop();
            }
            else {
            current_floor = current_floor -1;
            stop();                  
            }
            if (current_floor == 0){dir_up = true;}
            if (current_floor == 6){dir_up = false;}
            if (current_floor == 6){cycle_count++;}
            }
      }
/**Overide the toString() from java.lang.Object
 * @return A string reporting the elevator's state at each floor.
*/  
      public String toString(){
            print_floor = current_floor + 1;
            return "Currently "+passengers +" passengers are onboard."+ "\n"+"Current floor :" + print_floor ;
      }
            
/** Stops the Elevator at each floor.  
 * @param print_floor
 * @return No return value
*/
   public void stop(){
               
         while (pass_to_floor[current_floor] >= 1){
               disembark_passenger();
         }
         while(req_per_floor[current_floor] >= 1){
               board_passenger();
         }
         print_floor = current_floor + 1;
      }
            
            
/** Board passanger method
 * @return No return value
*/
      public void board_passenger(){
            passengers = passengers + 1;
            req_per_floor[current_floor]--;
      }
/** Disembark passenger method
 * @return No return value
*/
      public void disembark_passenger(){
            passengers = passengers - 1;
            pass_to_floor[current_floor]--;
      }


}


public class Floor {
      
      

      int queued_passengers =0;
      
      public void BoardPassenger(){
            {
            if (Elevator.passengers <= Elevator.capacity){
                  queued_passengers = queued_passengers - 1;
                  Elevator.passengers++;
            }
            else{

            }
            return ;
            }
      }
}

__END__
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

In class Floor you need to have created an instance or got a reference to Elevator
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
chinamox, why did you ignore my answer?
Avatar of chinamox
chinamox

ASKER

CEHJ,  I found the code that objects posted more understandable, as it illistrated what I was doing wrong.  Thank you for your response though, if I had a way to divide my points I would have awarded you some as well.