201310261330


Java Begins
-----------

    Einstein has one of his students stand up 15 minutes into an exam saying, "The questions in this year's exam are the same as last year's exam." Einstein replied, "Don't worry, the answers are different this year."
    
Why English?
------------

Circle is closed geometric figure, however i'd like to use code written outside ring time gap somewhere else. There are more useful reading in English, especially one that will never be translated otherwise than with help of translating robots (currently the quality of translation is pretty poor). I'd like to make this talk understandable to a wider auditorium.


Why should i read this?
-----------------------

The only reason to write down things already explained many times is to exclude "search" task from reading of the text given (to have everything necessary in one place).

If something complex can be used by pushing single button or so, we may call it the way Grady Booch did -- encapsulated complexity. But how complex parts of the program are usually hidden? It is translated into machine language (precompiled). Well, hackers can handle this 0011010001 mumbo-yumbo pretty well (so called cracks). It is encrypted. Well encrypted executable decrypts itself in RAM, so why to read this in file (better cracks)? Sometimes it's physically embedded -- this way is slightly better, access to decrypted code costs time and money here. Sometimes it's installed on remote computer (which is let's say 15410000000 km from earth [2]) -- even better than locally embedded case.

Sometimes is useful to distribute complex projects so, that not only qualified hackers are allowed to make changes to a "hidden" part. This is what we call open source project.

Java itself, BlueJ, Greenfoot, sample projects used here, they all are opensourced, only the form of license differs. So here comes fundamental rule number zero for opensource beginner:

  /**
  +------------------------------------------------+
  | Tatzupaos 0: WRITE/CHANGE YOUR CODE ONLY FROM: |
  |                                                |
  |        // vvvvvvvvvvvvvvvvvvvvvvvv             |
  |                                                |
  | TO:                                            |
  |                                                |
  |        // ^^^^^^^^^^^^^^^^^^^^^^^^             |
  +------------------------------------------------+ 
  */


Expectations
------------

Asking question, one may expect something pretty-much different from the sentence answered. Java style usually assumes we have some priorities, tigtly correlated with statistics among human population: [4]

  -- How it is useful (highest, java programmer)?
  -- How does it work (mid for java programmer, highest for hacker)?
  -- Why is this working at all (low for java programmer, highest for physicist)?

Enough yackety-yack (talkie-talkie).


Comments:
---------

  // Single line comment
  
  /* C-style multiline comments, still accepted in Java
          */
  
  /**
   * Java-style multiline comments
   */


Variables:
----------

Variable is a named space in RAM to store some chunk of data of limited size. Variable type must be defined first. If you'll ask about how much memory one or another variable takes in memory -- Java style answer is: Why do you need to know this? For primitive types (enumerated data like integers or alphabets) only a range and default value are important.

  +-----------+----------+-------------------+---------+
  | Data Type |    min   |       max         | default |
  +-----------+----------+-------------------+---------+
  | boolean:  |  false   |      true         |  false  |
  |           |          |                   |         |
  | char:     |'\u0000'  |   '\uffff'        |  \u0000 |
  |           |          |                   |         |
  | byte:     |    -128  |        127        |  0      |          
  | short:    |  -32768  |      32767        |  0      |
  | int:      | -2 ^ 31  | 2 ^ 31 - 1        |  0      |
  | long:     | -2 ^ 63  | 2 ^ 63 - 1        |  0L     |
  +-----------+----------+-------------------+---------+
  | float:  32-bit IEEE 754 floating point.  |  0.0f   |
  | double: 64-bit IEEE 754 floating point.  |  0.0d   |
  |                                          |         |
  | String (or any object)                   |  null   |
  +-----------+----------+-------------------+---------+
  
  Notes:  0b11010 binary literal
          0xfc    hexadecimal literal
          (char)  cast to char
          2 ^ 3   means  2 * 2 * 2 only in this table, NOT IN JAVA
  

Operators:
----------

The following quick reference summarizes the operators supported by the Java programming language.

  +-----------------------------------+-------------------------------+
  | Simple Assignment Operator        | =                             |
  | Arithmetic Operators              | +   -   *   /   % (remainder) |
  | Unary Operators                   | +   -   ++  --  !             |
  | Equality and Relational Operators | ==  !=  >   >=  <   <=        |
  | Conditional Operators             | &&  ||  ?:                    |
  | Type Comparison Operator          | instanceof                    |
  | Bitwise and Bit Shift Operators   | ~   <<  >>  >>> &   ^   |     |
  +-----------------------------------+-------------------------------+
    
Operator Precedence 

  +----------------------+--------------------------------------------+
  | Operators            | Precedence                                 |
  +----------------------+--------------------------------------------+
  | postfix              | expr++ expr--                              |
  | unary                | ++expr --expr +expr -expr ~ !              |
  | multiplicative       | * / %                                      |
  | additive             | + -                                        |
  | shift                | << >> >>>                                  |
  | relational           | < > <= >= instanceof                       |
  | equality             | == !=                                      |
  | bitwise AND          | &                                          |
  | bitwise exclusive OR | ^                                          |
  | bitwise inclusive OR | |                                          |
  | logical AND          | &&                                         |
  | logical OR           | ||                                         |
  | ternary              | ? :                                        |
  | assignment           | = += -= *= /= %= &= ^= |= <<= >>= >>>=     |
  +----------------------+--------------------------------------------+


Expressions:
------------
         ---+----- Operators
        |   |  +-- end of statement
        v   v  v
  int a = 2 + 3; 
      ^   ^   ^
      |   +---+--- Numeric Literals
       ----------- Variable
       
       
Blocks:
-------

{} Block encapsulates part of the code. Outside the block it's body looks like a single statement. Compare:
  
  if ( x > 2) {      if ( x > 2)
      a = 5;             a = 5;
      b = 1;             b = 1;
  }


Functions:
----------

  +--------------------------------------------- visible
  |      +-------------------------------------- to every instance
  |      |      +------------------------------- returns int
  |      |      |   +--------------------------- is method
  |      |      |   |        +------------------ method's name
  |      |      |   |        |       +-------+-- two arguments of type int
  v      v      v   v        v       v       V
  public static int function intsum( int aa, int ab) {
      // Write code here
      return 0;
  }


Next..
------

Approach, shown in Tatzupaos Zero exactly matches the way, chosen in [5] (Learn Java in 3D lessons). Also a shorter path to Android device is shown there: 0 -> Greenfoot -> BlueJ + Android SDK + Env3D. Another approach seems not very practical ( but without doubt is cool and curious) is to learn programming Java on your Android device [6]. It's also opensource project [7].

At this point, i believe, rubber frog may be flooded. Use search engines.


Links
-----

[1]. http://articles.economictimes.indiatimes.com/2012-07-16/news
     /32698515_1_employability-apprenticeship-regime-apprenticeship-act
[2]. http://en.wikipedia.org/wiki/Voyager_2
[3]. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
[4]. Citation needed.
[5]. http://env3d.org/beta/
[6]. DroidDevelop on http://www.en.assoft.ru
[7]. DroidDevelop source code: http://code.google.com/p/droiddevelop/

------------------------------------------------------------------------

Appendix A. Exercises.

Exercise 1:

  Calculate  1 + 2 * 3 and show result on 7-SEG display.
  
Scenario. EX1 - In Greenfoot create empty project called EX1. Create default subclasses for World and Actor named SevenWorld and SevenSeg respectively. Replace default code with one below. Compile and save.
  
SevenWorld.java

  import greenfoot.*;

  public class SevenWorld extends World {

      public SevenWorld() {    
          super(600, 400, 1);
          addObject( new SevenSeg(), 125, 125);
      }
  }

SevenSeg.java

  import greenfoot.*;
  import java.awt.Color;

  public class SevenSeg extends Actor
  {
  //    00       -- 
  //   5  1     |  |
  //    66       --
  //   4  2     |  |
  //    33       -- 
  // 
  //    00       --       22       33       --       55       66       77      
  //   0  0     |  1     |  2     |  3     4  4     5  |     6  |     |  7     
  //    --       --       22       33       44       55       66       --      
  //   0  0     |  1     2  |     |  3     |  4     |  5     6  6     |  7     
  //    00       --       22       33       --       55       66       --      
  //  
  // 01111110 00001100 10110110 10011110 11001100 11011010 11111010 00001110
  // 7e       0c       b6       9e       cc       da       fa       0e      
  // 
  //    88       99       AA       --       cc       --       EE       FF   
  //   8  8     9  9     A  A     b  |     c  |     |  d     E  |     F  |  
  //    88       99       AA       bb       --       dd       EE       FF   
  //   8  8     |  9     A  A     b  b     c  |     d  d     E  |     F  |  
  //    88       99       --       bb       cc       dd       EE       --   
  //                                                                        
  // 11111110 11011110 11101110 11111000 01110010 10111100 11110010 11100010 
  // fe       de       ee       f8       72       bc       f2       e2     
  // 
  //    GG       --       ii       jj       --       --       --       --   
  //   G  |     h  |     |  |     |  |     K  K     l  |     M  M     |  |  
  //    --       hh       --       --       KK       --       --       nn   
  //   G  G     h  h     i  |     |  j     K  K     l  |     M  M     n  n  
  //    GG       --       --       jj       --       ll       --       --   
  //                                                                        
  // 01111010 11101000 00100010 00011010 11101100 01110000 01101100 10101000 
  // 7a       e8       22       1a       ec       70       6c       a8     
  // 
  //    --       pp       qq       --       ss       --       --       --   
  //   |  |     p  p     q  q     |  |     s  |     t  |     u  u     |  |  
  //    oo       pp       qq       rr       ss       tt       --       --   
  //   o  o     p  |     |  q     r  |     |  s     t  |     u  u     v  v  
  //    oo       --       --       --       ss       tt       uu       vv   
  //                                                                        
  // 10111000 11100110 11001110 10100000 11011010 11110000 01111100 00111000 
  // b8       e6       ce       a0       da       f0       7c       38     
  // 
  //    --       --       --       zz    
  //   w  w     |  |     y  y     |  |   
  //    ww       --       yy       zz    
  //   w  w     x  x     |  y     |  |   
  //    ww       --       yy       zz    
  //                                     
  // 11111100 00101000 11011100 10010010 
  // fc       28       dc       f8      

    int[] i2c = { 0x7e, 0x0c, 0xb6, 0x9e, 0xcc, 0xda, 0xfa, 0x0e, 
                  0xfe, 0xde, 0xee, 0xf8, 0x72, 0xbc, 0xf2, 0xe2,
                  0x7a, 0xe8, 0x22, 0x1a, 0xec, 0x70, 0x6c, 0xa8,
                  0xb8, 0xe6, 0xce, 0xa0, 0xda, 0xf0, 0x7c, 0x38,
                  0xfc, 0x28, 0xdc, 0x92 };

      int count = 0;
      GreenfootImage img;
      
      public SevenSeg() {
          img = new GreenfootImage( 50, 100 );
          // setImage( img); // ONLY IF NO STATIC IMAGE IS SET IN GUI
          // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
          // define and calculate variable A here:

          // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          Pa( i2c[ A]);
      }
      
      public void Pa(int acount) {
          setImage( img);

          img.clear();
          img.setColor( new Color( 0, 0, 0));
          img.fill();
          img.setColor( Color.RED);

          if ((acount & 0x02) != 0) {
              int[] x7 = {6, 16, 35, 45};
              int[] y7 = {5, 15, 15, 5};
              img.fillPolygon( x7, y7, 4);
          }
   
          if ((acount & 0x04) != 0) {
              int[] x6 = {37, 47, 47, 37};
              int[] y6 = {17, 7, 48, 39};
              img.fillPolygon( x6, y6, 4);
          }
          
          if ((acount & 0x08) != 0) {
              int[] x5 = {37, 47, 47, 37};
              int[] y5 = {61, 51, 93, 83};
              img.fillPolygon( x5, y5, 4);
          }

          if ((acount & 0x10) != 0) {
              int[] x4 = {6, 16, 35, 45};
              int[] y4 = {95, 85, 85, 95};
              img.fillPolygon( x4, y4, 4);
          }
          
          if ((acount & 0x20) != 0) {
              int[] x3 = {4, 14, 14, 4};
              int[] y3 = {52, 61, 83, 93};
              img.fillPolygon( x3, y3, 4);
          }
          
          if ((acount & 0x40) != 0) {
              int[] x2 = {4, 14, 14, 4};
              int[] y2 = {7, 17, 38, 48};
              img.fillPolygon( x2, y2, 4);
          }

          if ((acount & 0x80) != 0) {
              int[] x1 = {7, 13, 37, 43, 37, 13};
              int[] y1 = {50, 44, 44, 50, 56, 56};
              img.fillPolygon( x1, y1, 6);
          }

      }
      
      public void act() 
      {
          // if ( Greenfoot.mouseClicked(this)) {
          //     System.out.println("CLICK" + count);
          //     count = count < 0x23 ? count + 1 : 0;
          // }
          //
          // Pa( i2c[ count]); 
      }
  }

------------------------------------------------------------------------

Exercise 2. The fly

Scenario. FLY - In Greenfoot create empty project called FLY. Create default subclasses for World and Actor named FlyWorld and Fly respectively. Replace default code with one below. Compile and save.

FlyWorld

  import greenfoot.*;

  public class FlyWorld extends World {
      public FlyWorld() {    
          super(1024, 768, 1);
          addObject( new Fly(), this.getWidth()/2, this.getHeight()/2);
      }
  }

Fly

Part of the functionality provided by this class is the ability to retrieve keyboard input. The methods getKey() and isKeyDown() are used for this and they return/understand the following key names:

  - "a", "b", .., "z" (alphabetical keys), "0".."9" (digits), most punctuation marks. getKey() also returns uppercase characters when appropriate.
  - "up", "down", "left", "right" (the cursor keys)
  - "enter", "space", "tab", "escape", "backspace", "shift", "control"
  - "F1", "F2", .., "F12" (the function keys)

  import greenfoot.*;

  public class Fly extends Actor
  {
      public Fly() {
          setRotation( 270);
      }
      
      public void act() {
          if (Greenfoot.isKeyDown("up")) move(1);
          if (Greenfoot.isKeyDown("left")) turn(-1);
          if (Greenfoot.isKeyDown("right")) turn(1);
          if (Greenfoot.isKeyDown("down")) move(-1);
          
          if ( Greenfoot.mouseClicked(this)) move(40);
      }    
  }

------------------------------------------------------------------------

Exercise 3. Fly eating.

  public void act() {
      if( foundMushroom()) eatMushroom();
  }

  public boolean foundMushroom() {
      Actor mushroom = getOneObjectAtOffset(0, 0, Mushroom.class);
      return (mushroom != null) ? true : false;
  }
  
  public void eatMushroom() {
      Actor mushroom = getOneObjectAtOffset(0, 0, Mushroom.class);
      if ( mushroom != null) getWorld().removeObject( mushroom);
  }
