Directions : Complete the following questions, for most of the questions the ret
ID: 3712632 • Letter: D
Question
Directions: Complete the following questions, for most of the questions the return type and name of the method are left out. Give each method a meaningful name and appropriate return type.
Write a method that takes a 2D array of booleans as a parameter and then the method will return the number of elements in that array. You can assume that all of the rows have the same number of elements.
Make a method which returns true if a 2D array of Strings is square. (A square array has the same number of columns as it does rows.)
Create a method that takes a 2D array of doubles as a parameter and makes each element a random number containing the numbers 1 through 10, not including 10. The method should not return anything.
Create a method that takes a 2D array of Strings as a parameter and makes every element in the third column the word “frogurt”. The method should not return anything.
Write in java. These dont take too long so i hope its okay for you to do all 4 methods
Explanation / Answer
1.
class Test
{
public static void main (String[] args)
{
boolean[][] b = new boolean[4][5];
System.out.println("Total Number of elements in array : "+computeTotalElements(b));
}
public static int computeTotalElements(boolean[][] b)
{
int count = 0;
for(int i = 0;i<b.length;i++) // b.length = rows
{
for(int j=0;j<b[i].length;j++)// b[i].length = columns in a row
{
count++;
}
}
return count;
}
}
Output:
Total Number of elements in array : 20
2.
class Test
{
public static void main (String[] args)
{
String[][] str = new String[4][5];
System.out.println("Array is Square : "+ifSquare(str));
}
public static boolean ifSquare(String[][] str)
{
if(str.length == str[0].length)// compare length of array(rows) with length of one row
return true;
else
return false;
}
}
Output:
Array is Square : false
3.
class Test
{
public static void main (String[] args)
{
double[][] rand = new double[4][5];
System.out.println("Random numbers of array : ");
randomArray(rand);
}
public static void randomArray (double[][] random)
{
for(int i = 0;i<random.length;i++)
{
for(int j = 0;j<random[i].length;j++)
{
random[i][j] = 1 + (Math.random() * 9);
}
}
for(int i = 0;i<random.length;i++)
{
for(int j = 0;j<random[i].length;j++)
{
System.out.printf("%.2f ",random[i][j]);
}
System.out.println();
}
}
}
Output:
Random numbers of array :
3.74 6.83 1.39 1.50 5.52
8.87 3.57 7.41 4.70 2.85
2.60 7.49 1.08 1.98 4.54
8.82 7.93 8.17 9.90 9.00
4
class Test
{
public static void main (String[] args)
{
String[][] str = new String[][]{{"yogurt","yogurt","yogurt"},{"yogurt","yogurt","yogurt"}};
System.out.println("Changing the third column : ");
update(str);
}
public static void update(String[][] str)
{
for(int i = 0;i<str.length;i++)
{
for(int j = 0;j<str[i].length;j++)
{
if( j== 2)
str[i][j] = "frogurt";
}
System.out.println();
}
for(int i = 0;i<str.length;i++)
{
for(int j = 0;j<str[i].length;j++)
{
System.out.print(str[i][j]+" ");
}
System.out.println();
}
}
}
Output:
Changing the third column :
yogurt yogurt frogurt
yogurt yogurt frogurt
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.