Results 1 to 3 of 3
  1. #1
    djester55 is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default homework help comparing strings

    hey i'am really have issue with strings. The class baseboard has been provided to me for this project.i have to use indexof method to compare the two string values and highlight where the word was found and save it to a file. the problem i'am having is with the indexof method. i have included a copy of the class file provided by my teacher , a copy of the assignment and what i have written for code so far. any help would be really appreciated


    /**
    * Classes to support 2D board-based problems.
    * Cells in a board have color as well as character attributes.
    * Last modified: 09.23.09
    *
    * Copyright: You may freely copy, modify or use this code strictly
    * for non-commerical instructional purposes.
    *
    *
    */

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Color;
    import java.util.Random;
    import java.util.Set;
    import java.util.HashSet;
    import java.util.Iterator;

    public class BaseBoard extends JFrame
    {
    protected Grid grid;
    protected int rows, cols;
    protected int frameWidth, frameHeight;
    public static int VIEWABLE_SCREEN_WIDTH, VIEWABLE_SCREEN_HEIGHT;

    /**
    * The constructor will attempt to create a board with the specified rows
    * and columns. If the calculated size of the board given the minimum
    * cell size of 2 pixels is at most 100 pixels less than the screen
    * resolution, then a board with the specified dimension will be constructed.
    * Otherwise, the largest board possible for the screen resolution and
    * minimum cell size will be constructed. The actual dimensions of the
    * board can be obtained by invoking getRows and getCols.
    *
    * @param c The suggested number of columns
    * @param r The suggested number of rows
    */
    public BaseBoard(int c, int r)
    {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    // maximum frame size is 100 pixels smaller than screen resolution
    VIEWABLE_SCREEN_WIDTH = screenSize.width - 100;
    VIEWABLE_SCREEN_HEIGHT = screenSize.height - 100;
    //System.out.println(VIEWABLE_SCREEN_WIDTH + " " + VIEWABLE_SCREEN_HEIGHT);

    this.init(c, r);
    }

    /**
    * Returns a single-dimensional array of cell objects making up the board object.
    * Cell[cols] is the first cell of the second row.
    */
    public Cell[] getCells() { return grid.getCells(); }

    /**
    * Returns a two-dimensional array of cell objects making up the board object.
    * Note that array indexing is row major as opposed to all cell accesses using
    * indices as parameters.
    */
    public Cell[][] getCells2D() { return grid.getCells2D(); }

    /**
    * Repaint the graphics context of the board object.
    */
    public void repaint() { grid.repaint(); }

    /**
    * Set the pen color to c for character writng.
    * Default pen color is red.
    */
    public void setPenColor(Color c) { grid.setPenColor(c); }

    /**
    * Returns the current color of the pen for text writing.
    */
    public Color getPenColor() { return grid.getPenColor(); }

    /**
    * Sets the current color of the grid line pen to c.
    */
    public void setLineColor(Color c) { grid.setLineColor(c); }

    /**
    * Returns the current color of the grid line pen.
    */
    public Color getLineColor() { return grid.getLineColor(); }

    /**
    * Returns number of rows in the board object.
    */
    public int getRows() { return rows; }

    /**
    * Returns number of columns in the board object.
    */
    public int getCols() { return cols; }

    /**
    * Sets the color of the cell at coordinate (x,y) to the color c.
    */
    public void fillCell(int x, int y, Color c) { grid.setColor(x, y, c); }

    /**
    * Sets the color of cell (x,y) to black.
    */
    public void turnOn(int x, int y) { grid.turnOn(x, y); }

    /**
    * Removes the color attribute of cell (x,y).
    */
    public void turnOff(int x, int y) { grid.turnOff(x, y); }

    /**
    * Returns true if cell (x,y) has a color attribute, false otherwise.
    */
    public boolean isOn(int x, int y) { return grid.isOn(x, y); }

    /**
    * Marks a cell with a boolean flag to allow for more complex operations on
    * a board object.
    */
    public void mark(int x, int y) { grid.mark(x, y); }

    /**
    * Removes the boolean marking from a cell object at coordinate (x, y).
    */
    public void unmark(int x, int y) { grid.unmark(x, y); }

    /**
    * Removes the boolean markings of all cells in the board object.
    */
    public void unmarkAll() { grid.unmarkAll(); }

    /**
    * Returns true if a cell (x,y) is marked, false otherwise.
    */
    public boolean isMarked(int x, int y) { return grid.isMarked(x, y); }

    /**
    * Fills cells within the rectangle defined by (xStart, yStart) and
    * (xEnd, yEnd) with the color c, where (xStart, yStart) is the coordinate
    * of the top left-hand coordinate of the rectangle and (xEnd, yEnd) is
    * that of the bottom right-hand corner.
    */
    public void fillCells(int xStart, int yStart, int xEnd, int yEnd, Color c)
    {
    grid.fillCells(xStart, yStart, xEnd, yEnd, c);
    }

    /**
    * Draws character c in the cell with coordinate (x,y).
    */
    public void setChar(int x, int y, char c) { grid.setChar(x, y, c); }

    /**
    * Returns the character drawn at coordinate (x,y). If the cell is
    * empty, the character returned is a space -- character ' '.
    */
    public char getChar(int x, int y) { return grid.getChar(x, y); }

    /**
    * Returns the characters stored in the board object as a single-dimensional
    * array. array[0] to array[cols - 1] are the characters in the first
    * row.
    */
    public char[] getChars() { return grid.getChars(); }

    /**
    * Returns the characters stored in the board object as a two-dimensional
    * array.
    */
    public char[][] getChars2D() { return grid.getChars2D(); }

    /**
    * Clears the color and characters of all cells in the board.
    */
    public void clear() { grid.clear(); }

    /**
    * Return a string representation of the board object made up of
    * a sequencing of its characters.
    */
    public String toString()
    {
    String s = "";

    for (int y = 0; y < rows; y++)
    {
    for (int x = 0; x < cols; x++)
    {
    s += grid.getChar(x, y);
    }
    s += "\n";
    }
    return s;
    }

    /**
    * Modifies the dimension of the board according to c and r. The contents
    * of the board are cleared.
    */
    public void setDimension(int c, int r)
    {
    this.init(c, r);
    }

    public void showGrid()
    {
    grid.showGrid();
    }

    public void hideGrid()
    {
    grid.hideGrid();
    }

    /**
    * Pauses the repainting of the board for 10 msec. Used in animation.
    */
    public void pause()
    {
    try { Thread.sleep(10); } catch (Exception e) {}
    }

    public void pause(int msec)
    {
    try { Thread.sleep(msec); } catch (Exception e) {}
    }

    public Color getColor(int x, int y)
    {
    return grid.getColor(x, y);
    }

    public void mouseClicked(int x, int y)
    {
    // must be implemented in child class to process
    // mouse click events
    }

    public void mouseDragged(int x1, int y1, int x2, int y2)
    {
    // must be implemented in child class to process
    // mouse drag events
    }

    public void mouseAt(int x, int y) { }


    public void enableMouse()
    {
    grid.enableMouse();
    }

    public void disableMouse()
    {
    grid.disableMouse();
    }

    private void init(int c, int r)
    {
    cols = c;
    rows = r;
    grid = new Grid(cols, rows, this);
    rows = grid.getRows();
    cols = grid.getCols();
    Dimension d = grid.getEnclosingFrameDimension();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.addComponentListener(grid);
    grid.setOpaque(true);
    this.setContentPane(grid);
    this.setSize(d.height, d.width);
    this.setVisible(true);
    }

    }

    class Grid extends JPanel implements ComponentListener, MouseListener, MouseMotionListener
    {
    private int preferredRows, preferredCols;
    private Graphics gphcs;
    private int rows, cols;
    private int cellSize;
    private JFrame window;
    private final int padding = 20;
    private final int minCellSize = 2;
    private boolean gridVisible = true;
    private boolean mouseEnabled = false;
    private int xStart, yStart, xEnd, yEnd;
    // used to optimize mouse motion handling
    private int lastRow, lastCol;

    // minimum cell size required to display character
    private final int minCellSizeChar = 15;

    private Color penColor = Color.red;
    private Color lineColor = Color.lightGray;
    Cell cells[][];


    public Grid(int c, int r, JFrame win)
    {
    preferredRows = r;
    preferredCols = c;
    window = win;

    setDimensions();

    cells = new Cell[rows][cols];
    for (int i = 0; i < rows; i++)
    for (int j = 0; j < cols; j++)
    cells[i][j] = new Cell();


    }

    public void enableMouse()
    {
    if (mouseEnabled)
    return;
    else
    {
    this.addMouseListener(this);
    this.addMouseMotionListener(this);
    mouseEnabled = true;
    }
    }

    public void disableMouse()
    {
    if (mouseEnabled)
    {
    mouseEnabled = false;
    this.removeMouseListener(this);
    this.removeMouseMotionListener(this);
    }
    }

    public void showGrid()
    {
    gridVisible = true;
    this.repaint();
    }

    public void hideGrid()
    {
    gridVisible = false;
    this.repaint();
    }

    public void setPenColor(Color c) { penColor = c; }

    public Color getPenColor() { return penColor; }

    public void setLineColor(Color c) { lineColor = c; }

    public Color getLineColor() { return lineColor; }

    public int getRows() { return rows; }

    public int getCols() { return cols; }

    public void setChar(int x, int y, char c) { cells[y][x].setChar(c); repaint(); }

    public char getChar(int x, int y) { return cells[y][x].getChar(); }

    public boolean isMarked(int x, int y) { return cells[y][x].isMarked(); }

    public void unmark(int x, int y) { cells[y][x].unmark(); }

    public void mark(int x, int y) { cells[y][x].mark(); }

    public boolean isOn(int x, int y) { return cells[y][x].isOn(); }

    public Dimension getEnclosingFrameDimension()
    {
    return new Dimension(rows * cellSize + 4 * padding, cols * cellSize + 2 * padding);
    }

    public void turnOn(int x, int y)
    {

    if (!cells[y][x].isOn())
    cells[y][x].turnOn();
    repaint();
    }

    public void turnOff(int x, int y)
    {
    cells[y][x].turnOff();
    repaint();
    }

    public void setColor(int x, int y, Color c)
    {
    cells[y][x].setColor(c);
    repaint();
    }

    public Color getColor(int x, int y)
    {
    return cells[y][x].getColor();
    }

    public Cell[] getCells()
    {
    Cell[] cells1D = new Cell[rows*cols];
    for (int y = 0; y < rows; y++)
    {
    for (int x = 0; x < cols; x++)
    {
    cells1D[cols*y+x] = cells[y][x];
    }
    }
    return cells1D;
    }

    public Cell[][] getCells2D() { return cells; }

    public char[] getChars()
    {
    char[] chars = new char[rows*cols];
    for (int y = 0; y < rows; y++)
    {
    for (int x = 0; x < cols; x++)
    {
    chars[cols*y+x] = cells[y][x].getChar();
    }
    }
    return chars;
    }

    public char[][] getChars2D()
    {
    char[][] chars2d = new char[rows][cols];

    for (int y = 0; y < rows; y++)
    {
    for (int x = 0; x < cols; x++)
    {
    chars2d[y][x] = cells[y][x].getChar();
    }
    }
    return chars2d;
    }

    public void fillCells(int xStart, int yStart, int xEnd, int yEnd, Color c)
    {
    for (int x = xStart; x <= xEnd; x++)
    {
    for (int y = yStart; y <= yEnd; y++)
    {
    cells[y][x].setColor(c);
    }
    }
    repaint();
    }

    public void unmarkAll()
    {
    for (int x = 0; x < cols; x++)
    {
    for (int y = 0; y < rows; y++)
    {
    cells[y][x].unmark();
    }
    }
    }

    // clear the grid
    public void clear()
    {
    if (gphcs == null) return;

    for (int x = 0; x < cols; x++)
    {
    for (int y = 0; y < rows; y++)
    {
    Color clr = cells[y][x].getColor();
    if (clr != null)
    {
    int tlx = padding + x * cellSize;
    int tly = padding + y * cellSize;
    gphcs.clearRect(tlx, tly, cellSize, cellSize);
    }
    }
    }

    cells = new Cell[rows][cols];
    for (int i = 0; i < rows; i++)
    for (int j = 0; j < cols; j++)
    cells[i][j] = new Cell();

    repaint();
    }

    public void componentResized(ComponentEvent e)
    {
    int frameWidth = window.getWidth();
    int frameHeight = window.getHeight();

    int pixR = (frameHeight - 4 * padding) / rows;
    int pixC = (frameWidth - 2 * padding) / cols;
    cellSize = Math.min(pixR, pixC);

    repaint();
    }

    private int translateX(int x)
    {
    return (x - padding) / cellSize;
    }

    private int translateY(int y)
    {
    return (y - padding) / cellSize;
    }

    public void componentMoved(ComponentEvent e) {}

    public void componentHidden(ComponentEvent e) {}

    public void componentShown(ComponentEvent e) {}

    public void mouseClicked(MouseEvent e)
    {
    int c = translateX(e.getX());
    int r = translateY(e.getY());

    if (c >= cols || r >= rows)
    {
    return;
    }
    else
    {
    BaseBoard b = (BaseBoard) window;
    b.mouseClicked(c,r);
    }
    }

    public void mouseExited(MouseEvent e) { }

    public void mouseEntered(MouseEvent e) { }

    public void mouseMoved(MouseEvent e)
    {
    this.processMouseMotion(e);
    }

    private void processMouseMotion(MouseEvent e)
    {
    int col = translateX(e.getX());
    int row = translateY(e.getY());


    if (col < 0 || row < 0 || col >= cols || row >= rows ||
    (lastRow - row == 0 && lastCol - col == 0))
    {
    return;
    }
    else
    {
    BaseBoard b = (BaseBoard) window;
    b.mouseAt(col, row);
    lastRow = row;
    lastCol = col;
    }

    }

    public void mouseReleased(MouseEvent e)
    {
    processDragEvent(e);
    }

    public void mousePressed(MouseEvent e)
    {
    // obtain x and y coord of a drag event
    xStart = translateX(e.getX());
    yStart = translateY(e.getY());
    }

    public void mouseDragged(MouseEvent e)
    {
    }

    private void processDragEvent(MouseEvent e)
    {
    xEnd = translateX(e.getX());
    yEnd = translateY(e.getY());

    // check first if coordinates are within board object
    if (xStart < 0 || yStart < 0 || xEnd >= cols || yEnd >= rows ||
    ((xStart - xEnd) == 0 && (yStart - yEnd) == 0))
    {
    return;
    }
    else
    {
    BaseBoard b = (BaseBoard) window;
    b.mouseDragged(xStart, yStart, xEnd, yEnd);
    }
    }

    private void setDimensions()
    {
    // if user specifies a grid dimension that is larger than what the screen size can
    // allow using the minCellSize, then the dimensions are trimmed
    if ((minCellSize * preferredRows + 2 * padding > BaseBoard.VIEWABLE_SCREEN_HEIGHT) ||
    (minCellSize * preferredCols + 2 * padding > BaseBoard.VIEWABLE_SCREEN_WIDTH))
    {
    cellSize = minCellSize;
    rows = Math.min((BaseBoard.VIEWABLE_SCREEN_HEIGHT - 2 * padding) / minCellSize, preferredRows);
    cols = Math.min((BaseBoard.VIEWABLE_SCREEN_WIDTH - 2 * padding) / minCellSize, preferredCols);
    }
    else
    {
    int pixR = (BaseBoard.VIEWABLE_SCREEN_HEIGHT - 2 * padding) / preferredRows;
    int pixC = (BaseBoard.VIEWABLE_SCREEN_WIDTH - 2 * padding) / preferredCols;
    cellSize = Math.min(pixR, pixC);
    rows = preferredRows;
    cols = preferredCols;
    }
    }

    public void paintComponent(Graphics g)
    {
    gphcs = g;
    super.paintComponent(g);

    Font f = new Font("SansSerif", Font.PLAIN, cellSize * 2 / 3);
    g.setFont(f);
    g.setColor(lineColor);

    if (gridVisible)
    {
    for (int i = 0; i <= cols; i++)
    {
    g.drawLine(i*cellSize+padding, padding, i*cellSize+padding, rows*cellSize+padding);
    }
    for (int j = 0; j <= rows; j++)
    {
    g.drawLine(padding, j*cellSize+padding, cols*cellSize+padding, j*cellSize+padding);
    }
    }

    for (int x = 0; x < cols; x++)
    {
    for (int y = 0; y < rows; y++)
    {
    int tlx = padding + x * cellSize;
    int tly = padding + y * cellSize;
    Color clr = cells[y][x].getColor();
    if (clr != null)
    {
    g.setColor(clr);
    g.fill3DRect(tlx+1, tly+1, cellSize-1, cellSize-1, true);
    }
    else
    {
    g.clearRect(tlx+1, tly+1, cellSize-1, cellSize-1);
    }
    g.setColor(penColor);
    if (cellSize > minCellSizeChar)
    g.drawString(""+cells[y][x].getChar(), tlx + cellSize/3, tly + 2*cellSize/3);
    }
    }
    }
    }

    class Cell
    {
    Color c = null;
    boolean marked = false;
    char ch = ' ';

    public Color getColor() { return c; }

    public boolean isOn() { return c != null; }

    public void turnOn() { if (!this.isOn()) c = Color.black; }

    public void turnOff() { c = null; }

    public void setColor(Color clr) { c = clr; }

    public void mark() { marked = true; }

    public void unmark() { marked = false; }

    public boolean isMarked() { return marked; }

    public char getChar() { return ch; }

    public void setChar(char c) { ch = c; }

    public String toString() { return "[" + ch + ", " + c + "]"; }
    }


    ///////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////
    // Do not modify code above this line. //
    ///////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////

    class Board extends BaseBoard
    {
    // Do not modify this method
    public Board(int c, int r)
    {
    super(c, r);
    }

    // Mouse must already be enabled (invoke enableMouse()).
    // Method is invoked automatically if the user clicks on a Board object
    // cell. The x and y coordinates of the clicked cell can be obtained
    // via the x and y parameters.
    public void mouseClicked(int x, int y)
    {

    }

    // Mouse must already be enabled (invoke enableMouse()).
    // Method is invoked automatically if the coordinate of the cell
    // where the mouse dragging action was initiated differs from the
    // coordinate of the destination cell.
    public void mouseDragged(int xStart, int yStart, int xEnd, int yEnd)
    {

    }

    // Mouse must already be enabled (invoke enableMouse()).
    // Method is invoked automatically when the mouse pointer moves
    // to a cell other than the current cell. The coordinate of the
    // cell where the mouse moved to can be obtained via the x and y
    // parameters
    public void mouseAt(int x, int y)
    {

    }

    }

    class Crossword extends BaseBoard
    {
    final static int ROWS = 20, COLS = 20;
    final static String[] words = {"fly", "plane", "plight", "rebel", "free", "sock", "blue", "data", "russia",
    "clan", "clam", "cram", "drastic", "be", "believe", "think", "aspire", "conspire", "under",
    "drop", "lift", "bench", "flock", "tempt", "preempt", "delete", "dream", "restore", "cajole",
    "reveal", "rest", "last", "recruit", "slight", "mull", "estonia", "repeat", "engage", "birth",
    "humid", "clue", "paradox", "river", "mountain", "sea", "ocean", "wave", "wind", "fire", "hike",
    "water", "nest", "kneel", "cover", "truest", "button", "CSC153", "rouse", "north", "star",
    "java", "coffee", "solitude", "extravert", "gather", "rescue", "rush", "plea", "prune", "plight",
    "pillow", "acidic", "cheese", "dune", "balloon", "clone", "dupe", "create", "crisis", "cruise",
    "bluff", "bloom", "bestow", "verbose", "enough", "crude", "clever", "calm", "current", "courant",
    "deflect", "perfect", "play", "march", "rose", "scent", "aroma", "spice"};

    Random rg = new Random();

    public Crossword()
    {
    super(COLS, ROWS);
    writeStrings();
    fillRandChars();
    }

    // return set of words to be placed on the board. Set size randomly chosen between 7 and 12
    private Set<String> chooseWords()
    {
    // select between 10 and 15 words
    int wordCount = rg.nextInt(6) + 10;
    Set<String> s = new HashSet<String>();

    while (s.size() < wordCount)
    {
    int i = rg.nextInt(words.length);
    if (!s.contains(words[i]))
    s.add(words[i]);
    }
    return s;
    }

    // fill the empty slots with random charaters
    private void fillRandChars()
    {
    for (int i = 0; i < ROWS; i++)
    {
    for (int j = 0; j < COLS; j++)
    {
    if (this.getChar(j, i) == ' ')
    this.setChar(j, i, (char)('a' + rg.nextInt(26)));
    }
    }
    }

    // fill the board with the randomly selected words
    private void writeStrings()
    {
    Set<String> s = chooseWords();

    Iterator it = s.iterator();
    while (it.hasNext())
    {
    String word = (String) it.next();

    int direction = rg.nextInt(3);

    if (direction == 0)
    fitVertical(word);
    else if (direction == 1)
    fitHorizontal(word);
    else
    fitDiagonal(word);
    }
    }

    private void fitVertical(String word)
    {
    int startX, startY;
    do {
    startX = rg.nextInt(ROWS);
    startY = rg.nextInt(COLS - word.length());
    } while (!doesItFitVert(startX, startY, word.length()));

    for (int i = 0; i < word.length(); i++)
    {
    char ch = word.charAt(i);
    setChar(startX, startY + i, ch);
    }
    }

    private boolean doesItFitVert(int startX, int startY, int len)
    {
    for (int i = 0; i < len; i++)
    {
    if (this.getChar(startX, startY+i) != ' ')
    return false;
    }
    return true;
    }

    private void fitHorizontal(String word)
    {
    int startX, startY;
    do {
    startX = rg.nextInt(ROWS - word.length());
    startY = rg.nextInt(COLS);
    } while (!doesItFitHoriz(startX, startY, word.length()));

    for (int i = 0; i < word.length(); i++)
    {
    char ch = word.charAt(i);
    setChar(startX+i, startY, ch);
    }
    }

    private boolean doesItFitHoriz(int startX, int startY, int len)
    {
    for (int i = 0; i < len; i++)
    {
    if (this.getChar(startX+i, startY) != ' ')
    return false;
    }
    return true;
    }

    private void fitDiagonal(String word)
    {
    int startX, startY;
    do {
    startX = rg.nextInt(ROWS - word.length());
    startY = rg.nextInt(COLS - word.length());
    } while (!doesItFitDiag(startX, startY, word.length()));

    for (int i = 0; i < word.length(); i++)
    {
    char ch = word.charAt(i);
    setChar(startX+i, startY+i, ch);
    }
    }

    private boolean doesItFitDiag(int startX, int startY, int len)
    {
    for (int i = 0; i < len; i++)
    {
    if (this.getChar(startX+i, startY+i) != ' ')
    return false;
    }
    return true;
    }

    public void highlightVertical(int row, int col, int cells)
    {
    Color c = new Color(rg.nextInt(50) + 100, rg.nextInt(50) + 180, rg.nextInt(50) + 180);
    for (int i = 0; i < cells; i++)
    this.fillCell(col, row+i, c);
    }

    public void highlightHorizontal(int row, int col, int cells)
    {
    Color c = new Color(rg.nextInt(50) + 180, rg.nextInt(50) + 100, rg.nextInt(50) + 180);
    for (int i = 0; i < cells; i++)
    this.fillCell(col+i, row, c);
    }

    public char[][] getCharArray() { return getChars2D(); }

    public void highlightDiagonal(int row, int col, int cells)
    {
    Color c = new Color(rg.nextInt(50) + 180, rg.nextInt(50) + 180, rg.nextInt(50) + 100);
    for (int i = 0; i < cells; i++)
    this.fillCell(col+i, row+i, c);
    }

    public static String[] getWords()
    {
    return words;
    }
    }


    The Assignment

    Programming II (CSC153) Fall 2009
    Project 2
    Due: Thursday, October 28th at 11:59pm.
    Word Puzzle
    For this project, you are to implement a solver for a crossword puzzle-like game. You are provided with a 20 by
    20 grid with a letter in each cell, and a list of words. You are asked to write a program that will solve the puzzle,
    that is to find word matches between the list and the grid. The grid is configured such that not all the words
    from the list can be matched, and the letter placement on the grid changes with every run of your program. A
    word can be matched on the board either vertically (top to bottom), horizontally (left to right), or diagonally (left
    to right and top to bottom). As a constraint to matching words, no two words can share the same cell on the
    grid. To help you develop your program, you are provided with a Java class called Crossword implemented in
    the file BaseBoard.java. This class allows you to easily create a graphical grid and place characters in its
    cells, obtain the list of words to match, and highlight the matched words on the grid. Your challenge is then to
    carry out the matching using various string operations and graphically display your results on the grid. Your
    program should also save the solution to the puzzle in a file.
    Before you can work with Crossword objects, you must download the BaseBoard.java file and place it in
    your project folder. If you are using NetBeans or Eclipse, add the file to your project and compile it (it should
    compile error free). To develop your program, create a class (or classes) in a different file than
    BaseBoard.java.
    Creating a grid object is a simple matter of invoking its default constructor:
    Crossword grid = new Crossword();
    You will notice that this creates a 20 by 20 grid with a letter placed in each cell. To obtain the list of possible
    words that can be matched, the static method getWords can be used. This returns the words as an array of
    String objects:
    String[] words = Crossword.getWords();
    The character contents of the grid can retrieved as a two-dimensional array of characters using the instance
    method getCharArray:
    char[][] letters = grid.getCharArray();
    Your program will need to find sequences of letters within this array that match words from the list obtained
    from the getWords method. Matched words are then highlighted on the grid using one of three methods,
    depending on whether the match was horizontal, vertical or diagonal:
    highlightVertical(row: int, col: int, cells: int): void
    highlightHorizontal(row: int, col: int, cells: int): void
    highlightDiagonal(row: int, col: int, cells: int): void
    For all three methods, the parameters row and col represent the starting position of the word to be highlighted
    (first row and column is 0), and the last parameter represents the number of cells to be highlighted. As an
    example, the highlight the second row of the Crossword object grid, the following statement can be used:
    grid.highlightHorizontal(1, 0, 20); // start at position (1,0) and highlight 20 cells
    Your program should also save the solution to a puzzle in an output file called solution.txt. This file will contain
    a listing of the grid characters, followed by the words that were successfully matched. For each match, the
    output file should have an entry that contains the matched word, its starting position (row number followed by
    column number) and whether the match was horizontal, vertical or diagonal. What follows is a sample run with
    the graphical output and the corresponding output file that will be generated by the program. Follow the file
    format below in generating your output file. Note that since the Crossword object will generate a different grid configuration for every program run, your program is not likely to produce an identical output.


    HERE IS MY CODE

    public class project2

    {
    public static void main(String args[])

    {
    int count = 0;
    Crossword grid = new Crossword(); //creates the default 20/20 board
    String[] words = Crossword.getWords();
    char[][] letters = grid.getCharArray();
    String s = new String(letters[0]); // converts a array to a string



    if (s.indexOf() == words.indexOf()) **error here**

    // grid.highlightHorizontal(1, 0, 20);


    }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Java Code:
    if (s.indexOf() == words.indexOf())
    words is a String array and not a String!
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    djester55 is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    i tried using a loop to check each word like this


    while (int i = 0, i < words.length i++)
    {
    if (s.indexOf() == words.indexOf(i))
    {
    grid.highlightHorizontal(1, 0, 1);

    }

    }
    for some reason this still doesn't work. Any other ideas?

Similar Threads

  1. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 AM
  2. Problem Comparing Strings (its not what you think)
    By hilather in forum New To Java
    Replies: 7
    Last Post: 11-19-2008, 06:43 PM
  3. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 09:03 AM
  4. JSTL -- Comparing two strings for equality
    By trinkets in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-12-2008, 04:39 PM
  5. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •