Write a short java program : (FUll code plzz) ( JAVA ) 1) write an array-returni
ID: 3548919 • Letter: W
Question
Write a short java program : (FUll code plzz) ( JAVA )
1) write an array-returning method that takes a String as parameter and returns the corresponding array of chars.
2)Write a method that returns an array composed of all the elements in an array of chars in reverse order.
3) write a method that returns the percentage of the number of elements that have the value true in an array of booleans.
4) write a method that returns the difference between the largest and smallest elements in an array of double.
5) write a value-returning method that returns the product of all the elements in an integer array
.
6) Write a void method that multiplies by 2 all the elements of an array of float.
Explanation / Answer
import java.util.*;
public class Operations
{
// 1) write an array-returning method that takes a String as parameter and returns the corresponding array of chars.
public static char[] char_array(String input)
{
return input.toCharArray();
}
// 2)Write a method that returns an array composed of all the elements in an array of chars in reverse order.
public static char[] reverse_char_array(char[] input)
{
char[] new_array = new char[input.length];
for(int i=0; i<input.length; i++)
new_array[i] = input[input.length-i-1];
return new_array;
}
// 3) write a method that returns the percentage of the number of elements that have the value true in an array of booleans.
public static float percent(boolean[] array)
{
int count=0;
for(int i=0; i<array.length; i++)
if(array[i])
count++;
return (float)(count*100)/array.length;
}
// 4) write a method that returns the difference between the largest and smallest elements in an array of double.
public static double diff_max_min(double[] array)
{
double max = array[0];
double min = array[0];
for(int i=1; i<array.length; i++)
{
if(array[i] > max) max = array[i];
if(array[i] < min) min = array[i];
}
return max - min;
}
// 5) write a value-returning method that returns the product of all the elements in an integer array
public static int prod_array(int[] array)
{
int prod = 1;
for(int i=0; i<array.length; i++)
prod = prod*array[i];
return prod;
}
// 6) Write a void method that multiplies by 2 all the elements of an array of float.
public static void multiply_with_2(float[] array)
{
for(int i=0; i<array.length; i++)
array[i] = 2*array[i];
}
public static void main(String[] args)
{
char[] new_array = char_array("Ashok");
// Testing method 1;
System.out.println(new String(new_array));
// Testing method 2;
char[] char_array = {'K','U','M','A','R'};
char[] rev_array = reverse_char_array(char_array);
System.out.println(new String(rev_array));
// Testing method 3;
boolean[] bool_array = {true,false,true,true,false};
System.out.println("Percent of trues in boolean array is " + percent(bool_array));
// Testing method 4;
double[] doub_array = {34,56,78,12};
System.out.println("Difference between largest and smallest of double array is " + diff_max_min(doub_array));
// Testing method 5;
int[] int_array = {1,2,3,4,5,6};
System.out.println("Product of elements in integer array is " +prod_array(int_array));
// Testing method 6;
float[] float_array = {11,22,33};
multiply_with_2(float_array);
for(int i=0; i<float_array.length; i++)
System.out.print(float_array[i] + " ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.