Solved

Problems adding exceptions...

Posted on 2006-11-12
6
350 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
[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
  • 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
Independent Software Vendors: 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!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

696 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