Solved

passing objects between classes

Posted on 2004-10-17
5
369 Views
Last Modified: 2010-03-31
This is an elevator simulation in which I have an Elevator class and a Floor class.  The Elevator class has 'main'.  Most of the code below works, but I'm having problems making the two classes work together.  In 'main', I created an array of Floor objects and initialized them in Floor by calling setPassengersWaiting.  However, in Elevator.stop, I call:  floors[currentFloor].unloadPassengers(this);  // DOESN'T WORK!.  'floors' is seen within 'main', but not within 'stop' and I'm not sure why.  Usually, if it's a static/non-static issue, you get an error stating so.  Here, it just says it cannot resolve 'floors'.  The code is below.  Thanks.

package cscie160.hw2;

public class Elevator
{
            // class constants
            private static final int TOPFLOOR = 7;  // number of floors the elevator travels
             private static final int UP = 1; // up direction
              private static final int DOWN = -1; // down direction
            private static final int MAXPASSENGERS = 10; // max number of passengers
                        
            // class variables
            private int currentFloor; private int direction; private int numOfPassengers;
            //static Elevator myElevator;
                  
            // boolean array for the destination floor requests
            private boolean[] destFloorArray = new boolean [8];
            
            // int array for the number of passengers per destination floor
            private int[] destPassengersArray = {0, 0, 0, 0, 0, 0, 0, 0};
                                    
            public Elevator() // class constructor
            {
                  currentFloor = 1; // initial floor
                    direction = UP; // initial direction
                    numOfPassengers = 0; // initial passenger count
            }
      
            public void move() // moves the elevator
            {  
                  if(direction == UP) // if direction = + 1
                     currentFloor++; // increment the current floor
                  else
                     currentFloor--; // else decrement it
                  if(atTopOrBottomFloor()) // if at the top floor or bottom floor
                     changeDirection(); // go the opposite way
              }
                        
            private boolean atTopOrBottomFloor() // evaluates need for change of direction
            {
                  if (currentFloor == 1 && direction == DOWN) // if on first floor and direction is down
                        return true; // return true
                  else if (currentFloor == TOPFLOOR && direction == UP)  // if on top floor and direction is up
                        return true; // return true
                  else
                        return false; // if neither is true, return false
            }
            
            private void changeDirection() // changes the sign on the direction variable
            {
                  direction *= -1;
              }
              
            public void stop() // stops the elevator and clears passengers
            {
                  if (destFloorArray[currentFloor] == true || Floor.passQueueArray[currentFloor] > 0) // if this floor is on the request list  
                  {
                        System.out.println("Elevator stopped on floor " + currentFloor + ".");  // prints current floor
                        
                        floors[currentFloor].unloadPassengers(this);  // DOESN'T WORK!
                        
                        numOfPassengers -= destPassengersArray[currentFloor]; // reduces number of total passengers
                        destPassengersArray[currentFloor] = 0; // clears passengers destined for this floor
                        destFloorArray[currentFloor] = false; // clears the request for this floor
                        System.out.println("Elevator is on floor " + currentFloor + " and contains " + numOfPassengers + " passenger.");
                        System.out.println();
                  }
            }            
            
            public String toString() // overrides toString
            {
                  if (numOfPassengers == 1)
                  {
                        return "Elevator is on floor " + currentFloor + " and contains "
                    + numOfPassengers + " passenger.";
                  }
                  else
                  {
                        return "Elevator is on floor " + currentFloor + " and contains "
                    + numOfPassengers + " passengers.";
                  }
            }
                        
            public void boardPassenger(int floor) // boards passengers and keeps totals
            {
                  destPassengersArray[floor] += 1; // increments number of passengers destined for this floor
                  numOfPassengers += 1; // increments number of total passengers
                  destFloorArray[floor] = true; // adds this floor to the requested list
                  System.out.print("Passenger boarded for floor " + floor + ":  ");    
                  System.out.println((destPassengersArray[floor]) + " total for this floor"); // total passengers for this floor
                System.out.println("Total passengers on board : " + numOfPassengers); // total passengers on board
                     System.out.println();
            }
            
            public void registerRequest()
            {
            }
                  
