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

Java Pracicals

The document provides a comprehensive overview of Java programming concepts, including class declarations, import statements, command line arguments, variable types, operators, control statements, and loops. It includes multiple examples and quizzes to reinforce the learning of these concepts. The training is aimed at WIPRO TalentNext participants and is presented by Vaibhav Diwan.

Uploaded by

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

Java Pracicals

The document provides a comprehensive overview of Java programming concepts, including class declarations, import statements, command line arguments, variable types, operators, control statements, and loops. It includes multiple examples and quizzes to reinforce the learning of these concepts. The training is aimed at WIPRO TalentNext participants and is presented by Vaibhav Diwan.

Uploaded by

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

Java Practical's

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 1
Vaibhav Diwan
Examples
example 1:- invalid example 2:- valid

//Student.java //Test.java
public class Test public class Test
{ {

} }
class A class A
{ {

} }.

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 2
Vaibhav Diwan
Examples….
example 3:- invalid

//Test.java
public class Test
{
Application location:-
}
D: (any disk)
public class A |-->student (any folder)
{ |-->Demo.java (your file name).

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 3
Vaibhav Diwan
Simple Example :-
import java.lang.System;
import java.lang.String;

class Test //class declaration


{ //class starts
public static void main(String[] args) //program execution starting point
{ //main starts
System.out.println("hi Students"); //printing statement
} //main ends
} //class ends
class A
{
}
class B
{
}

In above example String & System classes are present predefined java.lang package
hence must import that package by using import statement.

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 4
Vaibhav Diwan
To import the classes into our application
we are having two approaches,

1) Import all class of particular package.


a. Import java.lang.*; //it is importing all classes of
java.lang package.

2) Import required classes


a. Import java.lang.System;
b. Import java.lang.String;

In above two approaches second approach is best


approach because we are importing application required
classes.
Note: The source file is allows declaring multiple java
classes. Java Training for WIPRO TalentNext 2017 -
13-Mar-18 5
Vaibhav Diwan
Example-1:-
 Java contains 14 predefined packages but the default
package in java is java.lang package it means if we are
importing or not by default this package is imported.

 In below example importing classes are optional.

class Test
{
public static void main(String[] args)
{
System.out.println("hi students");
}
}

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 6
Vaibhav Diwan
Example-2:-
The class contains main method is called Mainclass and java
allows to declare multiple main class in a single source file.

class Test1
{ public static void main(String[] args)
{ System.out.println("Test1 World!");
}
}
class Test2
{ public static void main(String[] args)
{ System.out.println("Test2 World!");
}
}

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 7
Vaibhav Diwan
Example 2 .cont……
class Test3
{ public static void main(String[] args)
{ System.out.println("Test3 World!");
}
}
D:\morn11>java Test1
Test1 World!
D:\morn11>java Test2
Test2 World!
D:\morn11>java Test3
Test3 World!

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 8
Vaibhav Diwan
A Simple Java Program
public class Welcome {
public static void main(String args[]) {
System.out.println(“Welcome..!”);
}
}
Create source file : Welcome.java
Compile : javac Welcome.java
Execute : java Welcome

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 9
Vaibhav Diwan
Quiz
Sample.java file contains class A, B and C. How many .class files will be created after
compiling Sample.java?

What is your observation?

Sample.java

class A {
void m1() { }
}
class B {
void m2() { }
}
class C {
void m3() { }
}

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 10
Vaibhav Diwan
Quiz
.

What will be the result if you try to compile and execute the
following program ?
Reason out :

Sample.java

class Sample {
public static void main() {
System.out.println(“Welcome”);
}
}

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 11
Vaibhav Diwan
User provided values are printed Default values(JVM assigned
values)
int a = 10;
System.out.println(a);//10 int a;
System.out.println(a);//0
boolean b=true;
System.out.println(b);//true boolean b;
System.out.println(b);//false
char ch='a';
System.out.println(ch);//a char ch;
System.out.println(ch);
double d=10.5; //single space
System.out.println(d);//10.5
double d;
System.out.println(d);//0.0

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 12
Vaibhav Diwan
Example :-//Test.java
class Test
{ public static void main(String[] args)
{ float f=10.5;
System.out.println(f);
double d=20.5;
System.out.println(d);
}
}
D:\ratan>javac Test.java
Test.java:3: error: possible loss of precision
float f=10.5;
required: float found: double
in above example decimal value(10.5) by default double value hence compiler
generating error message so to represent float value use f constant or perform type
casting.
float f =10.5f; //using f constant (valid)
float f =(float)10.5; //using type casting (valid)

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 13
Vaibhav Diwan
Command Line Argument
class Simple {
static public void main(String[] args) {
System.out.println(args[0]);
}
}
When we compile the above code successfully and execute it as
Java Simple Wipro, the output will be
Wipro

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 14
Vaibhav Diwan
Command Line Argument
//Scanner

