Написание простого обработчика электронных таблиц для класса. Методы класса Location не проходят модульное тестирование. Любая помощь, почему? - PullRequest
1 голос
/ 07 марта 2019

Написание программы для класса, и у меня есть спецификация для этого: класс, который полностью реализует интерфейс Location и содержит конструктор, принимающий единственный параметр String (например, «D20»).Мой класс -

package textExcel;

//Update this file with your own code.

public class SpreadsheetLocation implements Location
{
String Loc;
private int col = Integer.parseInt(Loc.substring(1, 2));
private int row = Integer.parseInt(Loc.substring(0, 0));
@Override
public int getRow()
{
    System.out.println(row);
    // TODO Auto-generated method stub
    return row;
}

@Override
public int getCol()
{
    System.out.println(col);
    // TODO Auto-generated method stub
    return col;
}

public SpreadsheetLocation(String cellName)
{
    Loc = cellName;

}

}

, интерфейс -

 public interface Location
 {
// represents a location like B6, must be implemented by your SpreadsheetLocation class
int getRow(); // gets row of this location
int getCol(); // gets column of this location
 }

, а модульный тест -

public void testLongShortStringCell()
    {
        SpreadsheetLocation loc = new SpreadsheetLocation("L20");
        assertEquals("SpreadsheetLocation column", 11, loc.getCol());
        assertEquals("SpreadsheetLocation row", 19, loc.getRow());

        loc = new SpreadsheetLocation("D5");
        assertEquals("SpreadsheetLocation column", 3, loc.getCol());
        assertEquals("SpreadsheetLocation row", 4, loc.getRow());

        loc = new SpreadsheetLocation("A1");
        assertEquals("SpreadsheetLocation column", 0, loc.getCol());
        assertEquals("SpreadsheetLocation row", 0, loc.getRow());
    }

Что может быть причиной того, что мой код не прошел модульный тест?

1 Ответ

0 голосов
/ 07 марта 2019

Вы пытаетесь проанализировать Loc перед его инициализацией, в результате чего NullPointerException.

После перемещения синтаксического анализа в конструктор, вам все равно нужно будет его исправить. Столбец обозначается буквой, и вы не можете просто проанализировать его как int - вам нужно получить разницу между ним и A при упрощенном предположении, что он может иметь только один символ:

public SpreadsheetLocation(String cellName) {
    loc = cellName;
    row = Integer.parseInt(loc.substring(1)) - 1;
    col = loc.charAt(0) - 'A';
}
...