A Tour of JJ: Maximum Surprize

This page offers a quick tour of the JJ language intended for instructors mainly. This is because there is little emphasis on the example code, for it is assumed that instructors are familiar with similar languages.

The code describes a rather common algorithm, but it has a surprizing twist, which is not commonly known. The code serves also to illustrate many aspects of the JJ language.

The algorithm finds the maximumvalue of any number of values, in this case 100 values. It uses a random number generator which is imported from the Math class; it returns real values between 0.0 and 1.0. The entire code follows; note that there is no class, no method, and no

public static void main (String[] args)

Such concepts come later, not in this early program. First things first!


// Name Ann Onymous
// Does find maximum of many (with surprize!)

   int num; // number of values
   double max, value;

   max = Math.random();
   num = 100; // whatever

   while (num > 0) {

      value = Math.random();

      if (max < value) { 
      // update the max
         max = value;
         JJS.outputln (max);
      }//end if

      num = num - 1;
   }//end while
   
   JJS.outputString ("The maximum is ");
   JJS.outputlnInt (max);

Notice that this code looks much like pseudo-code; it is quite readable. The "if" has a paired "//end if", the "while" has an ending "//end while" and similarly classes, constructors and methods have corresponding unique endparts. The matching endparts are required in JJ in order to provide for good error detection, which is very important for beginners.

The beginning left brace "{" and the ending right brace "}" are required in JJ even if there is only one command between them, for this again helps error detection, and avoids the "dangling else" problem, which occurs in C, C++, Java, and even Pascal.

JJ is case-sensitive so that the word "max" diffes from "MAX", which differs from "Max". Additionally in JJ, this difference in upper/lower case should not be used to create names which only differ in the case; i.e. never use both Max and max as two different names.

Indentation is important, so the endpart of the if must be indented the same amount as the "}//end if" (and the same for all the other similar matching end pairs).

Comments follow a double slash "//" to the end of the line; there are no other ways to comment in JJ.

Boxes (or variables) are of type int, double, or boolean; there is no char type, for characters are simply short objects of class String. The word "box" is often used in JJ rather than variable, to avoid the confusion that sometimes occurs between the mathematical concept of the same name.

Note especially that this code need not be "encased" within a method, and that method need not be within a class. That comes a bit later; simplicity first.

Before running this code you may wish to anticipate the results of a run. Try to estimate, the number of times (out of 100) that the max value will be updated. In other words, how often inside of the If part will the random value make the condition be true, and so reset the max to that value.

Take a guess: pessimists will say that this reset will occur often, thus running slow; optimists will say the reset will occur seldom, thus running faster. Others will say that there is no reason for either path, so the reset will occur about half the time. What do you say? Choose something! Pick a number from 0 to 100. Now! You will be surprized.

To use the JJ environment you must first register (to open a JJ accout and pick a password) before accessing the system.