Command-line arguments in NetBeans Objectives

J.E.D.I

8.3 Command-line arguments in NetBeans

To illustrate on how to pass some arguments to your programs in NetBeans, let us create a Java program that will print the number of arguments and the first argument passed to it. public class CommandLineExample { public static void main String[] args { System.out.printlnNumber of arguments= + args.length; System.out.printlnFirst Argument=+ args[0]; } } Now, run netbeans and create a new project and name this CommandLineExample. Copy the code shown above and compile the code. Now, follow these steps to pass arguments to your program using NetBeans. Click on Projects encircled below. Introduction to Programming I 123 Figure 8.2: Opening Project File J.E.D.I Right-click on the CommandLineExample icon, and a popup menu will appear. Click on Properties. The Project Properties dialog will then appear. Introduction to Programming I 124 Figure 8.3: Opening Properties Figure 8.4: Properties Dialog J.E.D.I Now, click on Run On the Arguments textbox, type the arguments you want to pass to your program. In this case we typed in the arguments 5 4 3 2 1. Then, click on the OK button. Introduction to Programming I 125 Figure 8.5: Click On Running Project Figure 8.6: Set the Command-Line Arguments J.E.D.I Now try to RUN your program. As you can see here, the output to your program is the number of arguments which is 5, and the first argument which is 5. Introduction to Programming I 126 Figure 8.7: Running the Program in with the Shortcut Button Figure 8.8: Program Output J.E.D.I

8.4 Exercises

8.4.1 Print arguments

Get input from the user using command-line arguments and print all the arguments to the screen. For example, if the user entered, java Hello world that is all your program should print Hello world that is all

8.4.2 Arithmetic Operations

Get two numbers from the user using command-line arguments and print sum, difference, product and quotient of the two numbers. For example, if the user entered, java ArithmeticOperation 20 4 your program should print sum = 24 difference = 16 product = 80 quotient = 5 Introduction to Programming I 127 J.E.D.I 9 Working with the Java Class Library

9.1 Objectives

In this section, we will introduce some basic concepts of object-oriented programming. Later on, we will discuss the concept of classes and objects, and how to use these classes and their members. Comparison, conversion and casting of objects will also be covered. For now, we will focus on using classes that are already defined in the Java class library, we will discuss later on how to create your own classes. At the end of the lesson, the student should be able to: • Explain object-oriented programming and some of its concepts • Differentiate between classes and objects • Differentiate between instance variablesmethods and classstatic variablesmethods • Explain what methods are and how to call and pass parameters to methods • Identify the scope of a variable • Cast primitive data types and objects • Compare objects and determine the class of an objects

9.2 Introduction to Object-Oriented Programming