Question

I need help with the last method listed in the problem: Implement a class Grid that...

I need help with the last method listed in the problem: Implement a class Grid that stores measurements in a rectangular grid. The grid has a given number of rows and columns, and a description string can be added for any grid location. Supply the following constructor and methods:

public Grid(int numRows, int numColumns)

public void add(int row, int column, String description)

public String getDescription(int row, int column)

public ArrayList getDescribedLocations()

Here, Location is an inner class that encapsulates the row and the column of a grid location.

This is what I have so far for the last method:

import javax.xml.stream.Location;
import java.util.ArrayList;
/**
 * The object class for the Grid element which will store the measurement and description
 * */
class gridElement {
    int measurement;
    String description;
}

public class Grid {
    //Instance Variables
    private int row;
    private int column;
    private gridElement[][] gridArray;

//Constructors
    /**
     * Created a Grid with the provided rows and columns
     * @param numRows for the grid
     * @param numColumns for the grid
     * */
    public Grid (int numRows, int numColumns) {

    gridArray = new gridElement[numRows][numColumns];
    }
//Methods
    /**
     * A method to add a description to certain rows + columns
     * @param row in the grid
     * @param column in the griod
     * @param description of the location on the grid
     * */
    public void add(int row, int column, String description) {
        gridArray[row][column].description = description;
    }
    /**
     * A method to get the description
     * @param row of the grid
     * @param column of the grid
     * @return gridArray row & column description
     * */
    public String getDescription(int row, int column) {

        return this.gridArray[row][column].description;
    }
    /**
     * A method that returns the row and column of the grid elements which have a description
     * */
    public ArrayList<Location> getDescribedLocations(){
        int k = 0;
        ArrayList<Location> location = new ArrayList<>();

        for ( int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (gridArray[i][j].description != null) {
                    location.add(" i = " + i + " and j = " + j);
                }
            }
        }
        return location;

    }
}

I can't figure out how to display the row and columns that are filled with descriptions when the arraylist type is of "Location"

ERROR:

add(javax.xml.stream.Location)in ArrayList cannot be applied to(java.lang.String)

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

// import javax.xml.stream.Location; this is an interface

import java.util.ArrayList;

/**

* The object class for the Grid element which will store the measurement and description

* */

class gridElement

{

       int measurement;

       String description;

}

public class Grid {

      

       //Instance Variables

    private int row;

    private int column;

    private gridElement[][] gridArray;

      

    // create an inner class Location that encapsulates the row and the column of a grid location.

       class Location

       {

             private int row;

             private int col;

            

             public Location(int row, int col)

             {

                    this.row = row;

                    this.col = col;

             }

            

             public int getRow()

             {

                    return row;

             }

            

             public int getCol()

             {

                    return col;

             }

       }

      

       //Constructors

    /**

     * Created a Grid with the provided rows and columns

     * @param numRows for the grid

     * @param numColumns for the grid

     * */

    public Grid (int numRows, int numColumns) {

       gridArray = new gridElement[numRows][numColumns];

    }

   

//Methods

    /**

     * A method to add a description to certain rows + columns

     * @param row in the grid

     * @param column in the griod

     * @param description of the location on the grid

     * */

    public void add(int row, int column, String description) {

        gridArray[row][column].description = description;

    }

   

    /**

     * A method to get the description

     * @param row of the grid

     * @param column of the grid

     * @return gridArray row & column description

     * */

    public String getDescription(int row, int column) {

        return this.gridArray[row][column].description;

    }

   

    /**

     * A method that returns the row and column of the grid elements which have a description

     * */

    public ArrayList<Location> getDescribedLocations(){

        //int k = 0;

        ArrayList<Location> location = new ArrayList<>();

        for ( int i = 0; i < row; i++) {

            for (int j = 0; j < column; j++) {

                if (gridArray[i][j].description != null) {

                    location.add(new Location(i,j));

                }

            }

        }

        return location;

    }

}

Add a comment
Know the answer?
Add Answer to:
I need help with the last method listed in the problem: Implement a class Grid that...
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
  • 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;...

  • (Java Zybooks) Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Use s...

    (Java Zybooks) Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Use separate print statements to print the row and column. Ex: numRows = 2 and numColumns = 3 prints: 1A 1B 1C 2A 2B 2C import java.util.Scanner; public class NestedLoops { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int...

  • In Java; Given numRows and numColumns, print a list of all seats in a theater. Rows...

    In Java; Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Use separate print statements to print the row and column. Ex: numRows = 2 and numColumns = 3 prints: 1A 1B 1C 2A 2B 2C import java.util.Scanner; public class NestedLoops { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int...

  • (Java Zybooks) Given numRows and numColumns, print a list of all seats in a theater. Rows...

    (Java Zybooks) Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Use separate print statements to print the row and column. Ex: numRows = 2 and numColumns = 3 prints: 1A 1B 1C 2A 2B 2C import java.util.Scanner; public class NestedLoops { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int...

  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...

  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean...

  • Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int...

    Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int row) String field(int row, int column) Please use the CSVReader and CSVReaderTester class to complete the code. I have my own CSV files and cannot copy them to here. So if possible, just use a random CSV file. CSVReader.java import java.util.ArrayList; import java.util.Scanner; import java.io.*; /**    Class to read and process the contents of a standard CSV file */ public class CSVReader {...

  • Modify the Auction class according to these exercises: 4.48: close method 4.49: getUnsold method 4.51: Rewrite getLot method 4.52: removeLot method For help watch Textbook Video Note 4.2 which sho...

    Modify the Auction class according to these exercises: 4.48: close method 4.49: getUnsold method 4.51: Rewrite getLot method 4.52: removeLot method For help watch Textbook Video Note 4.2 which shows you how to use and test the auction project, as well as solving the getUnsold exercise. Document with javadoc and use good style (Appendix J) as usual. Test your code thoroughly as you go. Create a jar file of your project. From BlueJ, choose Project->Create Jar File... Check the "Include...

  • Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static...

    Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static String solve(char[][] grid) {        // TODO        /*        * 1. Construct a graph using grid        * 2. Use BFS to find shortest path from start to finish        * 3. Return the sequence of moves to get from start to finish        */               // Hardcoded solution to toyTest        return...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

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