Solved

Problems adding exceptions...

Posted on 2006-11-12
6
347 Views
Last Modified: 2012-05-05
I am refining an Elevator Modal and now I am trying to add some exceptions but am having problems. Specifically, I am having trouble addding an ElevatorFullEception to the code below:

__Code__

public class Elevator {
      int capacity = 10;
      int num_floors = 7;
      
      public int passengers;
      public int current_floor;
      static int cycle_count = 0;
      int print_floor = 0;
      
      // Direction of movement
      private boolean dir_up;
      
      // switched to boolean array to reflect calls to a particular floor
      // if you think of a real elevator it stops whether one person presses
      // the button or ten people press it.
      public boolean [] req_per_floor = new boolean[7];
      
      // records the number of passengers who want to head to a floor
      public int [] pass_to_floor ={0,2,1,0,0,0,0};
      
      //Creates
       public static Floor floors[] = new Floor[7];
      
       //class constructor inserted here.
      public Elevator() {
            current_floor = 0;
            dir_up = true;
            passengers = 0;
      }
      

/** Main Method
  * @param args
  *
*/                  
      public static void main(String[] args){
      Elevator myElevator = new Elevator();
                  
                  //consturct som new floors
                  for (int f =0; f < 6; f++){//might have to be seven
                        floors[f] = new Floor();
                  }
                  
                  //set passengers waiting
                  floors[1].queued_passengers = 3;
                  floors[2].queued_passengers = 2;
                  floors[3].queued_passengers = 1;
                  floors[5].queued_passengers = 4;
                  floors[6].queued_passengers = 2; // should thow exceptions
            
                  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;
                  }
            else {
            current_floor = current_floor -1;
            }
                  if(passengers < capacity){
                        if(floors[current_floor].queued_passengers > 0){
                              stop();
                        }
                  }
            //reverses dirction at bottom floor
            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(){
         System.out.println("stop!");
        
        if(pass_to_floor[current_floor] >= 1){
              disembark_passenger();
              }
         if(req_per_floor[current_floor] == true ){
               try{
               boardPassenger();
               }catch (ElevatorFullException myElevator){
                     System.out.println("ElevatorFullEception");
               }
               
         }
         print_floor = current_floor + 1;
      }
            
            
/** Board passanger method
 * @return No return value
*/
      public void boardPassenger(){
            if(floors[current_floor].queued_passengers == 0){
                  move();
            }
            
            while(passengers <= capacity){
                  floors[current_floor].queued_passengers = floors[current_floor].queued_passengers - 1;
                  passengers++;
                  pass_to_floor[0]++;
                  }
            }
            
            //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 void registerRequest(){
            if (floors[current_floor].queued_passengers >= 1){
            req_per_floor[current_floor]= true;
            }
            else{
                  req_per_floor[current_floor]= false;
            }
      }

}
//end class Elevator


public class Floor {
      
      
      public int queued_passengers;
      
            
      public void unloadPassenger(Elevator myElevator){
            int i = myElevator.pass_to_floor[myElevator.current_floor];
            if (i > 0){
                  myElevator.passengers = myElevator.passengers - 1;
                  }
            return;
      }
}

// end Floor class

import java.lang.Exception ;
public class ElevatorFullException extends Exception {
      public ElevatorFullException(){}
      //public ElevatorFullException (String x){
      //      super(x);
      public ElevatorFullException (Elevator myElevator){
            if(myElevator.passengers >= myElevator.capacity){
                  myElevator.move();
            }
      }
      
      public String toString() {
            return "Danger! Elevator is overloaded!";
      }
      public String getMessage() {
            return "Notice: The Elevator is now full of passengers.";
      }
}

__END__

Could someone please point out what I am doing wrong and show me how I might correct it?  

Thanks,

-mox
0
Comment
Question by:chinamox
  • 4
6 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17925526
try using something like this (wherever you want to throw this exception):

throw new ElevatorFullException(this); // Will throw ElevatorFullException from Elevator class.
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 500 total points
ID: 17925545
        while(passengers <= capacity){
               floors[current_floor].queued_passengers = floors[current_floor].queued_passengers - 1;
               passengers++;
               pass_to_floor[0]++;
               }
          }
Enter here:
          if(passengers >= capacity)
          {
               throw new ElevatorFullException();
          }

The code in the exception itself is in a wrong place.

Then,
 
                     public void boardPassenger(){

should be

                     public void boardPassenger() throws ElevatorFullException
                    {

An exception is thrown and caught, it should not interfere with anything else
that the arguments you may give it.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17925552
A-S, you type quicker, but I type more.

I suspect a misconception of the deployment of exceptions.

;JOOP!
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:chinamox
ID: 17927205
Thank you both.  sciurware, why do you 'expect a misconception of the deployment of exceptions?'
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17927799
Because the coding inside the ElevatorFullException should not be there.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 17928386
:)
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

770 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