Question

Using java :

In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in creating board-game applications. Later we will use this class to create a board game. A grid is a two-dimensional matrix (see example below) with the same number of rows and columns. You can create a grid of size 8, for example, it’s an 8x8 grid. There are 64 cells in this grid. A cell in the grid can have any arbitrary value. Later in this course, we will learn how to create a cell that can hold any arbitrary values. However, for the time being, we will assume that the cell-values are non-negative integers. If a cell contains a value ‘0’ that means, the cell is empty. To identify a cell, you need row number and column number, for example, cell (1, 3) means 3rd cell of the 1st row. You need to create a class that satisfies the following requirements.

1 2 3 4 5 6 7 7 6 5 4 3 2 Each box is a cell​​​​​​

Tasks Write tests and code for each of the following requirements, in order. The words in bold indicate message names. Whenever a requirement says the user can "ask whether...", the expected answer is boolean. Whenever a requirement speaks of a "particular" item, then that item will be an argument to the method. 1. The user can create a Grid specifying the number of row and column. Also the user can create a Grid specifying the size only.

2. The user can ask a Grid whether its isEmpty. A grid is empty when all the cells of the grid is empty.

3. The user can clear all the cells of the grid. It’s a void method.

4. The user can ask a Grid whether a particular cell isValid. Each box is a “cell”

5. The user can ask a Grid to set a particular value by setValue to a particular row and col. The grid sets the value only if that cell is valid.

6. The user can ask a Grid to getValue (in this case integer) from a particular row and col. The grid returns the value only if the locations are valid.

7. The user can ask a Grid to set part of its row and column with particular values. Example, setCells(int rows[], int cols[], int vals[]), in this method user can specify the indexes of rows and columns that the user wants to set with some particular values (supplied in vals array). Note that, rows, cols, and vals arrays should be of same size.

8. Make another overridden method for setCells(int rows[], int cols[], int v), that will set particular rows and cols of a grid with a particular value.

Apart from these basic requirements, feel free to add more behaviors to the Grid class that you think appropriate. Please provide an explanation for those behaviors as a comment in the source file.

Write a test class to test the behaviors of the Grid class and submit the test class as well.

Note: A grid is composed of many cells. So, you may want to decompose the grid into a Cell class that encapsulates all the necessary behavior of a cell in the grid

0 0
Add a comment Improve this question Transcribed image text
Answer #1
// Grid.java
public class Grid {
    private int row, col;

    // Note: we have taken Integer(wrapper) not integer(primitive) type
    // so that empty cells have default value as null
    private Integer data[][];

    // constructors
    public Grid(int size) {
        row = col = size;
        data = new Integer[size][size];
    }

    public Grid(int row, int col) {
        this.row = row;
        this.col = row;
        data = new Integer[row][col];
    }

    // check if all cells are empty
    public boolean isEmpty() {
        for(Integer[] r: data) {
            for(Integer v: r) {
                if(v != null) {
                    return false;
                }
            }
        }
        return true;
 }

    public void clear() {
        data = new Integer[row][col];
    }

    public boolean isValid(int r, int c) {
        if(r < 0 || r >= row || c < 0 || c >= col) {
            return false;
        } else {
            return true;
        }
    }

    public void setValue(int r, int c, Integer value) {
        if(isValid(r, c)) {
            data[r][c] = value;
        }
    }
    public Integer setValue(int r, int c) {
        if(isValid(r, c)) {
            return data[r][c];
        } else {
            return null;
        }
    }

    public void setCells(int r[], int c[], int values[]) {
        if(r.length == c.length && r.length == values.length) {
            int len = r.length;

            for(int i=0; i<len; i++) {
                if(isValid(r[i], c[i])) {
data[r[i]][c[i]] = value;
                }
            }
        }
    }

    public void printGrid() {
        System.out.println();
        for(Integer r[]: data) {
            for(Integer v: r) {
                System.out.printf("%3s", (v==null?"-":v));
            }
            System.out.println();
        }
    }

    public static void main(String args[]) {
        Grid g = new Grid(5);
        System.out.println(g.isEmpty());
  g.printGrid();

        g.setCells(new int[]{1, 2, 3}, new int[]{1, 2, 3}, new int[]{1, 2, 3});

        g.printGrid();
    }
}

Grid.java 75 } } 76 77 public void printGrid () 78e System.out.println(); for(Integer r[]: data) for(Integer System.out.print

*******************************************************END**********************************************************

Add a comment
Know the answer?
Add Answer to:
Using java : In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in crea...
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
  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...

    Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections; class Grid { private boolean bombGrid[][]; private int countGrid[][]; private int numRows; private int numColumns; private int numBombs; public Grid() { this(10, 10, 25); }    public Grid(int rows, int columns) { this(rows, columns, 25); }    public Grid(int rows, int columns, int numBombs) { this.numRows = rows; this.numColumns = columns; this.numBombs = numBombs; createBombGrid(); createCountGrid(); }    public int getNumRows() { return numRows;...

  • Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row...

    Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row major order. Tries to ensure even spacing. void print_grid(int * my_array, int rows, int cols){ int i,j; for(i=0; i<rows; i++){ for(j=0; j<cols; j++){ if(my_array[i*cols + j]!=-1){ printf(" %d ", my_array[i*cols + j]); } else{ printf("%d ", my_array[i*cols + j]); } } printf("\n"); } } int main(int argc, char *argv[]) { int seed,rows,cols;...

  • need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0...

    need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0 sudoku(int g[][9]); //constructor //Postcondition: grid = g void initializeSudokuGrid(); //Function to prompt the user to specify the numbers of the //partially filled grid. //Postcondition: grid is initialized to the numbers // specified by the user. void initializeSudokuGrid(int g[][9]); //Function to initialize grid to g //Postcondition: grid = g; void printSudokuGrid(); //Function to print the sudoku grid. bool solveSudoku(); //Funtion to solve the sudoku problem....

  • The game Battleship is played on a grid board. Each opponent has multiple ships that are placed on the grid where the o...

    The game Battleship is played on a grid board. Each opponent has multiple ships that are placed on the grid where the other opponent cannot see them. In order to attack, each player takes turns calling out coordinates on a grid. If the attacker calls out a coordinate that hits their opponent's ship, they must call out, "Hit." You are going to be developing a computer program to mimic this game. Use the Gridlayout that is six columns by six...

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • The game Battleship is played on a grid board. Each opponent has multiple ships that are...

    The game Battleship is played on a grid board. Each opponent has multiple ships that are placed on the grid where the other opponent cannot see them. In order to attack, each player takes turns calling out coordinates on a grid. If the attacker calls out a coordinate that hits their opponent's ship, they must call out, "Hit." You are going to be developing a computer program to mimic this game. Use the Gridlayout that is six columns by six...

  • Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques...

    Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques we have been using in class are allowed. Nothing else. The program should read the initial state of the board by reading in "alive" cells from a user input data file. Meaning your program should ask the user the name of the data file. Assume all the other cells are "dead." Make sure to use modular coding techniques. The main program should be pretty...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every...

    Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every part, and include java doc and comments Create a battleship program. Include Javadoc and comments. Grade 12 level coding style using java only. You will need to create a Ship class, Location class, Grid Class, Add a ship to the Grid, Create the Player class, the Battleship class, Add at least one extension. Make sure to include the testers. Starting code/ sample/methods will be...

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