2. Command Line Argument
Animal
Human
public class CommandLineArgument
{
public static void main(String args[])
{
System.out.println(“Hello ” +args[0]);
}
}
Compile : - javac CommandLineArgument.java
Run:- java CommandLineArgument info viaan Lecture
class args[0] args[1] args[2]
Output:- Hello info
Run:- java CommandLineArgument “info viaan” Lecture
class args[0] args[1]
2
www.infoviaan.com
3. Command Line Argument
Animal
Human
public class NoOfArg
{
public static void main(String ar[])
{
System.out.println(“You passed total no. of
arguments: ”+ar.length);
}
}
Compile : - javac NoOfArg.java
Run:- java NoOfArg Hello I am good
1 2 3 4
Output:- You passed total no. of arguments: 4
3
www.infoviaan.com
4. Control Statements
Animal
• Control the flow of program
Control
Statements
Decision Making
Statements
Looping Statements
if else
Ladder if else
Nested if else
Switch case
do...while
for
while
for each
4
www.infoviaan.com
5. Program - if else
• An if statement can be followed by an optional else
statement, which executes when the Boolean expression is
false.
public class TestIfElse {
public static void main(String[] args) {
int age = Integer.parseInt(args[0]);
if (age > 17)
System.out.println("You are Eligibe
for Vote :)");
else
System.out.println("Sorry! You Are
not eligible :(");
}
}
5
www.infoviaan.com
6. Program - Nested if..else
public class Nested {
public static void main(String[] args) {
int a, b, c;
a = 113; b = 88; c = 95;
if (a > b) {
if (a > c) {
System.out.println("A is biggest " + a);
} else {
System.out.println("C is boggest " + c);
}
} else {
if (b > c)
System.out.println("B is biggest " + b);
else
System.out.println("C is Biggest " + c);
}
}
} 6
www.infoviaan.com
7. Switch statement
• A switch statement allows a variable to be tested for
equality against a list of values.
• Each value is called a case, and the variable being
switched on is checked for each case.
7
www.infoviaan.com
8. Program - Switch
public class Switch {
public static void main(String[] args) {
int i = 2;
switch (i) {
case 1: System.out.println(“Addition");
break;
case 2:System.out.println(“Subtraction");
break;
case 3:System.out.println(“Mulplication");
break;
default: System.out.println(“Not
Available");
}
}
}
8
www.infoviaan.com
9. Program - Switch with
String (Java 5)public class Switch {
public static void main(String[] args) {
String c = “Ram";
switch (c) {
case “Ram":System.out.println(“Lord");
break;
case "Shani": System.out.println(“Dev");
break;
case “RamDev“: System.out.println(“BaBa");
break;
default: System.out.println(“People");
}
}
}
9
www.infoviaan.com
10. Loop
• while - allows code to be executed repeatedly based on
a given Boolean condition
• do...while - executes a block of code at least once, and
then repeatedly executes the block, or not, depending
on a given boolean condition at the end of the block
• for – it specifying iteration, which allows code to be
executed repeatedly
• for each (java 5) - used to access each successive value
in a collection of values. Arrays and Collections
10
www.infoviaan.com
12. Program - While Loop
public class TestWhile {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Hello "+i);
i++;
}
}
}
12
www.infoviaan.com
14. Program – do While Loop
public class TestDo {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Hello " +
i);
i++;
} while (i <= 5);
}
}
14
www.infoviaan.com
16. Program - for Loop
public class TestForLoop {
public static void main(String[] args) {
int n = 4;
int fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
System.out.println("Factorial Value is = "
+ fact);
}
}
16
www.infoviaan.com
19. Nested Loop
• A nested loop is a (inner) loop that appears in the
loop body of another (outer) loop.
• The inner or outerloop can be any type: while, do
while, or for.
• For example, the inner loop can be a while loop while
an outer loop can be a for loop. Of course, they can
be the same kind of loops too.
19
www.infoviaan.com
20. Programs - Nested Loop
public class TestNestedLoop {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
20
www.infoviaan.com
21. For each Loop(java 5)
• Access value directly from array or collection.
for(type variable : array){
//statement
}
Program : -
public class TestForEach{
public static void main(String[] args) {
int ar[]= {10,20,30,40,50};
for (int i : ar) {
System.out.println(i);
}
}
} 21
www.infoviaan.com
22. Array
Animal
Human
• An array is a container object that holds a fixed
number of values of a single type.
• It’s reference data type, which contain 2 byte
memory for holding address
• Advantages:
• Contiguous memory allocation
• Collection of similar data types
• Limitations:
• Can’t store dissimilar data types
• Not resizable
22
www.infoviaan.com
23. Declare an Array
Animal
Human
1. int x[] = {10,20,30,40,50}; //memory allocate at
compile time
2. int x[] = new int[5];
3. int x[] = new int[]{10,20,30,40,50}; //memory
allocate at runtime
• Which one is better to use?
Knows the elements - 3
Don't know the elements -2
23
www.infoviaan.com
24. One-D Array
Animal
Human
int a[] = new int[9];
a[0] = 10;
a[1] = 20;
…..
a[8] = 90;
or
int a[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
int size = a.length;
a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]
a[8]
1000
1000
10
20
30
40
50
60
70
80
90
4 B
2 B
24
www.infoviaan.com
26. Programs – One_D_Array
public class TestOneDArray {
public static void main(String[] args) {
char ar[]= {'X','Y','Z','A','@'};
for (char c : ar) {
System.out.println(c);
}
}
}
26
www.infoviaan.com
27. Copy an Array
Animal
• To copy an arrays value to another use System.ArrayCopy()
method, which take 5 arguments.
System.ArrayCopy(copyFrom, 2, copyTo, 0, 5);
Start index
Start index
No. of elements
27
www.infoviaan.com
28. Programs – Copy Array
public class TestArrayCopy {
public static void main(String[] args) {
char cf[] = { 'X', 'Y', 'Z', 'A', 'W' };
char ct[] = new char[4];
System.arraycopy(cf, 1, ct, 0, 4);
System.out.println(ct);
}
}
28
www.infoviaan.com
35. QA
1. What is difference between while and do..while loop?
2. Switch case work with which of the followings.
Byte,short,int,long,float,double,String
3. Difference between for in and for loop.
4. What happens when we forget to put a break statement in a
case clause of a switch?
5. Which method is given parameter via command line
arguments?
6. How many arguments can be passed to main()?
7. Which loop will execute the body of loop even when condition
controlling the loop is initially false?
8. Which of the jump statements can skip processing remainder
of code in its body for a particular iteration?
9. Why java uses String array as command Line Argument?
35
www.infoviaan.com
36. QA
1. What is the index of Brighton in the following array?
String[] skiResorts = { "Whistler Blackcomb", "Squaw Valley",
"Brighton", "Snowmass", "Sun Valley", "Taos" };
2. Write an expression that refers to the string Brighton within
the array.
3. What is the value of the expression skiResorts.length?
4. What is the index of the last item in the array?
5. What is the value of the expression skiResorts[4]?
36
www.infoviaan.com
37. Module Project 1
Ross is an event organizer. He has received data regarding the
participation of employees in two different events. Some employees
have participated in only one event and others have participated in
both events. Ross now needs to count the number of employees who
have taken part in both events. The records received by Ross consist
of employee ids, which are unique. Write a program that accepts the
employee ids participating in each event (the first line relates to the
first event and the second line relates to the second event). The
program should print the number of common employee ids in both
the events.
Suppose the following input is given to the program, where each line
represents a different event:
1001,1002,1003,1004,1005
1106,1008,1005,1003,1016,1017,1112
Now the common employee ids are 1003 and 1005, so the program
should give the output as:
2
www.infoviaan.com
38. Module Project 2
Write a java code to find the distance from Mumbai to major
cities of India.
Hint: Create an String array of major cities and integer array of
distances. User gives the city name and the same is searched in
the respective array and displays result.
www.infoviaan.com