In: Computer Science
public class Sum2 { // TODO - write your code below this comment. }
Download the Sum2.java file, and open it in jGrasp (or a text editor of your choice). This program takes two values as command-line arguments, converts them to ints via Integer.parseInt, and adds them together. Example output of this program is shown below, with the command-line arguments 3 4:
Sum: 7
I have uploaded the Images of the code, Typed code ,Steps to run the code and Output of the Code. I have provided explanation using comments(read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation
please refer code Images
Typed Code:
class Sum2
{
public static void main(String[] args)
{
//accessing args[0] and changing it into integer
//storing it into num1
int num1 = Integer.parseInt(args[0]);
//accessing args[1] and changing it into integer
//storing it into num2
int num2 = Integer.parseInt(args[1]);
//adding those two numbers
//storing it into sum
int sum = num1+num2;
//printing the sum
System.out.print("sum:"+sum);
}
}
//code ended here
Steps:
1.Take the code into an editor.
2.Save that java file in any location
3.Go to command prompt and move to that location in command prompt.
command to move to that location where the Sum2.java exists --> cd location.
Here location is where Sum2.java is placed in your computer.
4.compile the code using command --> javac Sum2.java
where Sum2.java is file name
5.Acessing the class and giving arguments using command --> java Sum2 3 4
Sum2 is a class name and 3,4 are args[0] and args[1]
6.then we will get the output
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!