            public static void main(String[] args)
            {
                  Elevator myElevator = new Elevator();  
                  //Floor myFloor  = new Floor();
                                                      
                  Floor floors[] = new Floor[8];
                  for(int i = 0; i < floors.length; i++)
                  {
                        floors[i] = new Floor();
                    }
                  floors[2].setPassengersWaiting(2,3);
                  floors[4].setPassengersWaiting(4,2);
                                    
                  System.out.println(myElevator);
                  System.out.println();
                  myElevator.boardPassenger (2); // boards one passenger for floor 2
                  myElevator.boardPassenger (2); // boards one passenger for floor 2
                  myElevator.boardPassenger (3); // boards one passenger for floor 3
                                    
                  int n = 0; // counter variable

                  do // loop to iterate the elevator
                  {
                        //Elevator begins moving  
                        for(int i = 1; i < TOPFLOOR; i++)
                        {
                              myElevator.move();
                              
                              //Check if you need to stop at each floor      
                              myElevator.stop();
                              
                              n++;
                        }
                  }  while ( n < 15 ); // loop fifteen times
                  
            }
}

package cscie160.hw2;

public class Floor
{
      public static int[] passQueueArray = {0, 0, 0, 0, 0, 0, 0, 0};
      
      public void setPassengersWaiting(int floor, int passengers)
      {
                  passQueueArray[floor] = passengers;
      }

      public void unloadPassengers(Elevator myElevator)
      {
            
      
      }
}
0
Comment
Question by:jfollen
  • 3
5 Comments
 
LVL 2

Accepted Solution

by:
bcsonka earned 250 total points
ID: 12332912
Hi,

You've created floors as a local variable in Elevator.main().  You need to do one of the following instead in order for floors to be recognized by the Elevator.stop() method:

1.) Create floors as a static instance variable in the Elevator class.
2.) Pass floors as a parameter to the Elevator.stop() method.

I hope that helps.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12332925
public void unloadPassengers(Elevator myElevator)
     {
         
     
     }

What is the code it contains??


When I try like this, I got following results


public void unloadPassengers(Elevator myElevator) {
    System.err.println("entered here "+myElevator);
  }



entered here Elevator is on floor 2 and contains 3 passengers.

entered here Elevator is on floor 3 and contains 1 passenger.

Elevator is on floor 1 and contains 0 passengers.



Passenger boarded for floor 2:  1 total for this floor

Total passengers on board : 1



Passenger boarded for floor 2:  2 total for this floor

Total passengers on board : 2



Passenger boarded for floor 3:  1 total for this floor

Total passengers on board : 3



Elevator stopped on floor 2.

Elevator is on floor 2 and contains 1 passenger.



Elevator stopped on floor 3.

Elevator is on floor 3 and contains 0 passenger.



Elevator stopped on floor 4.

entered here Elevator is on floor 4 and contains 0 passengers.

entered here Elevator is on floor 4 and contains 0 passengers.

entered here Elevator is on floor 2 and contains 0 passengers.

entered here Elevator is on floor 2 and contains 0 passengers.

entered here Elevator is on floor 4 and contains 0 passengers.

Elevator is on floor 4 and contains 0 passenger.



Elevator stopped on floor 4.

Elevator is on floor 4 and contains 0 passenger.



Elevator stopped on floor 2.

Elevator is on floor 2 and contains 0 passenger.



Elevator stopped on floor 2.

Elevator is on floor 2 and contains 0 passenger.



Elevator stopped on floor 4.

Elevator is on floor 4 and contains 0 passenger.



0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12332930
package org.prithvi.test;

public class Elevator {
  // class constants
  private static final int TOPFLOOR = 7; // number of floors the elevator travels
  private static final int UP = 1; // up direction
  private static final int DOWN = -1; // down direction
  private static final int MAXPASSENGERS = 10; // max number of passengers

  // class variables
  private int currentFloor;
  private int direction;
  private int numOfPassengers;

  //static Elevator myElevator;

  // boolean array for the destination floor requests
  private boolean[] destFloorArray = new boolean[8];

  // int array for the number of passengers per destination floor
  private int[] destPassengersArray = {
      0, 0, 0, 0, 0, 0, 0, 0};

