Java Fundamentals language basics



1. Write a Java program that accepts two Strings as command-line arguments and generates the output in the required format. 

Example1:

If the two command-line arguments are Wipro and Bangalore then the output generated should be Wipro Technologies Bangalore.

Example2:

If the command line arguments are ABC and Mumbai then the output generated should be ABC Technologies Mumbai 

[Note: It is mandatory to pass two arguments in the command line]

1:  public class Assignment01 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            System.out.println(args[0]+" "+"Technolgies"+" "+args[1]);  
5:       }  
6:  }  

compilation:  javac Assignment01.java

execution: java Wipro Bangalore

Output 1:

Wipro Technologies Bangalore

compilation:  javac Assignment01.java

execution: java ABC Mumbai

Output 2:

ABC Technologies Mumbai

Note: Two Command-line Arguments are ABC Mumbai and these values are stored in an array args[] of type string and these arguments are passed to the main function. To print that values, args[0] and args[1] are used by concatenating the string "Technologies" in between them.


2. Write a Program to accept a String as a command-line argument and print a Welcome message as given below.

Example1:

C:\> java Sample John

O/P Expected: Welcome John

1:  public class Assignment02 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            System.out.println("Welcome"+" "+args[0]);  
5:       }  
6:  }  

To run this program following steps are used

compile>javac  Assignment02.java  

run: > java  Sample John

Output:

Welcome John

Following are the description of the program

Create a class "Assignment02 ".

Used the args[0] it will take the user input as string type.

The system.out.println  method print the string 

3. Write a Program to accept two integers as command-line arguments and print the sum of the two numbers

Example1:

C:\>java Sample 10 20

O/P Expected: The sum of 10 and 20 is 30

1:  public class Assignment03 {  
2:       public static void main(String[] args) {  
3:            // TODO Auto-generated method stub  
4:            int num1 = Integer.parseInt(args[0]);  
5:            int num2 = Integer.parseInt(args[1]);       
6:            int sum = num1 + num2;       
7:            System.out.println("The sum of "+num1+" and "+num2+" is "+sum);  
8:       }  
9:  }  

Post a Comment

0 Comments