Question

Create a program BloomGrid.java that creates a 5-by-5 2D array, then asks the user for an...

Create a program BloomGrid.java that creates a 5-by-5 2D array, then asks the user for an x and y coordinate. Then, print out the contents of the 2D array, which should have a 3-by-3 plus shape displayed on the grid with the center of the plus being your coordinate. You do not have to make any changes to the grid orientation. If the user places a point at the edge of the grid, you should be able to place any portion of the plus that's still on the grid and not crash.

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

import java.util.Scanner;

public class BloomGrid {

    static boolean isValid(char[][] grid, int x, int y) {
        return x < grid.length && y < grid[x].length;
    }

    static void set(char[][] grid, int x, int y) {
        if(isValid(grid, x-1, y)) grid[x-1][y] = 'X';
        if(isValid(grid, x, y-1)) grid[x][y-1] = 'X';
        if(isValid(grid, x, y)) grid[x][y] = 'X';
        if(isValid(grid, x, y+1)) grid[x][y+1] = 'X';
        if(isValid(grid, x+1, y)) grid[x+1][y] = 'X';
    }

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

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        char[][] grid = new char[5][5];
        for(int i = 0; i < grid.length; ++i) {
            for(int j = 0; j < grid[i].length; ++j) {
                grid[i][j] = '.';
            }
        }

        System.out.print("Enter a x-coordinate: ");
        int x = in.nextInt();
        System.out.print("Enter a y-coordinate: ");
        int y = in.nextInt();

        set(grid, x, y);

        print(grid);
    }

}



Add a comment
Know the answer?
Add Answer to:
Create a program BloomGrid.java that creates a 5-by-5 2D array, then asks the user for an...
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
  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers...

    LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers and stores them in an array called data. Then call a method that accepts the array as an argument, sorts the array in ascending order and prints out the original AND sorted array (Hint: The method does not return anything in this case, so it should be set to void).

  • Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array...

    Write a Java program in Eclipse. . Write a Java program that: 1. Creates an array of 100 integers. Initialize the array to have all zeros. 2. Puts even numbers starting from 6 (excluding multiples of 12) into the first 50 slots of the array above. So numbers 6,8,10,14,16,18,20,22,26, etc. go into the first 50 positions in the array. Print the array. 3. Puts random numbers between 45 and 55 into the second 50 slots of the array above (second...

  • *Using C++* You will create a program that uses a Critter class to move around a...

    *Using C++* You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • with C++ You will create a program that uses a Critter class to move around a...

    with C++ You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • (C programing, Not C++) Write a program in C that asks the user to input 10...

    (C programing, Not C++) Write a program in C that asks the user to input 10 numbers. Store these numbers in an array. Then ask the user to input a single number. Your program should execute a linear search on the array, trying to find that number. If the number exists in the array, have the program print out which position/element the number was found at. If the target number is not in the array, have the program print out...

  • In C plus plus Objective: Create an array of numbers based upon user input. Program logic:...

    In C plus plus Objective: Create an array of numbers based upon user input. Program logic: Ask the user for how big to size the array. Create an array based upon that size. Ask for a number, add that number to the array. Repeat adding to the end until all numbers have been entered or until they enter -1 for the number. Print the list.

  • A = randi(6, 7); %creates a 2D array with 6 rows %with random integers sz =...

    A = randi(6, 7); %creates a 2D array with 6 rows %with random integers sz = size(A); for i = 1:sz(1) for j = 1:sz (2) if rem(j, 2)==0 A(:, j) = 0; end end end Rewrite this code in vectorized form you may not use any loops or if statements). Your answer should be one line of code:

  • using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an...

    using c++ 13 do pretty much whatever they want Exercise: Array Resizing You will create an array manipulation program that allows the user is to pre much whatever meant to ananas Wananching the program the user will passat are the contains a set of value and that w 2 Check to see the could be opened at the program was not opened the continue 3. Create an array and with the values from Presente en de h and powermany reded...

  • Write a program that asks the user to input 10 integers of an array. The program...

    Write a program that asks the user to input 10 integers of an array. The program then inserts a new value at position (or index) given by the user, shifting each element right and dropping off the last element. For example, in the sample input/output below, the program will insert the value 30 at index 3. All the previous numbers of the array starting from position 3 (i.e., 7 1 20 9 23 8 22 will be shifted one position...

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