Assignment: Module 02 - Part 01
![]() |
Assignment: Module 02 - Part 01 |
02. Write a class SumProgram to display the sum of any two variables.Compile and run the code from command line using javac and java command.
Output: 25
Here is an example of a Java program called "SumProgram" that takes two arguments from the command line and displays the sum of the two variables:
public class SumProgram {
public static void main(String[] args) {
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
int sum = num1 + num2;
System.out.println(sum);
}
}
To compile and run the code from the command line using the javac and java command, you would use the following steps:
- Open the command prompt and navigate to the directory where the "SumProgram.java" file is located.
- Use the command "javac SumProgram.java" to compile the code. This will create a new file called "SumProgram.class" in the same directory.
- Use the command "java SumProgram 10 15" to run the program. The arguments 10 and 15 will be passed to the program as the first and second argument respectively, and the program will output 25.
Note: Before you run the above commands, you need to make sure that the path of JDK is added to the system's environmental variables.