CMPE 261 - Lab worksheet - Week 1-2

Java basics

  1. Using an editor enter the simple Java command line program below and save as ""

    /** A simple program to print hello message and arguments*/
    class HelloWorld {
        public static void main(String[] args) {
            if (args.length==0)
                System.console().format("You have given me no arguments.\n");
            else {
                for(int i=0; i<args.length; i=i+1)
                    System.console().format("Argument %d is %s!\n", i, args[i]);
  2. Compile and run the file, and create documentation:

    $ javac
    $ java HelloWorld
    $ javadoc -d docs -sourcepath .


Write a Java command line program to take an integer \(n\) as the first command line parameter, and print factorials of numbers from 1 to \(n\). Make sure to compute factorial in a separate function. Comment your program.