Professional Documents
Culture Documents
INPUT
import java.util.*;
public class Activity21 {
public static int add(int a, int b){
int sum;
sum=a+b;
return sum;
}
public static double add(double a, double b){
double sum;
sum=a+b;
return sum;
}
public static void main(String[] args) {
System.out.println(add(5.5,6.6));
System.out.println(add(5,6));
}
OUTPUT
12.1
11
Try to understand the difference between public and private static method.
2. Write a Java program to sort a numeric array and a string array.
INPUT
import java.util.*;
public class Activity21 {
public static void main(String[] args) {
int[] mylist={12,43,1,23};
System.out.println(Arrays.toString(mylist));
Arrays.sort(mylist);
System.out.println(Arrays.toString(mylist));
String[] mylist2={"C","Q","A"};
System.out.println(Arrays.toString(mylist2));
Arrays.sort(mylist2);
System.out.println(Arrays.toString(mylist2));
}
}
OUTPUT
[12, 43, 1, 23]
[1, 12, 23, 43]
[C, Q, A]
[A, C, Q]
3. Write a Java program to find the duplicate values of an array of integer values.
Hint: if content of my_array = {1, 2, 3, 3, 5, 6, 7, 7}, then display output as
Duplicate Array Element : 3
Duplicate Array Element : 7
INPUT
import java.util.*;
public class Activity21 {
public static void main(String[] args) {
int [] arr = {1, 2, 3, 3, 5, 6, 7, 7} ;
for(int i = 0; i < arr.length; i++) {
for(int j = i + 1; j < arr.length; j++) {
if(arr[i] == arr[j])
System.out.println("Duplicate elements in given array: "+arr[j]);
}
}
}
}
OUTPUT
Duplicate elements in given array: 3
Duplicate elements in given array: 7
4. Write a Java program to find the common elements between two arrays (string values).
Hint: if content of arrays are
array1 = {"Python", "JAVA", "PHP", "C#", "C++", "SQL"};
array2 = {"Networking", "SQL", "Oracle", "DB2", "JAVA"};
Then print output as
JAVA
SQL
INPUT
import java.util.*;
if(array1[i].equals(array2[j]) )
OUTPUT