Solved

passing objects between classes

Posted on 2004-10-17
5
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 83
Tagging and Merging on Branch 1 68
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 32
Session in java desktop 5 37
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

734 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