0% found this document useful (0 votes)
177 views

Java Programs (Practiced in Wipro Training Sessions)

The document summarizes several Java programs that were practiced in Wipro training sessions on dates from 3-7-21 to 5-7-21. It includes code examples and descriptions of programs to display a welcome message, add command line arguments, display command line arguments, read input using the Scanner class, and read input using the DataInputStream class.

Uploaded by

Unknown
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views

Java Programs (Practiced in Wipro Training Sessions)

The document summarizes several Java programs that were practiced in Wipro training sessions on dates from 3-7-21 to 5-7-21. It includes code examples and descriptions of programs to display a welcome message, add command line arguments, display command line arguments, read input using the Scanner class, and read input using the DataInputStream class.

Uploaded by

Unknown
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SRI SAIRAM

JAVA PROGRAMS (PRACTICED IN WIPRO TRAINING SESSIONS)

DATES: 3-7-21, 4-7-21 AND 5-7-21

//Displaying Welcome to Java World message


//HelloWorld.java
import java.lang.*;//By default this package imported
class HelloWorld{
public static void main(String[] args){
System.out.println("Welcome to Java World");
System.out.println("Java is Every Where");
}//close main
}//close main

/*
F:\BHAVAJAVA\bhavawipro>javac HelloWorld.java

F:\BHAVAJAVA\bhavawipro>dir
Volume in drive F has no label.
Volume Serial Number is 3CC2-730D

Directory of F:\BHAVAJAVA\bhavawipro

07/02/21 08:56 AM <DIR> .


07/02/21 08:56 AM <DIR> ..
07/02/21 08:56 AM 472 HelloWorld.class
07/02/21 08:56 AM 311 HelloWorld.java
2 File(s) 783 bytes
2 Dir(s) 147,334,508,544 bytes free

F:\BHAVAJAVA\bhavawipro>java HelloWorld
Welcome to Java World
Java is Every Where
*/
//Reading arguments(Input) from Command Line
//AdditionCommand.java
class AdditionCommand{
public static void main(String[] args){
String str1=args[0];
String str2=args[1];
int a=Integer.parseInt(str1);
int b=Integer.parseInt(str2);
int c;
c=a+b;
System.out.println("Addition of Two Numbers="+c);
}
}
/*
F:\BHAVAJAVA\bhavawipro>javac AdditionCommand.java

F:\BHAVAJAVA\bhavawipro>java AdditionCommand 2 5
Addition of Two Numbers=7

F:\BHAVAJAVA\bhavawipro>java AdditionCommand 1099 789


Addition of Two Numbers=1888
*/

/*
Description
In java.lang package in Integer class
static int parseInt(String str)//Returns int value for String
*/
//Print command Line Arguments and displaying no of arguments passed
//DisplayCommand.java
class DisplayCommand{
public static void main(String[] args){
int len=args.length;
//args.length returns no of arguments received into args[] arry
System.out.println("Command Line arguments are...");
System.out.println("No of arguments="+len);
for(int i=0;i<len;i++){
System.out.println(args[i]);
}
}
}
/*
F:\BHAVAJAVA\bhavawipro>javac DisplayCommand.java

F:\BHAVAJAVA\bhavawipro>java DisplayCommand Hello Java World


Command Line arguments are...
No of arguments=3
Hello
Java
World

F:\BHAVAJAVA\bhavawipro>java DisplayCommand Java is Island name in Indonesiya


Command Line arguments are...
No of arguments=6
Java
is
Island
name
in
Indonesiya

F:\BHAVAJAVA\bhavawipro>java DisplayCommand 1 2 3 4 5
Command Line arguments are...
No of arguments=5
1
2
3
4
5

F:\BHAVAJAVA\bhavawipro>java DisplayCommand 12.7 9 J 15.77 7 Java


Command Line arguments are...
No of arguments=6
12.7
9
J
15.77
7
Java
*/
//Addition.java
//Reading Input through Scanner class object
//Addition.java
import java.util.*;
class Addition{
public static void main(String[] args){
//Create object for Scanner class
Scanner sin=new Scanner(System.in);
System.out.println("Enter a,b,c");
int a=sin.nextInt();
int b=sin.nextInt();
int c=sin.nextInt();
int result=a+b+c;
System.out.println("Addition result="+result);
}
}

/*
F:\BHAVAJAVA\bhavawipro>javac Addition.java

F:\BHAVAJAVA\bhavawipro>java Addition
Enter a,b,c
27
77
99
Addition result=203

F:\BHAVAJAVA\bhavawipro>java Addition
Enter a,b,c
777
5757
9999
Addition result=16533
*/

/*
How to create object for the class
ClassName objname=new ClassName(arglist);

Syntax for nextInt() defined in Scanner class


int nextInt()//To read integer input
*/
//Multiplication.java-Reading Input through Scanner class object
//Multiplication.java
import java.util.*;
class Multiplication{
public static void main(){
//Create object for Scanner class
Scanner sin=new Scanner(System.in);
System.out.println("Enter a,b,c");
float a=sin.nextFloat();
float b=sin.nextFloat();
float c=sin.nextFloat();
float result=a*b*c;
System.out.println("Multiplication result="+result);
}
}

/*
F:\BHAVAJAVA\bhavawipro>javac Multiplication.java
F:\BHAVAJAVA\bhavawipro>java Multiplication
Enter a,b,c
7.9 5.7 9.9
Multiplication result=445.79697
*/

/*
Syntax for nextFloat() defined in Scanner class
int nextFloat()//To read floating point input
*/
//Reading Input through DataInputStream class object
//AddData.java
import java.io.*;
class AddData{
public static void main(String[] args)throws IOException{
DataInputStream din=new DataInputStream(System.in);
System.out.println("Enter a,b");
String str1=din.readLine();
String str2=din.readLine();
int a=Integer.parseInt(str1);
int b=Integer.parseInt(str2);
int c=a+b;
System.out.println("Addition of "+a+" and "+b+"="+c);
}
}
/*
F:\BHAVAJAVA\bhavawipro>javac AddData.java
Note: AddData.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

F:\BHAVAJAVA\bhavawipro>java AddData
Enter a,b
2
5
Addition of 2 and 5=7
*/

/*
In DataInputStream class for reading string data
public String readLine() throws IOException;
*/

You might also like