Question

Write Java code to create a 2D array that mimics the one below. Do not use...

Write Java code to create a 2D array that mimics the one below. Do not use curly brackets to assign values. Instead, use nested loops and if-else or switch.

x

o

o

o

x

o

x

o

x

o

o

o

z

o

o

o

x

o

x

o

x

o

o

o

x

Homework Answers

Answer #1
public class TwoDArray {

    public static void main(String[] args) {
        char[][] data = new char[5][5];
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (i == 2 && j == 2) {
                    data[i][j] = 'z';
                } else if (i == j || i == 4-j) {
                    data[i][j] = 'x';
                } else {
                    data[i][j] = 'o';
                }
            }
        }
        // print array
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                System.out.print(data[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Know the answer?
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
Similar Questions
1. a. Write the Java code to fill a Java array of ints of size 100...
1. a. Write the Java code to fill a Java array of ints of size 100 with the value -1. b. Write the Java code to create an ArrayList of Strings, and add the Strings "Just", "Do", "It!" to it.
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an...
DESCRIPTION: You will be given a 2D array, called matrix, of Strings. The array has an unknown number of cells filled with data. Your goal is to iterate through the 2D array and keep a count of how many cells are full. You will be given the dimensions of the array. INPUT: All input has been handled for you: Two integers denoting the dimensions of the array. These are the integer variables rows and cols. A partially filled 2-D array....
[Multiple correct answers] Write a line of Java code to create an array of integers called...
[Multiple correct answers] Write a line of Java code to create an array of integers called studentIDs that can hold 1200 values.    int [] studentIDs = new int[1200];     int studentIDs [] = new int[1200];     int studentIDs = new int[1200];     int [] studentIDs = new int(1200);     int studentIDs [] = new int(1200);     int [] studentIDs = int[1200];     int studentIDs [] = int[1200];
Create a 2D numpy array that contains values (4, 3, 1, 33, 21) in the first...
Create a 2D numpy array that contains values (4, 3, 1, 33, 21) in the first row values (67, 27, 89, 34, 67) in the second row and values (60, 99, 89, 12, 43) in the third row Your code should print the following: [[ 4 3 1 33 21] [67 27 89 34 67] [60 99 89 12 43]] Write code that prints values [34, 67] [12, 43]. You must use the colon : based slicing approach to complete...
Write the below code to use HTML and JavaScript. 1)Write a JavaScript program to create a...
Write the below code to use HTML and JavaScript. 1)Write a JavaScript program to create a new string from a given string changing the position of first and last characters. The string length must be greater than or equal to 1. 2)Write a JavaScript program to check whether a string starts with 'Java' and false otherwise
Write a Java part code to do the following   : ** Suppose a file called infile stored...
Write a Java part code to do the following   : ** Suppose a file called infile stored in drive D: ,filled with five integers(1000,200,3030,40 and 500) Read  the integers from infile then store them in array called ar[] and print it elements. Method called search( ) to print the location of   value 500. Method called sort() to sort array elements. Build another file called outfile in drive D: to save the sorted array elements . Note : Use ArrayIndexOutOfBoundsException when you use array...
Write a program to do the following. JAVA CODE • Input an integer n. • Create...
Write a program to do the following. JAVA CODE • Input an integer n. • Create a BinarySearchTree S inserting the keys 1, 2, . . . , n in that order, which will result in a completely-skewed tree. • Measure the time to search for n + 1 in S. 4 • Display the time taken for search.
Please create an array of Leg objects, one constructor that takes three parameters as constant C...
Please create an array of Leg objects, one constructor that takes three parameters as constant C string, and one number representing the distance in miles between the two cities Write a code block to create a static array (that is, not dynamic and not a vector) of 3 Leg objects using city names of your choosing. That's THREE objects, each created using THREE parameters. For example, the Leg class declaration looked like, class Leg { const char* const startCity; const...
For this lab assignment you will need to write some code and create some graphs. You...
For this lab assignment you will need to write some code and create some graphs. You may use excel to create your graphs, or other language of your choice. Your data needs to demonstrate the experimental running time for Selection Sort (code in book), Merge Sort (code in book), and the Arrays.sort() method. Here is a basic outline of how you need to code this assignment. 1) Create several arrays of size 100, 1000, 10000, 100000, 1000000, … (you need...
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January - December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...