This site requires JavaScript, please enable it in your browser!
Greenfoot back
rockon411
rockon411 wrote ...

2017/4/13

Maps and Interpreters

rockon411 rockon411

2017/4/13

#
I am new to maps and scanners so bear with me. I am supposed to create a object that is able to read and understand commands.
import sofia.micro.*;
import student.IOHelper;
import java.util.*;

public class Minnow extends Actor
{

    private Scanner input;
    private Map<String, Command> map;
    private Minnow raccoon;
    
    public Minnow)
    {
        this(IOHelper.createScannerForURL(
            "http://courses.cs.vt.edu/~cs1114/Fall2012/rocket-commands.txt"));
        map = new HashMap<String, Command>();
        ForwardCommand bob = new ForwardCommand(raccoon);
        map.put("forward", bob);
        LeftCommand bill = new LeftCommand(raccoon);
        map.put("left", bill);
        RightCommand joe = new RightCommand(raccoon);
        map.put("right", joe);
    }
    public Minnow(Scanner scanner)
    {
        input = scanner;
    }
    public void act()
    {
            map.get(input.next()).execute();
    }
}
Any help would be appreciated this is what I have so far.
public class ForwardCommand implements Command
{
    private Minnow minnow;

    public ForwardCommand(Minnow minnow)
    {
        minnow = new Minnow();
    }

    public void execute()
    {
        minnow.move(1);
    }
}
public class LeftCommand implements Command
{
    private Minnow minnow;

    public LeftCommand(Minnow minnow)
    {
        minnow = new Minnow();
    }

    public void execute()
    {
        minnow.turn(-90);
    }
public class RightCommand implements Command
{
    private Minnow minnow;

    public RightCommand(Minnow minnow)
    {
        minnow = new Minnow();
    }

    public void execute()
    {
        minnow.turn(90);
    }
public interface Command 
{

    void execute();
}
danpost danpost

2017/4/13

#
You certainly should not be creating a new minnow every time a command is created. These minnows being created within these method are not even added into the world. That should be an indication that something went wrong somewhere. Another thing is the 'racoon' field in the Minnow class. It is never assigned any Minnow object, so you are passing 'null' to the command constructors; and then the command methods are ignoring it and setting their 'minnow' fields to new (different) objects, anyway. Maybe you are trying to pass 'this' minnow to the commands; but, still, the classes should be saving the parameter reference. For example:
public RightCommand(Minnow minnow)
{
    this.minnow = minnow;
}
where in the Minnow class you would create the command with:
map.put("forward", new ForwardCommand(this));
Try to work with that and see what you can come up with.
rockon411 rockon411

2017/4/14

#
Thank you so much! I think that fixed the majority of the problems. I still have a null pointer exception in my act() method, however. I changed the code in an attempt to fix it, but I can't seem to get it.
public void act()
    {
        if (input.hasNext())
        {
            map.get(input.next()).execute();
        }
    }
danpost danpost

2017/4/14

#
rockon411 wrote...
Thank you so much! I think that fixed the majority of the problems. I still have a null pointer exception in my act() method, however. I changed the code in an attempt to fix it, but I can't seem to get it. < Code Omitted >
Are you creating a Scanner object and assigning it to the 'input' field? You can probably just do that in the constructor ( 'public Minnow()' ) of the Minnow class and remove the other constructor ( 'public Minnow(Scanner)' ).
rockon411 rockon411

2017/4/14

#
Thank you!
You need to login to post a reply.