In: Computer Science
1. Implement a public method named initialize. It takes a two dimensional square array of integers named
array as a parameter. It initializes all of the elements of the
array to the sum of their indices except for the
major diagonal (upper left to lower right) where each element is
initialized to -1. (For testing use a 4X4 or
5X5 array and have the application print out the array in 2
dimension format.
2. Implement a method named totals that takes a two dimensional
integer array named array as a parameter
and returns a one dimensional array where each element is the sum
of the columns of the input array.
3. A method named vowels that accepts a String array named alpha. The method will return the index of the
String that contains the most vowels. If there is a tie anyone will be good.
4. A method named count that accepts 2 parameters, an array of integers named array and an integer named min. The method will return the number of elements that are greater than ‘min’.
5. A method named order that accepts an integer array named digits. The method will print to the screen the both the maximum and minimum values of the array.
6. A method named summit that accepts 2 integer arrays named gamma and delta. If the arrays are the same size the method will return an array of the sum of the corresponding elements. If they are of different sizes the method will return an array of 10 elements where all of the elements are set to -1.
7. A method named border that accepts a two dimensional array of chars named array as a parameter. It initializes all of the elements of the array to ‘x’ except for the perimeter (first and last column, and first and last row) that is initialized to ‘o’.
Answer 1 :-
public void initialize(int[][] array)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(i==j)
{
array[i][j]=-1;
}
else
{
array[i][j]=i+j;
}
}
}
for(i=0;i<5;i++)
{
System.out.println();
for(j=0;j<5;j++)
{
System.out.print(" " +array[i][j]);
}
}
}
---------------------------------------------------------------------------
Answer 2:-
int[] totals(int[][] array)
{
int i,j;
int size=array.length;
int one_d_array[]=new
int[size];
for(i=0;i
for(j=0;j
one_d_array[i]=array[j][i];
}
}
return one_d_array;
}
---------------------------------------------------------------
Answer 3:-
int vowels(String[] alpha)
{
int i;
int max_count = 0;
int count[]=new
int[alpha.length];
for(i=0;i
for (i=0; i < alpha[i].length();
i++) {
char ch =
alpha[i].charAt(i);
if (ch == 'a' ||
ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch
== 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
count[i]=count[i]+1;
}
}
}
for(i=0;i
max_count=count[0];
if(count[i]>max_count)
{
max_count=count[i];
}
}
return max_count;
}
----------------------------------------------------------------
Answer 4:-
int count(int[] array, int min)
{
int tocount=0;
int i;
for(i=0;i
if(array[i]>min)
{
tocount=tocount+1;
}
}
return tocount;
}
-------------------------------------------------------------------------------
Answer 5:-
void order(int[] digit)
{
int max=digit[0];
int min=digit[0];
int i;
for(i=0;i
if(digit[i]>max)
{
max=digit[i];
}
if(digit[i]<
min)
{
digit[i]=min;
}
}
System.out.println("Maximum Value
is :"+max);
System.out.println("Minimum Value
is :"+min);
}
---------------------------------------------------------------------------------------------------
Answer 6:-
int[] summit(int gamma[], int delta[])
{
int i,j;
int[] array=new int[10];
int size1=gamma.length;
int size2=delta.length;
if(size1==size2)
{
for(i=0;i
array[i]=gamma[i]+delta[i];
}
}
else
{
for (i = 0; i
< size1; i++) {
array[i] = -1;
}
}
return array;
}
------------------------------------------------------------------------
Answer 7:-
void border(char[][] array)
{
int i,j;
int
size=array.length;
for(i=0;i
for(j=0;j
if(i==0 ||i==size-1 || j==0
||j==size-1)
{
array[i][j]='o';
}
else
{
array[i][j]='x';
}
}
}
for(i=0;i
System.out.println();
for(j=0;j
System.out.print(" "
+array[i][j]);
}
}
}