0% found this document useful (0 votes)
6 views3 pages

Solution Java Programming

Uploaded by

BHAVIK SOLANKI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Solution Java Programming

Uploaded by

BHAVIK SOLANKI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question 1 - Write a Java program to find the maximum and minimum value of an

array. ?
--------------------------------------------------------------------------
import java.util.Arrays;
public class p4n_example {

static int max;


static int min;

public static void max_min(int my_array[]) {


max = my_array[0];
min = my_array[0];
int len = my_array.length;
for (int i = 1; i < len - 1; i = i + 2) {
if (i + 1 > len) {
if (my_array[i] > max) max = my_array[i];
if (my_array[i] < min) min = my_array[i];
}
if (my_array[i] > my_array[i + 1]) {
if (my_array[i] > max) max = my_array[i];
if (my_array[i + 1] < min) min = my_array[i + 1];
}
if (my_array[i] < my_array[i + 1]) {
if (my_array[i] < min) min = my_array[i];
if (my_array[i + 1] > max) max = my_array[i + 1];
}
}
}

public static void main(String[] args) {


int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};
max_min(my_array);
System.out.println(" Original Array: "+Arrays.toString(my_array));
System.out.println(" Maximum value for the above array = " + max);
System.out.println(" Minimum value for the above array = " + min);
}
}

Output

Original Array: [25, 14, 56, 15, 36, 56, 77, 18, 29, 49]
Maximum value for the above array = 77
Minimum value for the above array = 14
___________________________________________
Question 2 : Write a Java program to find the duplicate values of an array of string
values ?

--------------------------------------------------------------------------

public class p4n_example{


public static void main(String[] args)
{
String[] my_array = {"bcd", "abd", "jude", "bcd", "oiu", "gzw", "oiu"};

for (int i = 0; i < my_array.length-1; i++)


{
for (int j = i+1; j < my_array.length; j++)
{
if( (my_array[i].equals(my_array[j])) && (i != j) )
{
System.out.println("Duplicate Element is : "+my_array[j]);
}
}
}
}
}

Output
Duplicate Element is : bcd
Duplicate Element is : oiu

____________________________________________
Question 3 : Write a Java program to find the second largest element in an array. ?

--------------------------------------------------------------------------
import java.util.Arrays;
public class p4n_Main {
public static void main(String[] args) {
int[] my_array = {
10789, 2035, 1899, 1456, 2013,
1458, 2458, 1254, 1472, 2365,
1456, 2165, 1457, 2456};
System.out.println("Original numeric array : "+Arrays.toString(my_array));
Arrays.sort(my_array);
int index = my_array.length-1;
while(my_array[index]==my_array[my_array.length-1]){
index--;
}
System.out.println("Second largest value: " + my_array[index]);
}
}

Output

Original numeric array : [10789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365,
1456, 2165, 1457, 2456]
Second largest value: 2458

You might also like