TCP2201 Object Oriented Analysis and Design Lab 1 - Introductory Programs
TCP2201 Object Oriented Analysis and Design Lab 1 - Introductory Programs
TCP2201-T1/2014.15/WKS
If any errors occurred, the compiler or interpreter will exit with error code.
All Java applications require that a main function be defined with the attributes public and
static. In the example, main is declared as void since the method does not return a value.
The main function expects one (1) argument, a reference to an array of String objects
these are arguments passed during runtime. The array holds any command-line arguments
passed to the program when it is executed from the command line. To illustrate this feature,
modify the program as follows:
class argProg{
public static void main(String[] a){
if(a.length==0)
System.out.println("Welcome to Java");
else
System.out.println("This is the first Java program of "
+ a[0]);
}
}
1.
2.
3.
4.
5.
Compile and run the above application using the command: java argProg
Run the program again with a name as a command line argument.
Example: java argProg Adam
Compare the output from the programs above.
EXERCISE
Change the program code so that it displays the number of arguments passed in during
program execution.
Change the program to accept any number of arguments in the command line such as
java argProg Martin Kalin
and print out all the passed arguments
TCP2201-T1/2014.15/WKS
What is the name of the save file for the program shown above?
Which method does the applet above call/use when running in a browser?
How many libraries are imported in the code above?
What does the asterisk mean at the end of each library declaration?
What is the command to compile the code above?
TCP2201-T1/2014.15/WKS
TCP2201-T1/2014.15/WKS
TCP2201-T1/2014.15/WKS