Running The Aircraft Engine Simulation Program


The Aircraft Engine Simulation: An Extended Example

In this section I will ramp up the complexity somewhat and present an extended example of compositional design — the aircraft engine simulation. Don’t panic! It’s a simple simulation designed primarily to get you comfortable with an increased level of both conceptual and physical complexity. This means there are more classes that participate in the design which means there are more source code files to create, compile, and maintain.

Figure 10-10 gives the project specification for the aircraft engine simulation. Read it carefully before proceeding to the next section.

image from book

                         Project Specification                       Aircraft Engine Simulation Objectives: - Apply compositional design techniques to create a Java program - Create a composite aggregation class whose functionality is derived   from its various part classes - Employ UML class and sequence diagrams to express your design ideas - Derive user-defined data types to model a problem domain - Employ inter-object message passing - Employ the type-safe enumeration pattern in your design Tasks: - Write a program that simulates the basic functionality of an aircraft   engine. The engine should contain the following parts: fuel pump, oil   pump, compressor, temperature sensor, and oxygen sensor. Limit the   functionality to the following functions:          * set and check each engine part status          * set and check the overall engine status          * start the engine          * stop the engine Hints: - Each engine should have an assigned engine number and each part   contained by the engine should register itself with the engine number. - When checking the engine status while the engine is running it should   stop running if it detects a fault in one of its parts. - When an individual part status changes the engine must perform a   comprehensive status check on itself. - If, when trying to start the engine, the engine detects a faulty part   the engine must not start until the status on the faulty part changes. - Limit the user interface to simple text messages printed to the console.

image from book

Figure 10-10: Aircraft Engine Project Specification

The project specification offers good direction and several hints regarding the project requirements. The aircraft engine simulation consists of seven classes. The Engine class is the composite. An engine has a fuel pump, oil pump, compressor, oxygen sensor, and temperature sensor. These parts are represented in the design by the FuelPump, OilPump, Compressor, OxygenSensor, and TemperatureSensor classes respectively. Each of these classes have an association with the PartStatus class. The purpose of the PartStatus class is discussed in detail below.

The Purpose of the Engine Class

The purpose of the Engine class is to embody the behavior of this thing we are modeling called an engine. A UML class diagram for the Engine class is given in figure 10-12.

image from book
Figure 10-12: Engine Class

Engine Class Attributes and Methods

Referring to figure 10-12 — the Engine class has several attributes: its_compressor, its_fuelpump, its_oilpump, its_oxygensensor, and its_temperaturesensor. Each map to their respective part classes. However, several more attributes are required to complete the class. Two of these, its_engine_number and is_running are primitive type variables. The last attribute, its_status, is a PartStatus reference.

image from book
Figure 10-11: Engine Simulation Class Diagram

As you can tell from looking at the class diagram in figure 10-12 all the Engine class attributes are declared to be private. (this is denoted by the ‘-’ symbol preceding each attribute’s name) The design of Engine class will guard against returning a reference to any of its part objects. This will ensure that when a reference to an Engine object goes out of scope the Engine object it referenced, along with all its component objects, will be collected by the JVM garbage collector. (Although you cannot guarantee when this collection event will occur!)

The Engine class has one constructor that takes an integer as an argument. When created, an Engine object will set its_engine_number attribute using this number.

The remaining Engine class methods map pretty much directly to those specified or hinted at in the project specification. The Engine class interface allows you to set the status of each of an engine’s component parts, check the status of an engine, and start and stop an engine.

PartStatus —A Typesafe Enumeration Pattern

The PartStatus class is an implementation of a popular Java design pattern known as the Typesafe Enumeration Pattern. C and C++ programmers liked the enumeration construct those languages offered but were left high-and-dry by Java and its lack of enumeration support. Although Java 1.5 now offers enumerations, early versions of Java did not.

The typesafe enumeration pattern is employed when you want to have an object assume certain valid states and have the compiler warn you when you attempt to assign an invalid state to an object.

The specific problem encountered in the aircraft engine simulation program is that of setting the part status. If you define two valid part status states in your design as being WORKING and NOT_WORKING how do you ensure that only those states are assigned to a part’s status attribute?

If you attempt to solve the problem using a primitive type integer variable and define two constants like so:

 public static final int WORKING = 0; public static final int NOT_WORKING = 1;

...you can then declare an attribute named its_status and assign to it one of these constant values:

 private int its_status = WORKING;

The value of its_status is now set to 0, which is a valid state value in your design, however, nothing in the Java compiler will prevent you from assigning an unacceptable integer value to its_status. Take a look now at the code for the PartStatus class given in example 10-7.

Example 10.7: PartStatus.java

image from book
 1     public class PartStatus { 2       private boolean its_status = false; 3 4       public static final PartStatus WORKING = new PartStatus(true); 5       public static final PartStatus NOT_WORKING = new PartStatus(false); 6 7       private PartStatus(boolean status){ 8          its_status = status; 9       } 10 11      public boolean isWorking(){ return its_status; } 12    }
image from book

This type of programming takes a little getting used to. The PartStatus class has one private attribute which is a boolean variable. It has two public constant attributes WORKING and NOT_WORKING which are instances of type PartStatus. Notice that the PartStatus class has a private constructor. This means that the only code allowed to create instances of the PartStatus class is itself, which is done on lines 4 and 5. Study the code listing given at the end of the chapter and see how the PartStatus class and its typesafe enumeration functionality is utilized in the program.

Aircraft Engine Simulation Sequence Diagrams

The aircraft engine simulation is sufficiently complex to warrant focused sequence diagrams. Figure 10-13 gives the sequence diagram for the creation of an Engine object.

image from book
Figure 10-13: Aircraft Engine Create Engine Object Sequence

Referring to figure 10-13 — a User starts the sequence of events by running the EngineTester program. (The EngineTester class is covered in the next section.) The EngineTester program creates an Engine object by calling the Engine() constructor with an integer argument. The Engine constructor in turn creates all the required part objects. When all the part object constructor calls return, the Engine constructor call returns to the EngineTester program. At this point the Engine object is ready for additional interface method calls from the EngineTester program.

As I said earlier, this sequence diagram represents a focused part of the aircraft simulation program. In fact, this diagram only accounts for the sequence of events resulting from the execution of line number 3 of example 10.8 in the next section. The creation of additional sequence diagrams for the aircraft simulation program is left for you to do as an exercise.

Example 10.8: EngineTester.java

image from book
 1     public class EngineTester { 2       public static void main(String[] args){ 3        Engine e1 = new Engine(1); 4        e1.startEngine(); 5        e1.setCompressorStatus(PartStatus.NOT_WORKING); 6        e1.startEngine(); 7        e1.startEngine(); 8        e1.setCompressorStatus(PartStatus.WORKING); 9        e1.startEngine(); 10      } 11    }
image from book




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net