Lab Exercises
- Exercise 1: Read command-line arguments (20 minutes)
- Exercise 2: Read command-line arguments using NetBeans (10 minutes)
- Homework Exercise (for people who are taking Sang Shin’s “Java Programming online course”)
Exercise 1: Read command-line arguments
(1.1) Build and run Hello Java program that receives command-line arguments
1. Go to a directory where you want to create Java program
2. Write HelloCommandLineArguments.java using your editor of choice such as notepad on Windows platform or gedit on Solaris platform (in this example, I am using jedit) as shown in Code-1.10 below.
C:\myjavaprograms>jedit HelloCommandLineArguments.java
public class HelloCommandLineArguments {
public static void main( String[] args ){ // Print the string “Hello, ” on screen // Check if a command line argument exists // Display the arguments from the command line } |
Code-1.10: HelloCommandLineArguments.java
3. Compile HelloCommandLineArguments.java using javac compiler.
4. Run the HelloCommandLineArguments program using java command passing arguments.
I am saying Hello to the people below..
argument index 0: David
argument index 1: Charles
argument index 2: Young
(1.2) Read numbers as arguments
1. Go to a directory where you want to write Java source files.
2. Write MyCompute.java using your editor of choice such as notepad on Windows platform or gedit on Solaris platform (in this example, I am using jedit) as shown in Code-1.20 below.
public class MyCompute {
public static void main(String[] args) { // Check if a command line argument exists // Display the addition of the two numbers // Display the multiplication of the two numbers } |
Code-1.20: MyCompute.java
4. Run the MyCompute program using java command passing two number arguments.
C:\myjavaprograms>java MyCompute
I am reading numbers as command line arguments..
Please enter two numbers!
C:\myjavaprograms>java MyCompute 4
I am reading numbers as command line arguments..
Please enter two numbers!
C:\myjavaprograms>java MyCompute 4 6
I am reading numbers as command line arguments..
Result of addition = 10
Result of multiplication = 24
Summary
In this exercise, you learned how to read input arguments in your Java application.
Exercise 2: Pass command-line arguments using NetBeans
- Open, build and run HelloCommandLineArguments NetBeans project
- Open, build and run MyCompute NetBeans project
(2.1) Open, build and run HelloCommandLineArguments NetBeans project
1. Open HelloCommandLineArugments NetBeans project.
- Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/commandarguments/samples directory.
- Windows: If you unzipped the hands-on lab zip file, 1038_commandarguments.zip, under C:\ directory, the directory to which you want to browse down should be C:\commandarguments\samples.
- Solaris/Linux: If you unzipped the 1038_commandarguments.zip file under $HOME directory, the directory to which you want to browse down should be $HOME/commandarguments/samples.
- Select HelloCommandLineArugments.
- Click Open Project Folder. (Figure-2.10 below)
Figure-2.10: pen HelloCommandLineArguments project
- Observe that the HelloCommandLineArugments project node is displayed under Projects pane of the NetBeans IDE.
2. Pass command-line arguments
- Right click HelloCommandLineArugments project node and select Properties.
Figure-2.11: Open Properties of the project
- Observe that the Project Properties dialog box appears.
- Select Run under Categories section on the left.
- For the Arguments field on the right, enter command line arguments, for example, David Charles Monica, in this example. (Figure-2.12 below)
- Click OK.
Figure-2.12: Provide command line arguments
3. Build and run the project.
- Right click HelloCommandLinekArguments project node and select Run Project.
- Observe the result in the Output window of the IDE. (Figure-2.13 below)
Figure-2.13: Result
(2.2) Open, build and run MyCompute NetBeans project
1. Open MyCompute NetBeans project.
- Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/commandarguments/samples directory.
- Windows: If you unzipped the hands-on lab zip file, 1038_commandarguments.zip, under C:\ directory, the directory to which you want to browse down should be C:\commandarguments\samples.
- Solaris/Linux: If you unzipped the 1038_commandarguments.zip file under $HOME directory, the directory to which you want to browse down should be $HOME/commandarguments/samples.
- Select MyCompute.
- Click Open Project Folder.
- Right click MyCompute project node and select Properties.
- Observe that the Project Properties dialog box appears.
- Select Run under Categories section on the left.
- For the Arguments field on the right, enter command line arguments, for example, 10 20, in this example. (Figure-2.21 below)
- Click OK.
Figure-2.21: Pass command line arguments
3. Build and run the project.
- Right click MyCompute project node and select Run Project.
- Observe the result in the Output window of the IDE. (Figure-2.13 below)
Figure-2.13: Result
Summary
In this exercise, you learned how to pass command-line arguments when you are using NetBeans.
Homework exercise (for people who are taking Sang Shin’s “Java Programming online course”)
- Receive the ages of the all your family members (between 3 to 6 members) as command line arguments in the format as following (name then age)
- Monica 12 Daniel 34 Shelley 23
- Compute and display the average of the ages that were entered.
- Zip file of the the MyOwnCommandLineArguments NetBeans project. (Someone else should be able to open and run it as a NetBeans project.) You can use your favorite zip utility or you can use “jar” utility that comes with JDK as following.
- cd <parent directory that contains MyOwnCommandLineArguments directory> (assuming you named your project as MyOwnCommandLineArguments)
- jar cvf MyOwnCommandLineArguments.zip MyOwnCommandLineArguments (MyOwnCommandLineArguments should contain nbproject directory)
- Captured output screen – name it as JavaIntro-commandarguments.gif orJavaIntro-commandarguments.jpg (or JavaIntro-commandarguments.<whatver graphics format>)
- Any screen capture that shows that your program is working is good enough. No cosmetic polishment is required.
- If you decide to use different IDE other than NetBeans, the zip file should contain all the files that are needed for rebuilding the project – war file with necessary source files is OK.