  public Elevator() { // class constructor
    currentFloor = 1; // initial floor
    direction = UP; // initial direction
    numOfPassengers = 0; // initial passenger count
  }

  public void move() { // moves the elevator
    if (direction == UP) { // if direction = + 1
      currentFloor++; // increment the current floor
    }
    else {
      currentFloor--; // else decrement it
    }
    if (atTopOrBottomFloor()) { // if at the top floor or bottom floor
      changeDirection(); // go the opposite way
    }
  }

  private boolean atTopOrBottomFloor() { // evaluates need for change of direction
    if (currentFloor == 1 && direction == DOWN) { // if on first floor and direction is down
      return true; // return true
    }
    else if (currentFloor == TOPFLOOR && direction == UP) { // if on top floor and direction is up
      return true; // return true
    }
    else {
      return false; // if neither is true, return false
    }
  }

  private void changeDirection() { // changes the sign on the direction variable
    direction *= -1;
  }

  public void stop() { // stops the elevator and clears passengers
    if (destFloorArray[currentFloor] == true ||
        Floor.passQueueArray[currentFloor] > 0) { // if this floor is on the request list
      System.out.println("Elevator stopped on floor " + currentFloor + "."); // prints current floor

      floors[currentFloor].unloadPassengers(this); // DOESN'T WORK!

      numOfPassengers -= destPassengersArray[currentFloor]; // reduces number of total passengers
      destPassengersArray[currentFloor] = 0; // clears passengers destined for this floor
      destFloorArray[currentFloor] = false; // clears the request for this floor
      System.out.println("Elevator is on floor " + currentFloor +
                         " and contains " + numOfPassengers + " passenger.");
      System.out.println();
    }
  }

  public String toString() { // overrides toString
    if (numOfPassengers == 1) {
      return "Elevator is on floor " + currentFloor + " and contains "
          + numOfPassengers + " passenger.";
    }
    else {
      return "Elevator is on floor " + currentFloor + " and contains "
          + numOfPassengers + " passengers.";
    }
  }

  public void boardPassenger(int floor) { // boards passengers and keeps totals
    destPassengersArray[floor] += 1; // increments number of passengers destined for this floor
    numOfPassengers += 1; // increments number of total passengers
    destFloorArray[floor] = true; // adds this floor to the requested list
    System.out.print("Passenger boarded for floor " + floor + ":  ");
    System.out.println( (destPassengersArray[floor]) + " total for this floor"); // total passengers for this floor
    System.out.println("Total passengers on board : " + numOfPassengers); // total passengers on board
    System.out.println();
  }

  public void registerRequest() {
  }

  public static void main(String[] args) {
    Elevator myElevator = new Elevator();
    //Floor myFloor  = new Floor();

   
    for (int i = 0; i < floors.length; i++) {
      floors[i] = new Floor();
    }
    floors[2].setPassengersWaiting(2, 3);
    floors[4].setPassengersWaiting(4, 2);

    System.out.println(myElevator);
    System.out.println();
    myElevator.boardPassenger(2); // boards one passenger for floor 2
    myElevator.boardPassenger(2); // boards one passenger for floor 2
    myElevator.boardPassenger(3); // boards one passenger for floor 3

    int n = 0; // counter variable

    do { // loop to iterate the elevator
      //Elevator begins moving
      for (int i = 1; i < TOPFLOOR; i++) {
        myElevator.move();

        //Check if you need to stop at each floor
        myElevator.stop();

        n++;
      }
    }
    while (n < 15); // loop fifteen times

  }
  static  Floor floors[] = new Floor[8];
}



package org.prithvi.test;

public class Floor {
  public static int[] passQueueArray = {
      0, 0, 0, 0, 0, 0, 0, 0};

  public void setPassengersWaiting(int floor, int passengers) {
    passQueueArray[floor] = passengers;
  }

  public void unloadPassengers(Elevator myElevator) {
    System.err.println("entered here "+myElevator);
  }
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12332934
I have tried the code some thing like above
0
 

Author Comment

by:jfollen
ID: 12332944
Yes, that did the trick.  Thanks very much.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now