Scientific Simulations in Java

MATHSC 481/983 Special
Topics in Computational Math (3 hours)

       

 


Last updated on March 6, 2000.


 

 

This represents an evolving, loose collection of notes, examples, and discussons related to the topics in the course.

Object Concepts

Classes and Objects
The class defines all the objects of the same type. In fact, in many respects a class is a type. An object is an instantiation of a class. For example, suppose that x, y, and z are strings with the following values: x = "Ann", y = "Bob", and z = "Carol and Dan". Each is an object of the String class, which is part of the java.lang package. In definging the String class the authors of Java included approximately 55 methods such as length(), charAt(int index), and endsWith(String s). The expression x.length() would evaluate to 3; the expression y.charAt(0) would evaluate to 'B', and the expression z.endsWith("an") would return true.

A class may be abstract, in that it specifies that certain methods must exist without providing any implementation code. Objects cannot be instantiated from an abstract class.

Inheritance
A Java class may extend another class. When that happens the new class inherits all the attributes and methods of the parent class.

The two Shape Application examples as well as the Simple Draw applet contain the four classes: Shape, myRectangle, myEllipse, and mySquare. Shape is an abstract class, which specifies that every shape will have a bounding box and a color. The bounding box is specified by four ints: the x,y coordinates of the upper left corner and the height and width. The Shape class also specifies that every shape will have the following methods: moveTo(), draw(), area(), and perimeter(). Since the moveTo() method simply changes the coordinates of the upper left corner of the bounding box this method is implemented in the Shape class. The other methods can not be implemented until we know more about the physical shape. The myRectangle and myEllipse class implement the draw(), area(), and perimeter() methods. They inherit the moveTo() method from the Shape class so that code doesn't have to be rewritten. Since a square is a rectangle the mySquare class inherits the draw(), area(), and perimeter() methods from the myRectangle class.

A subclass may also override a method from its parent class. The shape examples do this in the case of the toString() method which they inherit from the Object class. The Shape class itself implements the code for displaying the attributes. The subclasses simply concatanate their name with the string they obtain by invoking their parents toString() method.

Polymorphism
Polymorphism refers to the fact that an object knows what it is and executes the method that is appropriate for itself. The Simple Draw applet is an excellent example of this. The applet code maintains a list of shapes and the paint method simply tells each shape in the list to draw() itself. Each object executes the draw() method specified in its class definition.

Design Methodology

The Unified Modeling Language, UML, was developed to help designers get a handle on different aspects of the design process.

Use cases are scenarios describing how the user of the software will interact with the software. These are particularly important during the initial specifications of the software.

Class Diagrams are used to specify a class. They include the name, the attributes, and the behavior. Class diagrams also indicate relationships between classes, particularly inheritance and composition. Class diagrams can be used effectively from three different perspectives: conceptual, specification, and implementation. In the conceptual phase, the classes are used to communicate about the domain knowledge of the user. In the specification phase, the classes reflect responsibilities. In the implementation phase the classes should match up with those of the software.

In the design phase, Class-Responsibility-Collaboration cards, CRC cards, are particularly useful in extablishing the "contractual" relationships between different objects. This is a good technique for developing the interface specifications as well as delineating what classes are necessary.

For the general plotter applet, two design groups submitted the following CRC cards. Each group had four different teams, and the lack of coherence and agreement is very evident in these cards.