0

I'm very new to Java and I'm confused on how to create a method that goes back to original position, going through each step and turn.

This is an assignment for school and essentially, I have to create a Bug class, which moves 1 unit of distance on a horizontal line, turns, reverses, and moves back to original position.

Here's the code I have so far:

public class Bug
{
    private int position;
    private String direction;

    public void initialPosition(int position ) {
        position=0;
    }

    public void initialDirection(String direction) {
        direction="right";
    }

    public void setPosition (int position) {
        this.position = position;
    }

    public int getPosition() {
        return position;
    }

    public void setDirection (String direction) {
        this.direction = direction;
    }

    public String getDirection() {
        return direction;
    }

    public void move() {
        position+=1;
    }   

    public void reverseDirection() {
        if (direction=="right") {
            direction="left";
        }
        else {
            direction="right";
        }
    }

    public String toString() {
        return "Position: " + position + " & Direction: " + direction;
    }
}

Any tips on code will be super appreciated as well. Thanks!

  • Some remarks: Within your setters, you hide your attributes with the parameters since they have the same name. --- in `initialPosition(...)` and `initialDirection(...)`, you assing a new value to the parameter. Even if you were accessing `this.position` (or `this.direction`, repsectively), the parameter is superfluous. --- your direction has no effect on your position (you always just add `1` to your current position). – Turing85 May 05 '18 at 19:52
  • Not entirely sure what you are asking, perhaps consider rephrasing your question into something more specific? Also, you shouldn't use `==` on string comparison, you should `direction.equals("right")` – Dmich May 05 '18 at 19:53
  • 1
    Some reference to what @Dmich mentioned about `==`: [Never compare `String`s (or really any reference-type in Java) with `==` unless you know exactly what you are doing and why you are doing it.](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Turing85 May 05 '18 at 19:54
  • Oh ok, I see now. Thanks! I'll make the change –  May 05 '18 at 20:06
  • @xmdoh initialPosition and initialDirection are incorrect. You are ignoring the passed parameter. For example, for the intialPosition you should write this.position = position; – Marco Altieri May 05 '18 at 20:06
  • @xmdoh unless the assignment requires to use a string for the direction, I would use an integer: +1 forward direction, -1 backward direction. In this way to reverse the direction you just need to change the sign. Something like direction = -direction; – Marco Altieri May 05 '18 at 20:07
  • @MarcoAltieri The assigment does require a string for direction –  May 05 '18 at 20:08

1 Answers1

0

In your move() function, get the current direction and then use an if statement so that

if (direction.equals("right")) {
    position+=1;
} 
else {
    position-=1;
}
Marco Altieri
  • 3,726
  • 2
  • 33
  • 47
RyanJ
  • 158
  • 10
  • [Never compare `String`s (or really any reference-type in Java) with `==` unless you know exactly what you are doing and why you are doing it.](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Turing85 May 05 '18 at 19:53
  • I know that, but considering that this is an assignment for school and their teacher is most likely familiar with their code, I decided to have my answer match their code. – RyanJ May 05 '18 at 19:56
  • Even if you do not fix this specific issue, you should at least mention it. – Turing85 May 05 '18 at 20:00
  • 1
    @RyanJ I would suggest at least add a note about String comparison. The sooner they know it, the better. – MC Emperor May 05 '18 at 20:01
  • Okay, thank you both for your advice, I'll make sure to offer a correction on proper practice if I spot it. – RyanJ May 05 '18 at 20:02
  • @xmdoh You are welcome, is there anything else I can help you with? – RyanJ May 05 '18 at 20:09
  • @RyanJ How would I call these methods in a driver class? Creating a bug object and calling it by bug1.move() doesn't seem to work. I'm getting error "The method move(int) in the type Bug is not applicable for the arguments " –  May 05 '18 at 20:12
  • @xmdoh So your move method must require an int value passed into it? If that's the case then the code for your move method should look something like `public void move(int distance) if (direction.equals("right")) { position+=distance; } else { position-=distance; }` – RyanJ May 05 '18 at 20:13
  • @RyanJ nevermind, it was referencing the wrong class so it required an int, but now it's referencing the right class and it doesn't require an int value - my bad. Thanks again! It's working now! –  May 05 '18 at 20:26
  • @xmdoh You're very welcome :) If this is all of your questions answered, please mark the question as answered. – RyanJ May 05 '18 at 20:29