Aegidius
 Plüss   Aplulogo
     
 www.aplu.ch      Print Text 
© 2021, V10.4
 
  JavaOC
 
 


Examples for the very first Java lesson using JavaOC:

Do some simple calculations:

  // MyFirst.java

class MyFirst
{
  MyFirst()
  {
    int items = 1234;
    double price = 2.55;
    double toPay = items * price;
    System.out.print(toPay);
  }
}
 

For a quick demonstration without an IDE you compile from a command shell with javac MyFirst.java and run the program with javaoc MyFirst. You may use constructors with primitive or string type parameters:

  // MyFirstP.java

class MyFirstP
{
  MyFirstP(int items, double price)
  {
    double toPay = items * price;
    System.out.print(toPay);
  }
}
 

and pass the values as command line arguments, e.g.
javaoc MyFirstP 1234 2.5 .

You better use simple helper classes like ch.aplu.util.Console (see www.aplu.ch/java):

  // MyFirstC.java

import ch.aplu.util.*;

class MyFirstC extends Console
{
  MyFirstC()
  {
    print("Number of items: ");
    int items = readInt();
    print("Unit price: ");
    double price = readDouble();
    double toPay = items * price;

    print("Amount to pay: " + toPay);
  }
}
 

which gives you a full-blown GUI application:

To let your students grow up in the OOP-world from the very first hour, use Logo-like turtles with classes ch.aplu.turtle.* (see www.aplu.ch/java):

  // JagTurtle.java

import ch.aplu.turtle.*;

class JagTurtle extends Turtle
{
  JagTurtle()
  {
    for (int i = 0; i < 4; i++)
    {
      jag();
      right(60);
    }
    fill(-1010);
  }

  void jag()
  {
    forward(100);
    left(150);
    forward(100);
  }
}
 

which declares a JagTurtle who "is-a" Turtle with a new "jag"-facility and make it draw a star:

 
 
 

 

There is no need of a main-method from a logical point of view. To run a program, just use a small operating system utility that creates an object instance.

 
How it works
JavaOC parses the command line and uses JNI (Java Native Interface) to create first a Java VM (Virtual Machine) and then an instance of the given Java class while passing the command line arguments to the constructor found by reflection.