class Command
{
public static void main(String ar[])
{
int a=Integer.parseInt(ar[0]);
int b=Integer.parseInt(ar[1]);
int c=Integer.parseInt(ar[2]);
int d=a*b*c;
System.out.println("d="+d);
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 15
Vaibhav Diwan
Scanner
import java.util.Scanner;

class Student
{
public static void main(String ar[])
{
//java.util.Scanner sc=new java.util.Scanner(System.in);

Scanner sc=new Scanner(System.in);


System.out.println("Enter roll no");
nt stuRno=sc.nextInt(); //123

System.out.println("Enter student name");


String stuNm=sc.next(); //amit

System.out.println("Enter percentage");
float stuPer=sc.nextFloat(); //89.89

System.out.println("Enter gender type");


char stuGen=sc.next().charAt(0);//female

System.out.println("Name="+stuNm);
System.out.println("Roll no="+stuRno);
System.out.println("Percentage="+stuPer);
System.out.println("Gender="+stuGen);
} Java Training for WIPRO TalentNext 2017 -
13-Mar-18 16
• } Vaibhav Diwan
Example on Finding length of an Array

class FindNumberOfArguments {
public static void main(String[ ] args) {
int len = args.length;
System.out.println(len);
}
}

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 17
Vaibhav Diwan
Quiz
What will be the result, if we try to compile and
execute the following
code?
class Test {
public static void main(String [ ] ar) {
int for=2;
System.out.println(for);
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 18
Vaibhav Diwan
Local variable Example:-
class Test
{ public static void main(String[] args)
{ int a=10; //local variables
int b=20;
System.out.println(a);
System.out.println(b);
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 19
Vaibhav Diwan
Instance Variable Example
• class Test
• { //instance variables
• int a=10;
• int b=20;
• //static method
• public static void main(String[] args)
• { //Static Area
• Test t=new Test();
• System.out.println(t.a);
• System.out.println(t.b);
• t.m1(); //instance method calling
• }
• //instance method
• void m1()//user defined method must called by user inside main method
• { //instance area
• System.out.println(a);
• System.out.println(b);
• }//main ends
• }//class ends

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 20
Vaibhav Diwan
Static Variable Example
class Test
{ //static variables
static int a=1000;
static int b=2000;
public static void main(String[] args) //static method
{ System.out.println(Test.a);
System.out.println(Test.b);
Test t = new Test();
t.m1(); //instance method calling
}
//instance method
void m1() //user defined method called by user inside main method
{ System.out.println(Test.a);
System.out.println(Test.b);
}
}

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 21
Vaibhav Diwan
Examples
class Test
{ int a=10;
int b=20; // instance variables
static int c=30; static int d=40; //static variables
void m1() //instance method
{ System.out.println(a);
System.out.println(b);
System.out.println(Test.c);
System.out.println(Test.d);
}
static void m2() //static method
{ Test t = new Test();
System.out.println(t.a);
System.out.println(t.b);
System.out.println(Test.c);
System.out.println(Test.d);
}
public static void main(String[] args)
{ Test t = new Test();
t.m1(); //instance method calling
Test.m2(); //static method calling
}
}

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 22
Vaibhav Diwan
Arithmetic Operators - Example
class Sample{
public static void main(String[ ] args){
int a = 10;
int b = 3;
System.out.println("a + b = " + (a + b) );
System.out.println("a - b = " + (a - b) );
System.out.println("a * b = " + (a * b) );
System.out.println("a / b = " + (a / b) );
System.out.println("a % b = " + (a % b) );
}
}
13-Mar-18
Java Training for WIPRO TalentNext 2017 -
Vaibhav Diwan
23
Unary Operator - Example
class Sample{
public static void main(String args[]) {
int a = 10;
int b = 20;
System.out.println("++a = " + (++a) );
System.out.println("--b = " + (--b) );
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 24
Vaibhav Diwan
Relational Operators - Example
class Sample{
public static void main(String[] args){
int a = 10;
int b = 20;
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
}13-Mar-18 Java Training for WIPRO TalentNext 2017 -
25
Vaibhav Diwan
Logical Operators - Example
class Sample{
public static void main(String[] args){
boolean a = true;
boolean b = false;
System.out.println("a && b = " + (a&&b) );
System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b) );
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 26
Vaibhav Diwan
Conditional Statements

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 27
Vaibhav Diwan
If - Example
public class Test {
public static void main(String args[]) {
int x = 5;
if( x < 20 ) {
System.out.print("This is if statement");
}
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 28
Vaibhav Diwan
If – else Example
public class Check {
public static void main(String[ ] args) {
int age;
age = Integer.parseInt(args[0]);
if(age>18) {
System.out.println("Eligible to vote");
}
else {
System.out.println("Not eligible to vote");
}
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 29
Vaibhav Diwan
else - if Example
/* program to print seasons for a month input using if & else if */
public class ElseIfDemo {
public static void main(String[] args) {
int month = Integer.parseInt(args[0]);
if(month == 12 || month == 1 || month == 2)
System.out.println("Winter");
else if(month == 3 || month == 4 || month == 5)
System.out.println("Spring");
else if(month == 6 || month == 7 || month == 8)
System.out.println("Summer");
else if(month == 9 || month == 10 || month == 11)
System.out.println("Autumn");
else
System.out.println("invalid month");
} Java Training for WIPRO TalentNext 2017 -
13-Mar-18 30
Vaibhav Diwan
}
Switch Case - Example
public class SwitchDemo {
public static void main(String[] args) {
int weekday = Integer.parseInt(args[0]);
switch(weekday) {
case 1: System.out.println(“Sunday"); break;
case 2: System.out.println(“Monday"); break;
case 3: System.out.println(“Tuesday"); break;
case 4: System.out.println(“Wednesday"); break;
case 5: System.out.println(“Thursday"); break;
case 6: System.out.println(“Friday"); break;
case 7: System.out.println(“Saturday"); break;
default: System.out.println(“Invalid day");
}
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 31
Vaibhav Diwan
while loop – Example
public class Sample{
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println("i: "+i);
i = i + 1;
}
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 32
Vaibhav Diwan
• Output:
• i: 0
• i: 1
• i: 2
• i: 3
• i: 4

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 33
Vaibhav Diwan
do…while loop – Example
public class Sample {
public static void main(String[] args) {
int i =5;
do {
System.out.println("i: "+i);
i = i + 1;
} while (i < 5);
}
Java Training for WIPRO TalentNext 2017 -
}
13-Mar-18
Vaibhav Diwan
34
For loop - Example
public class Sample {
public static void main(String[] args) {
for (int i=1; i<=5; i++ ) {
System.out.println("i: "+i);
}
}
}

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 35
Vaibhav Diwan
Enhanced for loop
Syntax:
for(declaration : expression) {
Body of loop
}

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 36
Vaibhav Diwan
break - Example
public class Sample{
public static void main(String[] args) {
for (int i=1; i<=5; i++ ) {
if(i==2)
break;
System.out.println("i: "+i);
}
}
} Java Training for WIPRO TalentNext 2017 -
13-Mar-18 37
Vaibhav Diwan
Continue - Example
public class Sample {
public static void main(String[] args) {
int [] numbers = {1, 2, 3, 4, 5};
for(int i : numbers ) {
if( i == 3 ) {
continue;
}
System.out.println( "i: "+i );
}
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 38
Vaibhav Diwan
Arrays Example

Java Training for WIPRO TalentNext 2017 -


13-Mar-18 39
Vaibhav Diwan
Array - Example
public class ArrayDemo {
public static void main(String[] args) {
int[] x; // declares an array of integers
x = new int[5]; // allocates memory for 5integers
x[0] = 11;
X[4] = 22;
System.out.println("Element at index 0: " + x[0]);
System.out.println("Element at index 1: " + x[1]);
System.out.println("Element at index 4: " + x[4]);
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 40
Vaibhav Diwan
Array Copy - Example
public class ArrayLengthDemo {
public static void main(String[] args) {
// creates and initializes an array of integers
int[] source = {100, 200, 300};
// creates an integer array with 3 element
int[] dest = new int[3];
// copying an elements from source to dest array
System.arrayCopy(source, 0, dest, 0, source.length);
for (int i =0; i < dest.length; i++)
System.out.println("Element at index " + i + ": " +
dest[i]);
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 41
} Vaibhav Diwan
Two-Dimensional Array - Example
class TwoDimDemo {
public static void main(String[] args) {
int [][] x = new int[3][]; // initialize number of rows
x[0] = new int[3]; // define number of columns in each row
x[1] = new int[2];
x[2] = new int[5];
for(int i=0; i < x.length; i++) { // print array elements
for (int j=0; j < x[i].length; j++) {
x[i][j] = i;
System.out.print(x[i][j]);
}
System.out.println();
}
}
}
Java Training for WIPRO TalentNext 2017 -
13-Mar-18 42
Vaibhav Diwan

You might also like