Solved

Problems adding exceptions...

Posted on 2006-11-12
6
345 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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

19 Experts available now in Live!

Get 1:1 Help Now