Chapter 9 - Short Programs Flashcards

1
Q
  1. Write a value-returning method that returns the number of rows in a two-dimensional array of doubles. Include code to test your method.
A

private static int getRows(double[][] a)
{
return a.length;
}

public static void main(String[] args)
{
double[][] a={{9,8,10,11},{12,13,14,15}};
int numOfRows=getRows(a);
System.out.println(“Number of rows in a : “ +numOfRows);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Write a value-returning method that returns the number of columns that have two elements in a two-dimensional array of booleans. Include code to test your method.
A
private static int getColumnsOfTwoElements(boolean[][] a)
{
for (int j = 0; j < a.length; j++)
{
if(a[j].length==2)
colCount++;
}
return colCount;
}

public static void main(String[] args)
{
boolean[][] a={{true,true,false},{true,true},{true,true},{true,true}};
int columnCount=getColumnsOfTwoElements(a);
System.out.println(“Number of columns of count 2 : “ +columnCount);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Write a value-returning method that returns the number of columns with n elements in a two-dimensional array of chars, where n is a parameter of the method. Include code to test your method.
A
private static int getNelementColumns(char[][] a,int n)
{
int colCount=0;
for (int j = 0; j < a.length; j++)
{
if(a[j].length==n)
colCount++;
}
return colCount;
}

public static void main(String[] args)
{
char[][] a={{‘a’,’b’,’c’},{‘d’,’e’},{‘f’,’g’},{‘i’,’j’}};
int numberColumns=3;
int columnCount =getNelementColumns(a,numberColumns);
System.out.println(“Number of columns of count “
+numberColumns +” is :”+columnCount);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Write a value-returning method that returns the sum of all the elements in a two-dimensional array of floats. Include code to test your method.
A
private static int getSum(float[][] a)
{
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
total+=a[i][j];
}
}
return total;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Write a method with a void return value that sets to 0 all the elements of the even-numbered rows and sets to 1 all the elements of odd-numbered rows of a two-dimensional array of ints. Include code to test your method.
A
private static void setValues(int[][] a)
{
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
if((i+1)%2==0)
   a[i][j]=0;
else
   a[i][j]=1;
}
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Write a value-returning method that returns the sum of the elements in the last column of each row in a two-dimensional array of ints. Include code to test your method.
A
private static int sumOfLastColumn(int[][] a)
{
int lastColSum=0;
for (int i = 0; i < a.length; i++)
{
int indexofLastColum=a[i].length-1;
lastColSum+=a[i][indexofLastColum];
}
return lastColSum;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Write a method with a void return value that inverts all the elements of a two-dimensional array of booleans (true becomes false and false becomes true). Include code to test your method.
A
private static void invertBoolean(boolean[][] a)
{
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
if(a[i][j]==true)
a[i][j]=false;
else if(a[i][j]==false)
a[i][j]=true;
}
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Write a method that returns the number of elements having the value true in a two-dimensional array of booleans. Include code to test your method.
A
private static int getTrueCount(boolean[][] a)
{
int trueCount=0;
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
if(a[i][j]==true)
trueCount++;
}
}
return trueCount;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Write a method that returns the percentage of elements having the value false in a two-dimensional array of booleans. Include code to test your method.
A
private static double getFalsePercentage(boolean[][] a)
{
int falseCount=0;
int totalCount=0;
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
if(a[i][j]==false)
falseCount++;
totalCount++;
}
}
return ((double)falseCount/totalCount)*100;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Write a method that returns the average of all elements in a two-dimensional array of ints. Include code to test your method.
A

private static double getaverage(int[][] a)

{
int total=0;
int numElements=0;
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
total+=a[i][j];
numElements++;
}
}
return ((double)total/numElements);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. Write a method that returns the String “regular” if all the rows of a two-dimensional array of floats have the same number of columns; otherwise, it returns “irregular.” Include code to test your method.
A
private static String regular(float[][] a)
{
String result="regular";
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length-1; j++)
{
if(a[j].length!=a[j+1].length)
result="irregular";
}
}
return result;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Write a method that returns the concatenation of all elements in a two-dimensional array of Strings. Include code to test your method.
A
private static String concatenate(String[][] a)
{
String result="";
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
result=result.concat(a[i][j]+" ");
}
}
return result;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. Write an array-returning method that takes a two-dimensional array of chars as a parameter and returns a single-dimensional array of Strings as follows: The array returned should have a number of elements equal to the number of rows in the parameter array; every element of the array returned should be the concatenation of all the column elements of the corresponding row in the parameter array. Include code to test your method.
A
private static String[] getSingleDimension (char[][] letters)
{
String[] temp=new String[letters.length];
for (int i = 0; i < letters.length; i++)
{
temp[i]=new String("");
for (int j = 0; j < letters[i].length; j++)
{
temp[i]+=letters[i][j];
}
}
return temp;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly