Chapter 9 - Short Programs Flashcards
- Write a value-returning method that returns the number of rows in a two-dimensional array of doubles. Include code to test your method.
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);
}
- 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.
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);
}
- 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.
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);
}
- 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.
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; }
- 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.
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; } } }
- 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.
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; }
- 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.
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; } } }
- 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.
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; }
- 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.
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; }
- Write a method that returns the average of all elements in a two-dimensional array of ints. Include code to test your method.
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); }
- 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.
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; }
- Write a method that returns the concatenation of all elements in a two-dimensional array of Strings. Include code to test your method.
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; }
- 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.
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; }