NEED HELP DEBUGGING /* * AP(r) Computer Science GridWorld Case Study: * Copyrigh
ID: 3694603 • Letter: N
Question
NEED HELP DEBUGGING
/*
* AP(r) Computer Science GridWorld Case Study:
* Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* @author Cay Horstmann
* @modified 2011 Scott Anderson
*/
package info.gridworld.actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import info.gridworld.actor.*;
import java.awt.Color;
import java.util.ArrayList;
/**
* A <code>Bug</code> is an actor that can move and turn. It drops flowers as
* it moves. <br />
* The implementation of this class is testable on the AP CS A and AB exams.
*/
public class RandomAgent extends Actor
{
boolean VisitedMap[][];
int numRows = 0;
int numCols = 0;
/**
* Constructs a red Agent.
*/
public RandomAgent(int NumRows, int NumCols)
{
setColor(Color.RED);
numRows = NumRows;
numCols = NumCols;
Grid<Actor> gr = getGrid();
VisitedMap = new boolean[NumRows][NumCols];
for (int r=0; r<NumRows; r++)
{
for (int c=0; c<NumCols; c++)
{
VisitedMap[r][c] = false;
}
}
}
/**
* Moves if it can move.
*/
public void act()
{
Grid<Actor> gr = getGrid();
VisitedMap[getLocation().getRow()][getLocation().getCol()] = true;
// if ASK(KB, Glitter)
if (gr.getGlitter(getLocation()))
{
gr.grabGold(getLocation());
moveTo(new Location(gr.getNumRows()-1,0));
gr.climb(new Location(gr.getNumRows()-1,0));
removeSelfFromGrid();
}
else
{
boolean moved = false;
// find unvisited location
//get adjacent squares
ArrayList<Location> adjLocs = gr.getValidTrueAdjacentLocations(getLocation());
//which are OK?
//if there is an OK, move there
for (Location choice : adjLocs)
{
System.out.println(index);
index++;
StenchMap[getLocation().getWroc()][getLocation().getCol()] = gr.getStench(getLocation());
BreezeMap[getLocation().getWroc()][getLocation().getCol()] = gr.getBreeze(getLocation());
//visted previously?
if (VisitedMap[choice.getRow()][choice.getCol()] ==false)
{
System.out.println("VistedMapFalse");
if (stenchMap[getLocation().getRow()][getLocation().getCol()])
{ System.out.printLn("StenchMapTrue");
Location loc = selectMoveLoation(adkLocs);
moveTo(loc);
moved = true;
}
else if (breezeMap[getLocation().getRow()][getLocation().get()])
{
System.out.println("BreezeMapTrue");
Location loc = selectMoveLocation(adjLocs);
moveTo(loc);
Moved = true;
}
else
{
moveTo(choice);
moved = true;
}
if (moved)
break;
}
}
else
{
System.out.printLn("visitedMapTrue");
}
}
public void report()
{
Grid<Actor> gr = getGrid();
System.out.println();
System.out.println();
System.out.println("AT ("+getLocation().getRow()+","+getLocation().getCol()+")");
System.out.println("Stench="+gr.getStench(getLocation()));
System.out.println("gold"+gr.getGlitter(getLocation()));
arrayList<Location> tadjLocs = gr.getvalidTrueAdjectLocation;
for (Location poss : tadjLocs)
{
System.out.println(interger.toString (poss.getRow()) + "," + ",";
}
public Location selectMoveLocation (ArrayList<Location> locs)
{
int n = locs.size();
if (n == 0)
returngetLocation();
int r = (int) (math.random() *n);
return locs.get(r);
}
}
I have 9 errors listed below
agent (1).java:123: 'else' without 'if'
else
^
agent (1).java:131: illegal start of expression
public void report()
^
agent (1).java:131: illegal start of expression
public void report()
^
agent (1).java:131: ';' expected
public void report()
^
agent (1).java:144: ')' expected
System.out.println(interger.toString (poss.getRow()) + "," + ",";
^
agent (1).java:148: illegal start of expression
public Location selectMoveLocation (ArrayList<Location> locs)
^
agent (1).java:148: ';' expected
public Location selectMoveLocation (ArrayList<Location> locs)
^
agent (1).java:148: ';' expected
public Location selectMoveLocation (ArrayList<Location> locs)
^
agent (1).java:161: reached end of file while parsing
}
Explanation / Answer
1. First error, 'else' without 'if'
i.e in the line there is if statement followed by break. In that case it shows an error.
2. illegal start of expression
public void report()
Before start of new method or new class, first check whether the previous unrelated methods or classes are closed. Closed in the sense count the curly braces opened and closed.
3. System.out.println(interger.toString (poss.getRow()) + "," + ",";
It is wrong. Because it is not ended with circular brace.
Correct statement is System.out.println(interger.toString (poss.getRow()) + "," + ",");
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.