Background information:   executing Java programs

  • Background information:

    • We use BlueJ to show how a Java program works more clearly

    • Java programs are not usually executed using BlueJ !!!

    • Java programs are usually executed from a terminal

  • Demo on how Java programs are usually executed:

      cd ~/c/OutSchool/CS/AP-CS/demo/08-array/15-cmd-line-args
    
      javac Hello.java     // Compile the Java program
      java  Hello          // Run/execute the Java program
      

DEMO: demo/08-array/15-cmd-line-args/Hello.java

Command line arguments

  • When a Java program is executed in a terminal using a command java progName, we can provide additional command line arguments as follows:

      java progName arg1  arg2  arg3 ...  

  • Command line arguments are parameters passed to the Java program !

  • The command line arguments are received by the main( ) method:

        public static void main( String[] args )
        {
            ....
        }
       

    The parameter args of the main( ) method contains the command line arguments

Command line arguments   demo

  • Here is a Java program that shows that the command line arguments are passed to the variable args of the main( ) method:

    public class CommandArgs
    {
        public static void main( String[] args )
        {
            // Print the values in the String array args
            for ( int i = 0; i < args.length; i++ )
                System.out.println("args[" + i + "] = " + args[i]);
        }
    } 

  • After compiling the program, you can run it as:

      java CommandArgs a b c 1 2 3

    The program will print out the arguments (on separate lines)

DEMO: demo/08-array/15-cmd-line-args/CommandArgs.java

Using numbers in as command line arguments

  • The main( ) method can use the command line arguments like any Java method

  • Important note:

    • The command line arguments is an array of String:

    Notice the data type of args:

        public static void main( String[] args )
        {
            for ( int i = 0; i < args.length; i++ )
                System.out.println("args[" + i + "] = " + args[i]);
        } 

  • That means:

    • Numbers are passed as number strings

    • You must use Integer.parseInt() or Double.parseDouble() to convert them before you can use the numbers in computations !!

Using numbers in as command line arguments   Demo: calculator program

  • Exercise:

    • We will write a Java program that performs arithmetic operations on integers.

  • The Java program receives an simple expression as its command line argument

  • The simple expression consists of:

    1. an integer
    2. an arithmetic operator (+, , * or /)
    3. a second integer

    Example:

       java Calculator   2   +   3  

    The program will print the result of the expression

Using numbers in as command line arguments   where are the input arguments ?

  • This program shows you where the input arguments are passed to the main( ) method:

    public class Calculator1
    {
        public static void main( String[] args )
        {
            System.out.println("args[0] = " + args[0]);
            System.out.println("args[1] = " + args[1]);
            System.out.println("args[2] = " + args[2]);
        }
    } 

  • Try:

      java Calculator1  2 + 3
      java Calculator1  9 - 6 

DEMO: demo/08-array/15-cmd-line-args/Calculator1.java

Using numbers in as command line arguments   the Calculator program

Let's write the Calculator program:

public class Calculator
{
    public static void main( String[] args )
    {
        int a = Integer.parseInt( args[0] ); // First number
        int b = Integer.parseInt( args[2] ); // Second number
	int result = 0;

	switch ( args[1].charAt(0) )
	{
	   case '+' -> result = a + b;
	   case '-' -> result = a - b;
	   case '*' -> result = a * b;
	   case '/' -> result = a / b;
        }

	System.out.println(a + " " + args[1] + " " + b + " = " + result);
    }
} 

Using numbers in as command line arguments   the Calculator program

(1) Convert the first number string in args[0] to an integer a:

public class Calculator
{
    public static void main( String[] args )
    {
        int a = Integer.parseInt( args[0] ); // First number
        int b = Integer.parseInt( args[2] ); // Second number
	int result = 0;

	switch ( args[1].charAt(0) )
	{
	   case '+' -> result = a + b;
	   case '-' -> result = a - b;
	   case '*' -> result = a * b;
	   case '/' -> result = a / b;
        }

	System.out.println(a + " " + args[1] + " " + b + " = " + result);
    }
} 

Using numbers in as command line arguments   the Calculator program

(2) Convert the second number string in args[2] to an integer b:

public class Calculator
{
    public static void main( String[] args )
    {
        int a = Integer.parseInt( args[0] ); // First number
        int b = Integer.parseInt( args[2] ); // Second number
	int result = 0;

	switch ( args[1].charAt(0) )
	{
	   case '+' -> result = a + b;
	   case '-' -> result = a - b;
	   case '*' -> result = a * b;
	   case '/' -> result = a / b;
        }

	System.out.println(a + " " + args[1] + " " + b + " = " + result);
    }
} 

Using numbers in as command line arguments   the Calculator program

(3) Perform the operation indicated by the string in args[1]:

public class Calculator
{
    public static void main( String[] args )
    {
        int a = Integer.parseInt( args[0] ); // First number
        int b = Integer.parseInt( args[2] ); // Second number
	int result = 0;

	switch ( args[1].charAt(0) )
	{
	   case '+': result = a + b; break;
	   case '-': result = a - b; break;
	   case '.': result = a * b; break;
	   case '/': result = a / b; break;
        }

	System.out.println(a + " " + args[1] + " " + b + " = " + result);
    }
} 

Note:   we have to use  .   to indicate multiplication because * has a special meaning

Using numbers in as command line arguments   the Calculator program

(4) Finally, print the answer:

public class Calculator
{
    public static void main( String[] args )
    {
        int a = Integer.parseInt( args[0] ); // First number
        int b = Integer.parseInt( args[2] ); // Second number
	int result = 0;

	switch ( args[1].charAt(0) )
	{
	   case '+': result = a + b; break;
	   case '-': result = a - b; break;
	   case '.': result = a * b; break;
	   case '/': result = a / b; break;
        }

	System.out.println(a + " " + args[1] + " " + b + " = " + result);
    }
} 

DEMO: demo/08-array/15-cmd-line-args/Calculator.java