CMPE 261 - Lab worksheet - Week 1-2

Java basics

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

    /** A simple program to print hello message and arguments*/
    class HelloWorld {
        public static void main(String[] args) {
            System.console().format("Hello!\n");
            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 HelloWorld.java
    $ java HelloWorld
    $ javadoc -d docs -sourcepath . HelloWorld.java

Exercise

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.