Question

Please write this in JAVA ONLY. Write a method with a void return value that inverts...

Please write this in JAVA ONLY.

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. Your output should show all the values of the array prior to inverting them and afterwards.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

BooleanArrayTest.java

import java.util.Scanner;


public class BooleanArrayTest {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the number of rows: ");
       int rows = scan.nextInt();
       System.out.println("Enter the number of columns: ");
       int cols = scan.nextInt();
       boolean b[][] = new boolean[rows][cols];
       for(int i=0; i<rows; i++){
           for(int j=0; j<cols; j++){
               System.out.print("Enter value: ");
               b[i][j] = scan.nextBoolean();
           }
       }
       System.out.println("Before inverting, Array elements are: ");
       displayArray(b);
       inverts(b);
       System.out.println("After inverting, Array elements are: ");
       displayArray(b);
   }
   public static void inverts (boolean b[][]){
       for(int i=0; i<b.length; i++){
           for(int j=0; j<b[i].length; j++){
               b[i][j] = !b[i][j];
           }
       }
   }

   public static void displayArray(boolean b[][]){
       for(int i=0; i<b.length; i++){
           for(int j=0; j<b[i].length; j++){
               System.out.print(b[i][j]+" ");
           }
           System.out.println();
       }
      
   }

}

Output:

Enter the number of rows: 3 Enter the number of columns: 3 Enter value: true Enter value: true Enter value: false Enter value

Add a comment
Know the answer?
Add Answer to:
Please write this in JAVA ONLY. Write a method with a void return value that inverts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Note: According to the question, please write source code in java only using the class method....

    Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors. Exercise #2: Design and implement a program (name it compareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array...

  • Need Help Please Using Java Language Create a Code Write a class (and a client class...

    Need Help Please Using Java Language Create a Code Write a class (and a client class to test it) that encapsulates the evolution of the passwords of three students over four months. Your only instance variable should be a two-dimensional array of values representing the passwords. Dimension 1 represents the student and dimension 2 represents the month. (Since we are concerned about security, we are assuming that people change their password once a month; we only care about the value...

  • JAVA Write a static method that takes an array of a generic type as its only...

    JAVA Write a static method that takes an array of a generic type as its only argument. The method should display the array and return the number of elements in the array. Test the method in main with at least three arrays of objects. SAMPLE OUTPUT Here is an Integer array 12 21 7 16 8 13 That array held 6 elements Here is a String array one two three four That array held 4 elements Here is a Double...

  • //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList...

    //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.

  • Write code in static void main method class, which creates a boolean array called flags, size...

    Write code in static void main method class, which creates a boolean array called flags, size 10, and using a for loop sets each element to alternating values (true at index zero, false at index one Write code in a static void main method class, which creates a float array called sales, size 5, uses an initializer list to instantiate its elements with positive values having two decimal places (example 7.25), and using a for loop reads those values of...

  • Write a java Program Initialize Array. Write a Java method initArray() with the following header to...

    Write a java Program Initialize Array. Write a Java method initArray() with the following header to initialize a one-dimensional array a such that the element values (integers) are twice the index value. For example, if i = 23, then a23 = 46. The function is void with one parameter -- the integer array of a[]. Then write a main() with an int[] array2i size 100 to call

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • java code Write a method called reverse that takes an array of integers as an input...

    java code Write a method called reverse that takes an array of integers as an input and returns the same values in reverse order as the output. Test your method in the main.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT