The General Class Hierarchy Structure


Now it is time to map out the general structure of our gas mixture class hierarchy starting with the topmost or root class. The hierarchy root should declare methods that will (or must) be implemented by all gas classes. The root defines the blueprint that all subclasses must follow.

The question becomes, what kind of programming construct should this be? Should we use a concrete class, an abstract class, or an interface? In this situation it makes sense to use an abstract class as the root of the class hierarchy. All of our gas classes will be related , so there is no need to use an interface to define the generic class structure. We also don't want to get too specific at our hierarchy root but instead give ourselves flexibility in the ways in which we can implement subclasses. We will call the abstract superclass of our gas mixture class hierarchy AbstractGas . This class will declare a static variable representing the Universal Gas Constant and declare a number of abstract methods.

The simplest type of gas mixture is a perfect gas, so we'll make the PerfectGas class a subclass of AbstractGas . The PerfectGas class will define pressure , molarMass , and temperature fields. It will also declare methods that will be used to compute the density, viscosity , enthalpy, and entropy of the gas mixture. (To simplify things, we'll omit thermal conductivity and internal energy from our gas data model.) The PerfectGas class will implement the abstract methods of the AbstractGas class.

A real gas is a more general case of a perfect gas, so we will define RealGas as a subclass of PerfectGas . The RealGas class will inherit the methods declared in the PerfectGas class and will define additional fields to compute real gas viscosity, and other real gas properties. The RealGas class will use some of the PerfectGas class methods as is and will override others to provide a real gas calculation of enthalpy, entropy, specific heat, and viscosity

Specific perfect gas classes will be written as subclasses of the PerfectGas class. We will create an Air class that represents a perfect gas mixture of air. Specific real gas classes will be written as subclasses of the RealGas class. We will create a RealGas subclass named NitrogenGas that will represent a mixture of atomic and diatomic nitrogen.

It may sound confusing but it really isn't too complicated. The gas class hierarchy that we will create is shown schematically in Figure 18.1.

Figure 18.1. Gas class hierarchy

graphics/18fig01.gif

This example will make use of two class hierarchies. The RealGas class will declare an array of Species objects as one of its fields. The Species class has fields and methods that model a generic gas species. We will create two subclasses of Species named N2 and N . These classes will model diatomic and atomic nitrogen. The Species class hierarchy is shown in Figure 18.2.

Figure 18.2. Species class hierarchy

graphics/18fig02.gif

Now that we have mapped out what our class hierarchy will look like, it is time to code the classes starting with the AbstractGas class.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

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