Java Methods for Elevator Control

Slide Note
Embed
Share

Learn about the implementation of methods in Java for an elevator control system. Understand how to determine the next floor for the elevator to stop at based on current and desired floors, check weight limits, and efficiently manage elevator movements. Dive into writing and utilizing methods to enhance the functionality of an elevator program.


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.



Uploaded on May 10, 2024 | 0 Views


Presentation Transcript


  1. CSE 1321 Modules 7 & 8 Review

  2. Module 7 - Methods Provides ability to reuse code more easily Can perform calculations and/or return a value to method call Must send in the required number of parameters as well as matching parameter data types Method header is the method declaration (usually the top line of the method) Method signature is the combination of the method name and the parameter list. Optional arguments for Input Optional return value Method Header Method body Black Box

  3. Methods Parameters are passed by reference or by value to a method By reference means that the method is given a pointer to the parameter s memory space and any changes made are permanent By value means that the method is given a copy of the parameter s value and the original memory space is left untouched. Methods either have a return type or must be void Return types can be of any data type, there must be a RETURN statement if a return type is declared in the header Void means that no data is returned from the method to the calling code

  4. In-Class Exercise Write METHODs for the problem below: You have been asked to update the elevator code to write a method that determines which floor the elevator should stop at next based on the elevator s current floor and the desired stops that have been entered (up to two can be stored at a time now in separate variables). This method should read in the next two floors, the current floor and return a single value for the next floor to move the elevator to. You must also write a method that checks the weight of the current passengers against the ELEVATOR_LIMIT to make sure the elevator can move. This method should return a Boolean value and print a message that it cannot move to being overloaded. Remember, there is no 13th floor and the top floor is 100. Next Stops: Also, convert the main program 1 2 3 into a method for ease of use. 4 5 6 7 8 9 CLOSE 0 OPEN

  5. In-Class Exercise Solution METHOD BOOLEAN OVERLIMIT(parameters: PassengersWeightTotal) BEGIN METHOD IF (PassengersWeightTotal >= ELEVATOR_LIMIT) PRINTLINE( Unable to move, elevator weight limit exceeded, please exit elevator. ) RETURN TRUE ELSE RETURN FALSE END IF END METHOD METHOD INTEGER NextFloorToVisit(parameters: Floor1, Floor2, curFloor) BEGIN METHOD CREATE NextFloor = curFloor IF (|curFloor Floor1| < |curFloor Floor2|) NextFloor = Floor1 ELSE NextFloor = Floor2 RETURN NextFloor END METHOD

  6. In-Class Exercise Solution METHOD VOID goToFloor(parameters: NextStop) BEGIN METHOD SWITCH(NextStop) CASE 0: PRINTLINE( Shutting down elevator. ) NextStop = 0, currentFloor = 0 BREAK CASE 1: PRINTLINE( Moving to 1stfloor. ) NextStop = 1, currentFloor = 1 CASE 2: PRINTLINE( Moving to 2ndfloor. ) NextStop = 2, currentFloor = 2 BREAK //skipped floors 3-98 (13 is covered by the DEFAULT statement) CASE 99: PRINTLINE( Moving to 99thfloor. ) NextStop = 99, currentFloor = 99 BREAK CASE 100: PRINTLINE( Moving to 100thfloor. ) NextStop = 100, currentFloor = 100 BREAK DEFAULT: PRINTLINE( Staying put, that floor does not exist! ) NextStop = currentFloor, currentFloor = currentFloor END SWITCH END METHOD

  7. In-Class Exercise Solution Concluded BEGIN MAIN CREATE inputFloor1, inputFloor2 = 0, NextStop = 0, currentFloor = 0 DO PRINT( Please enter a floor using keypad 0 to shut elevator down : ) inputFloor1 = READ from user PRINT( Please enter a floor using keypad 0 to shut elevator down : ) inputFloor2 = READ from user NextStop = NextFloorToVisit(inputFloor1, inputFloor2, currentFloor) IF (! OVERLIMIT) goToFloor(NextStop) END If WHILE (inputFloor1 != 0 AND inputFloor2 != 0) END DO WHILE END MAIN

  8. Module 8 OOP & Classes Classes are: A complex data type A way of representing the concept of real life objects Classes have: A name Variables (attributes) should be PRIVATE Constructors (special functions/methods) Functions (methods) GET functions return the object s current attribute value SET functions change the object s attribute value

  9. Module 8 Visibility Visibility refers to what can see (and therefore modify) the attribute s value. It is the Computer Science version of the principle of least access only those that need to access it can do so. This prevents the data from being changed either accidentally or maliciously. Declaring an attribute as PRIVATE is the method through which this is accomplished and helps to promote encapsulation.

  10. Ps Pseudocode Class CLASS Elevator BEGIN END CLASS

  11. Ps Pseudocode Class Attributes CLASS Elevator BEGIN PRIVATE MAX_WEIGHT = 600 PRIVATE MAX_OCCUPANTS = 10 PRIVATE currentFloor PRIVATE currentOccupancy PRIVATE nextStop //Not done with class, so no END

  12. Pseudocode Class Constructor Ps CONSTRUCTOR Elevator(parameters: none) currentFloor = 0 currentOccupancy = 0 nextStop = 0 END CONSTRUCTOR

  13. Pseudocode Class Constructor 2 Ps CONSTRUCTOR Elevator(parameters: occ, stop) currentFloor = 0 currentOccupancy = occ nextStop = stop END CONSTRUCTOR

  14. Ps Pseudocode Class Method 1 METHOD BOOLEAN OverLimit(parameters: numPass, avgWeight) IF (numPass * avgWeight > MAX_WEIGHT * MAX_OCCUPANTS) RETURN TRUE ELSE RETURN FALSE END METHOD

  15. Ps Pseudocode Class Method 2 METHOD INTEGER getNextStop(parameters: curFloor, Stops[]) BEGIN METHOD CREATE NextFloor = curFloor IF (|curFloor Stops[0]| < |curFloor Stops[1]|) RETURN Stops[0] ELSE RETURN Stops[1] END METHOD

  16. Ps Pseudocode Class Method 3 METHOD INTEGER getCurrentFloor (parameters: none) return currentFloor END METHOD

  17. Ps Pseudocode Class Method 4 METHOD VOID setCurrentFloor (parameters: newFloor) currentFloor = newFloor END METHOD END CLASS

  18. Building the Elevators Ps BEGIN MAIN CREATE elevatorNorth, elevatorSouth as Elevator END MAIN

  19. Installing the Elevators Ps BEGIN MAIN CREATE elevatorNorth, elevatorSouth as Elevator elevatorNorth = NEW Elevator() elevatorSouth = NEW Elevator(2,100) END MAIN

  20. Using the Elevators Ps BEGIN MAIN CREATE elevatorNorth, elevatorSouth as Elevator elevatorNorth = NEW Elevator() elevatorSouth = NEW Elevator(2,100) PRINTLINE(elevatorNorth.getCurrentFloor()) PRINTLINE(elevatorSouth.OverLimit()) CALL elevatorNorth.setFloor(5) END MAIN

  21. In-class Exercise Create a fireplace class that contains firewood and temperature attributes, a method to add firewood (increasing attribute), method to reduce the firewood (as it burns over time), a method that increases the temperature as firewood is added and one to reduce the temperature as firewood is consumed.

  22. In-class Exercise Solution CLASS FIREPLACE BEGIN CLASS PRIVATE firewood PRIVATE temp CONSTRUCTOR FIREPLACE(parameters: none) firewood = 0 temp = 0 END CONSTRUCTOR

  23. In-class Exercise Solution CONSTRUCTOR FIREPLACE(parameters: wood) firewood = wood temp = 300 END CONSTRUCTOR METHOD addWood(parameters: wood) this.firewood += wood increaseTemp(100) END METHOD PRIVATE METHOD increaseTemp(parameters: temp) this.temp += temp END METHOD

  24. In-class Exercise Solution METHOD burnWood(parameters: wood) this.firewood -= wood reduceTemp(50) END METHOD METHOD reduceTemp(parameters: temp) this.temp -= temp END METHOD METHOD getTemp() RETURN this.temp END METHOD

  25. In-class Exercise Solution METHOD getFirewood() RETURN this.firewood END METHOD END CLASS

  26. End of Review Questions?

Related