/* looplet.java demonstration Java application for GEOL 757. J. Louie, UNR, Nov. 1998 In examining a Java program, I follow this procedure: 1) Determine how many different classes are defined within the file, what their names are, and which one is declared public, if any. Are any classes sub-classed from other classes or interfaces defined in other files? Any Applets, in particular? 2) Inspect the constructors that may be defined for any class - often near the end of a class's code, and the arguments they take. 3) Inspect each class's methods and their arguments. Find any main() methods. Are any superclass methods overridden? Used? 4) Only then do I look at the class data declarations, which are often near the beginning (since Java is descended from C). Are data objects from superclasses used? */ // This line is also a comment after the double slash. /* Imported Java system packages and classes; not used here but usually needed. */ import java.util.*; import java.io.*; import java.lang.Math; /* A couple more system classes needed for the applet further below. */ import java.awt.*; import java.applet.Applet; /* Declaration of class loop. File name should be the same as the class name with .java appended, including the same capitalization. In this file we define two classes, and one is declared public (looplet, down near the end). The file must take the name of the public class. A .java file may contain more than one class definition, but all of a class must be defined inside one file. loop does not extend any other classes or implement any interfaces. But it can be sub-classed, that is, extended by other classes. Since it is not declared public, loop can only be extended or instantiated by classes defined in other files in the same directory. */ class loop /* loop's entire definition is within the enclosing braces, and here indented one tab. On compilation, loop will be encoded into a binary object file called loop.class . */ { /* A class definition can include class data variable declarations, just like a structure declaration in C, or a block declaration in Fortran. All data belong to a class; there are no external variables or common blocks. But any class from the same directory can access other classes' data (if not declared private) by instantiating an object of that class, or through the class name if the data are declared static (created just once for the whole class, not for each instance). Here is loop's data; note these are declarations without assigning any values: */ int increment; int limit; float value; float farray[]; /* Note a semicolon terminates statements. My most frequent error is to leave off a semicolon. */ /* A class definition can also include one or more methods. Think of a class's methods as subroutines operating on the class's data. Methods (unless private) can be accessed by other classes in the same directory (i.e. package) in the same way as class data, after instantiating, or by giving the class name for a method declared static. The methods are not run in the sequence they appear in the code here. They can be defined in any order. To be a stand-alone application (instead of an applet) a class must declare a main() method. Execution will begin in main() when you give the "java loop" command (and loop.class exists, after a successful compilation). */ public static void main(String args[]) /* loop's main() method is defined between the enclosing braces, and indented one more tab. */ { /* Declare a local variable (to use within this method) to hold a reference to a class loop object. */ loop loopobject; /* Instantiate an object of the loop class by calling the loop class's constructor, and assign its reference to the variable. The constructor assigns values to the class data variables. */ loopobject = new loop(10); /* Call loop's spin() method to actually run around the loop. spin's argument is a loop class data variable, accessed through the loop object instance, much like for a member of a data structure in C. spin returns nothing (void), so no assignment is needed. */ loopobject.spin(loopobject.increment); /* End of main() method, Java machine will stop executing. Of course, you could put all declarations, data, assigments, and method code for this example directly in the main() method, for a very brief code. You could get rid of all these wordy comments as well. */ } /* loop's spin() method. It assumes the loop class data values have been assigned already; and it can't be called until a loop class object has been instantiated through the constructor below. spin() takes one integer argument - note that changing the capitalization of a variable name makes it a new name. Thus increment is a class loop data variable, and Increment is a spin() argument that only exists within the spin() method. */ void spin(int Increment) { /* A variable local to the spin() method, and local to this instance of the loop class as well. */ int i; /* Loop, control, and mathematical statements are pretty much exactly the same in Java as in C and C++. In Fortran, this would look like "do i=0,limit,Increment". limit is not local to this method, but local to this instance of the loop class. */ for (i=0; i;" statement is not needed since spin() is void and returns nothing. */ } /* Constructor for an instance of the class loop object; class data get values assigned to them here, and non-static arrays may be dimensioned as well. This loop class constructor requires one int argument, and returns a reference to the new class loop object. */ loop(int Limit) { increment = 1; limit = Limit; /* Note that the class data value limit is distinct from the constructor's Limit argument: changing the capitalization means it is a different variable; and the argument won't be saved after construction finishes unless it is assigned (copied) to class data. */ value = 10.0F; /* Note that numbers assigned to float rather than double variables must have an f or F after them. */ farray = new float[limit]; /* An array can't be used until it has been dimensioned with this kind of constructor call. */ } /* Here we make a second definition of loop's constructor that takes no arguments. Java distinguishes which constructor to call by the arguments you call it with - this is called polymorphism. Methods can also be polymorphic. It is a good idea for classes to have a no-arg constructor if you are going to extend them with sub-classes. It isn't needed here. */ loop() { increment = 1; limit = 10; value = 10.0F; farray = new float[limit]; } /* End of all the data and methods defined for the class loop objects. */ } /* After loop, we also define another class, and running the file through javac will produce two .class binary object code files, one for each class defined. This object sub-classes or extends the class Applet object. looplet thus inherits all the data, method, and constructor definitions of all Applets, and can add new definitions. Further, it can re-define data and methods already in Applet, called overriding. */ public class looplet extends Applet { /* The class looplet data we need to add to the Applet superclass's data (of which there is a lot) is simply a reference to the loop class object we will pass between looplet's methods. Suppose a web page contains a dozen looplet applets? Each one will run separately (and in parallel) in its own execution thread, with its own instance or copy of the class looplet data, since it is not declared static. */ loop loopobject; /* In an Applet we do not need to define a main() method or constructors. Since there is no main(), running "java looplet" with the compiled looplet.class file will not do much of anything. To do more, we just need to override the methods init() and paint(). We will not bother here to override the run() and stop() methods, which the web browser calls when it enters and leaves the web page containing the applet. */ /* The paint() method is called by the browser whenever the applet's area on the screen is first shown (thus it gets called by run() ), or needs to be refreshed. The class Graphics g instance only exists when the applet has started displaying, and knows how and where it will be plotting. We are overriding the paint() method defined in the Applet superclass. In the superclass it does nothing; we override it since in a web browser the only good way for the applet to show its existence is to plot something, or play a sound. */ public void paint(Graphics g) { /* Since paint is never called until after init() has finished, we already have instantiated our class loop object. The spin() method itself doesn't produce any graphics - the standard output will appear in the browser's Java Console window. */ loopobject.spin(loopobject.increment); /* Add some graphical action. The integer arguments are number of pixels to the right and down from the upper left corner of the applet panel. */ g.drawString("Number of elements in farray: " + loopobject.farray.length, 10, 30); /* Note the reference to the length data field of the farray data array in the loopobject object. Expression evaluation is strictly left-to-right in Java. */ } /* The init() method is called by the web browser when it first encounters the applet, before trying to paint it. If classes are to be instantiated or data obtained (over the net), this is the place. We use it to make our loop object. */ public void init() { loopobject = new loop(5); /* Give the looplet panel a different color so we can see it against the rest of the web page. */ setBackground(Color.cyan); } /* End of the definition of the class looplet applet. We also need to write an HTML web page that references the applet. It will probably be called looplet.html . */ }