Skip to content

Commit 35bfee9

Browse files
Naveen KhuntetaNaveen Khunteta
authored andcommitted
added code for functions in java
1 parent c44eb2a commit 35bfee9

6 files changed

Lines changed: 158 additions & 0 deletions

File tree

1.5 KB
Binary file not shown.
1.98 KB
Binary file not shown.

bin/JavaSessions/Test.class

481 Bytes
Binary file not shown.

src/JavaSessions/ArrayConcept.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package JavaSessions;
2+
3+
public class ArrayConcept {
4+
5+
public static void main(String[] args) {
6+
7+
//limitations of array:
8+
//1. size is fixed: static array -- to overcome this issue: we use dynamic array -- ArrayList
9+
//2. stores only similar data types values -- to overcome this issue: we use object array
10+
11+
//1. int:
12+
int d1[] = {10,20,30,40,50}; //sorted
13+
String s1[] = {"test", "selenium", "automation"};
14+
char c1[] = {'a', 'b', 'c'};
15+
16+
int i[] = new int[3];
17+
i[0]=10;
18+
i[1]=20;
19+
i[2]=30;
20+
21+
System.out.println(i[1]);
22+
System.out.println(i[0]+i[1]);
23+
//System.out.println(i[3]);//ArrayIndexOutOfBoundsException
24+
25+
System.out.println(i.length); //size of array
26+
//to print/traverse/iterate all the values of array: use for loop:
27+
for(int k=0; k<i.length; k++){
28+
System.out.println(i[k]);
29+
}
30+
31+
32+
//2. double:
33+
double d[] = new double[2];
34+
d[0]=12.33;
35+
d[1]=23.33;
36+
37+
//3. char:
38+
char c[] = new char[3];
39+
40+
//4. boolean:
41+
boolean b[] = new boolean[2];
42+
43+
//5. String:
44+
String s[] = new String[3];
45+
s[0]="Tom";
46+
s[1]="test";
47+
s[2]="selenium";
48+
49+
//6. Object array:
50+
Object ob[] = new Object[4];
51+
ob[0]="Tom";
52+
ob[1]=25;
53+
ob[2]='M';
54+
ob[3]=12.33;
55+
56+
for(int p=0; p<ob.length; p++){
57+
System.out.println(ob[p]);
58+
}
59+
60+
61+
}
62+
63+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package JavaSessions;
2+
3+
public class FunctionsInJava {
4+
5+
public static void main(String[] args) {
6+
7+
//object of the class: new keyword
8+
FunctionsInJava obj = new FunctionsInJava();
9+
10+
obj.test();
11+
12+
int k = obj.div();
13+
System.out.println("div is:"+ k);
14+
15+
String s1 = obj.PQR();
16+
System.out.println(s1);
17+
18+
int mul = obj.multiply(10, 20);
19+
System.out.println("multiply is:"+ mul);
20+
21+
int mul1 = obj.multiply(20, 30);
22+
System.out.println("multiply is:"+ mul1);
23+
24+
}
25+
//function can not be created inside a function
26+
//functions are independent to each other or parallel to each other
27+
28+
//method overloading: within the same class, if there are number of methods having the same method name but different parameters.
29+
public void sum(){ //0 param
30+
System.out.println("sum with zero param");
31+
}
32+
33+
public void sum(int i){ //1 param
34+
35+
}
36+
37+
public void sum(int i, int j){ //2 params
38+
39+
}
40+
41+
public void sum(String p, String q){ //2 params
42+
43+
}
44+
45+
46+
//1. no ip and no op
47+
//void --> does not return any value
48+
public void test(){
49+
System.out.println("test method");
50+
int a = 10;
51+
int b = 20;
52+
int c = a+b;
53+
System.out.println("sum is:"+c);
54+
}
55+
56+
//2. no ip but some op
57+
public int div(){
58+
System.out.println("div method");
59+
int p = 30;
60+
int q = 10;
61+
int r = p/q;
62+
63+
return r;
64+
}
65+
66+
public String PQR(){
67+
System.out.println("multiply method");
68+
String s = "Selenium";
69+
return s;
70+
}
71+
72+
//3. some ip and some op
73+
public int multiply(int m, int n){
74+
System.out.println("multiply method");
75+
int l = m * n;
76+
return l;
77+
}
78+
79+
80+
81+
}

src/JavaSessions/Test.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package JavaSessions;
2+
3+
public class Test {
4+
5+
public static void main(String[] args) {
6+
7+
FunctionsInJava obj = new FunctionsInJava();
8+
9+
}
10+
11+
12+
13+
14+
}

0 commit comments

Comments
 (0)