Programming Practice (vs Principles) Programming in any language involves syntax (with rules, parentheses, etc) as well as format (layout, spacing, style, etc). Syntax is usually very strict, and any small error in spelling or punctuation will prevent a program from running. Format is not as important to the computer, but good spacing and consistent indentation can help humans to read, use and change the code. Java was designed for professional programmers; it was not designed for beginners. JJ is a junior version of Java which was designed for beginners. JJ is a true subset of Java, with a simple environment and many friendly error messages to help beginners. It does not give error messages about things that are not in the subset, so does not confuse beginners. To help beginners JJ has a few rules, which differ slightly from those of Java. If these rules are followed then every program written in JJ will also run on any Java compiler. In fact following these rules will result in better programs in Java. JJ requires that two or more commands not be written on one line. This is just good programming practice in Java anyhow. JJ requires a matching left and right curly brace ( { and } ) even when there is only one line between the braces. So when this occurs in Java, which is rare actually, the brackets may be dropped; but this may cause a dangling else problem which never can occur in JJ. So including these braces is also good programming practice in Java,
Beginning One of the first programs in any language is usually to output In Java this First program appears quite complex, as shown below. class First { } public static void main (String[] args) { } System.out.println ( Hello World ); In JJ this entire program can be done as a single line: JJS.outputString ( Hello World ); Hello World. This Java program involves 3 kinds of parenthese, and many advanced concepts such as classes, methods, strings, arrays, and some words (public, static, void, main, System, args, etc) which cannot be understood easily at this early point. JJ introduces the main ideas of Java in gentle stages. Here outputstring is imported from JJS (JJ system) and it prints out the string Hello World which is between the brackets. That s all.
A Simple Second Program: with some computation (multiplication) follows; code is at the left; an execution or run is at the right in a dotted box. // Does simple computation with integers JJS.outputString ("10*9*8*7*6*5*4*3*2*1 = "); JJS.outputlnInt ( 10*9*8*7*6*5*4*3*2*1 ); 10*9*8*7*6*5*4*3*2*1 = 362880 Comments follow two slashes (//) to the end of the line. They can provide a description (documentation) but are ignored by the computer. Strings are the contents between quotes ( ). The outputstring which is imported from the class JJS (JJ System) prints the string within the quotes exactly. Multiplication is a mathematical operation which is indicated by an asterisk (*). outputlnint, (spoken as output line integer ) is a command or action which evaluates the arithmetic integer expression, prints its value and then outputs a new line. outputint is a similar action; it outputs the expression but does not follow with a new line ( ln), so that any later output occurs on the one same line. Semicolons are required after each command; they are not needed after comments. Case sensitivity refers to upper and lower case letters being different, so outputstring must begin with lower case o and the inner word String must begin with a capital S. Similarly JJS must be all caps; it is not jjs, Jjs, jjs or even J J S.
Program Structure Programs consist of 3 parts: Description (or documentation) Declaration (of data and their types) Directives (or commands, instructions) An example of a program in JJ follows in the box below where the three parts are separated by dashed lines. Runs (or executions) of this code are shown in a dotted box. // Name An Omynous // Does compute the circumference given the radius // Shows a typical simple computation with double type public static final double PI = 3.14159; // constant double radius; // variable JJS.outputString ("Enter the radius : "); // prompt radius = JJS.inputDouble (); // input JJS.outputlnDouble (radius); // echo JJS.outputString ("The circumference is "); JJS.outputlnDouble ( 2.0 * PI * radius ); Enter the radius : 1.0 The circumference is 6.28318 Enter the radius : 0.0 The circumference is 0.0 Enter the radius : -10.0 The circumference is -62.8318
Description or documentation is not required, but is nevertheless useful. It consists of a number of comments, beginning with two slashes (//) and continuing to the end of the line. Comments should include who wrote the code, what the code does, and possibly describing why, when, where but not necessariy how, for the code shows how. Declaration of data associates names with boxes of a specified type. Variable declarations consist of lines beginning with a type (int, double, etc) and followed by the names of boxes of that type. Sometimes comments may be used to clarify the names and allow shorter names to be used. Examples of Declarations are: int count; double radius, length, width, height; // size in inches Constants are named in all capitals (with underscores possible) and set to a value in the form: public static final <type> <name> = <value> ; as in the examples: public static final double PI = 3.14159; // approximate public static final int YEAR = 2001; // present year public static final String PROMPT = Enter input : ; public static final int FEET_PER_MILE = 5280; Directives or (or actions, instructions) are commands, such as input, output, set (assign) or some constructs (Choice, Loop) or call of some sub-program (method, function, routine). They all end with semi-colons.
Convert is a program which converts a real temperature (of type double) from degrees Celsius into degrees Fahrenheit; Some runs are shown at the right. // File Convert.jj // Does Convert temperatures c to f double c; // temperature Celsius double f; // temperature Fahrenheit JJS.outputString ("Enter degrees C : "); c = JJS.inputDouble (); f = 9.0 / 5.0 * c + 32.0; JJS.outputString ("Degrees F is: "); JJS.outputDouble ( f ); Enter degrees C: 100.0 Degrees F is: 212.0 Enter degrees C: 0.0 Degrees F is: 32.0 Enter degrees C: -40.0 Degrees F is: -40.0 Testing, at the right shows some interesting values: that -40 is the same value in Celsius and Fahrenheit. Modify this code to convert from Fahrenheit to Celsius. Test other values (such as 0 degrees f). Does the midpoint 50c map to mid 122f?
BirthYear is a program which asks for a person s age, and indicates the year that this person was born; it should be used carefully for older females. // File BirthYear.JJ // Does tell birth year from age public static final int YEAR = 2001; // now int age ; // in years JJS.outputString ("Enter an age : "); age = JJS.inputInt (); JJS.outputString ("You were born around: "); JJS.outputlnInt (YEAR - age); Enter an age : 20 You were born around: 1981 Enter an age : 0 You were born around: 2001 Enter an age : 39 You were born around: 1962 Code a similar program to input the birth year and then output the age.
SplitTime converts a given military time into a non-military statement of the time. It uses the integer divide (/) and mod or remainder (%) operations. // Program SplitTime.JJ II // Does split military time // shows use of div (/) and rem (%) int miltime, hour, mins; JJS.outputString ("Enter military time: "); miltime = JJS.inputInt (); // JJS.outputlnInt (miltime); hour = miltime / 100; mins = miltime % 100; JJS.outputString ("It is "); JJS.outputInt (mins); JJS.outputString ( minute(s) \n after "); JJS.outputlnInt (hour); NOTE the \n within the string creates a new line on the output. Enter military time: 1234 It is 34 minute(s) after 12 Enter military time: 2001 It is 1 minute(s) after 20 Enter military time: 9999 It is 99 minute(s) after 99 Enter military time: -1234 It is -34 minute(s) after -12 Notice that bad input (such as 9999) gives bad output; Garbage in; Garbage out! Later we will test to verify that the input is proper (hours < 24 and minutes < 60).
TimeConvert of minutes into days, hours and minutes is done in two ways as shown. // Does convert minutes to hours, days.. int days, hours, mins; JJS.outputString ("Enter minutes: "); mins = JJS.inputInt (); hours = mins / 60; mins = mins % 60; days = hours / 24; hours = hours % 24; JJS.outputString ("Days = "); JJS.outputlnInt (days); JJS.outputString ("Hours = "); JJS.outputlnInt (hours); JJS.outputString ("Minutes = "); JJS.outputlnInt (mins); Data Flow Diagrams (DFDs) can be very useful here to See the structure. Draw them. days = mins / (24*60); mins = mins % (24*60); hours = mins / 60; mins = mins % 60; Enter minutes: 2000 Day = 1 hours = 9 Minutes = 20 Modify this code to input the number of seconds; test for a million and a billion. Modify this code to output also the number of weeks. Convert similarly cups to gallons, quarts and pints; Do this in 3 different ways.
Strings are sequences of characters; they are similar to types, but are objects of a class String. the values of strings are written between a double quote mark. the joining or concatenation of two strings is done by re-using the plus sign (+). // Does format people s names public static final String COMMA = ","; public static final String SPACE = " "; String first, last; // names of people JJS.outputString ("Enter first name : "); first = JJS.inputString (); JJS.outputString ("Enter last name : "); last = JJS.inputString (); JJS.outputString ("The full name is ); JJS.outputString (first + SPACE + last); JJS.outputString ( In directory form "); JJS.outputString (last + COMMA + SPACE + first); Enter first name : Bill Enter last name : Gates The full name is Bill Gates In directory form Gates, Bill Enter first name : Ben Enter last name : Dover The full name is Ben Dover In directory form Dover, Ben Modify this code to include a middle initial of a name, and a constant PERIOD, so that Bill Q. Gates in directory form is output as Gates, Bill Q..
MeanSize shows many ways to compute the mean or average number of children per family. It shows a mix of ints and reals, and ways of converting between them. // Does find mean children per family // Shows a mix of int and double types int children, families, intmean, roundmean; double realmean; JJS.outputString ("Enter number of children: "); children = JJS.inputInt (); JJS.outputString ("Enter number of families: "); families = JJS.inputInt (); intmean = children / families; //chops realmean = JJS.intToDouble (children) / JJS.intToDouble (families) ; roundmean = JJS.doubleToInt (realmean + 0.5 ); Enter number of children : 180 Enter number of families : 100 Children per family are : 1 1.8 2 Enter number of children : 100 Enter number of families : 300 Children per family are : 0 0.333333333333333 0 JJS.outputString ("Children per family are "); JJS.outputlnInt (intmean); JJS.outputlnDouble(realMean); JJS.outputlnDouble(roundMean);
power is a function pow (x,n) which provides the value of x raised to the power y, where both x and y are real (doub le) values. Reliability (probability of success) of a series of n independent parts, each with reliability p, is p to the power of n; or p multiplied by itself n times. Code doing this follows. // Does compute system reliability int num; // number of components double prob; // probability of each JJS.outputString ("Enter number of parts: "); num = JJS.inputInt (); JJS.outputString ("Enter each reliablity "); prob = JJS.inputDouble (); JJS.outputString ( Total reliability is "); JJS.outputDouble (Math.pow (prob, num) ); Enter number of parts : 10 Enter each reliability : 0.9 Total reliability is 0.348674 Enter number of parts : 10 Enter each reliability : 0.99 Total reliability is 0.904382075 Other functions that are similarly available from the Math class include: abs(i), abs(d), sqrt(d), rand() as well as others, including trig functions (sin(x), cos(x), tan(x) ).
random is a function, imported from the Math class, which provides a real value (type double) between 0.0 and 1.0 which appears to occur by chance. DiceRolls shows how the random function can simulate the roll of one die, which has an integer value from 1 to 6. Then the throw of two such dice is done providing values from 2 to 12. Not all 11 values occur equally often; infact 7 occurs most often and 2 and 12 occur least often. // simulate sum of two dice int first, second, sum; double prob; // 0.0 <= prob < 1.0 prob = Math.random(); first = JJS.doubleToInt (6.0 * prob) + 1; prob = Math.random(); second = JJS.doubleToInt(6.0 * prob) + 1; sum = first + second; JJS.outputString ("Dice sum is "); JJS.outputInt (sum); Dice sum is 12 Dice sum is 11 Dice sum is 6 Dice sum is 5 Dice sum is 7 Dice sum is 2 Dice sum is 8 Dice sum is 7