Search code examples
javaeclipseif-statementgetter-setter

Issue with if-statement with getters


My task is to develop an application that tests my Robot class. The test application will ask the user for the robots name. It will then instantiate a Robot object using the name. The application will then use loop to ask the user to enter direction (x or y) to move and the distance. The loop will terminate when the user enters a direction of q (for quit). Once the loop is done, the program will display the robots final position and the total distance traveled.

My main issue is trying to work with my Robot class and testing it in my Prog3 class when it comes to my if statement. Here is my Robot class:

public class Robot {
private String name;
private int xPosition;
private int yPosition;
private int totalDistance;

public Robot(String name, int xPosition, int yPosition, int totalDistance){
    super();
    this.name = name;
    this.xPosition = 0;
    this.yPosition = 0;
    this.totalDistance = totalDistance;
}

public String getName() {
    return name;
}

public int getxPosition() {
    return xPosition;
}

public int getyPosition() {
    return yPosition;
}

public int getTotalDistance() {
    return totalDistance;
}

public void moveX(int distance){
    xPosition += distance;
    totalDistance += Math.abs(distance);
}

public void moveY(int distance){
    yPosition += distance;
    totalDistance += Math.abs(distance);
}



}

Now here is my Prog3 class:

import java.util.Scanner;
public class Prog3 {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Robot robot = new Robot();

    //asks the user for robots name
    System.out.print("Enter robots name; ");
    Robot.name = in.nextLine();

    //asks the user to move the robot
    System.out.print("Direction of move (x/y/q): ");
    String move = in.nextLine();

    if( move.equals("x") ){
        System.out.print("Distance: ");

    }
    else if( move.equals("y") ){
        System.out.print("Distance: ");

    }
    else{


    }

}
}

As you can see, my Prog3 class is not complete due to the fact that i am completely stuck. Thank you so much for your time as it is greatly appreciated.


Solution

  • your constructor does not make sense, why would you need to input x and y values if they are always set to 0?

    I wrote you a new blank constructor, and added some setters methods

    public class Robot {
    private String name;
    private int xPosition;
    private int yPosition;
    private int totalDistance;
    
    public Robot(){
        name = null;
        xPosition = 0;
        yPosition = 0;
        totalDistance = 0;
    }
    
    public Robot(String name, int xPosition, int yPosition, int totalDistance){
        super();
        this.name = name;
        this.xPosition = 0;
        this.yPosition = 0;
        this.totalDistance = totalDistance;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName( String name){
        this.name = name;
    }
    
    public int getxPosition() {
        return xPosition;
    }
    
    public void setxPosition(int x) {
        xPosition = x;
    }
    
    public int getyPosition() {
        return yPosition;
    }
    
    public void setyPosition(int y){
        yPosition = y;
    }
    
    public int getTotalDistance() {
        return totalDistance;
    }
    
    
    public void moveX(int distance){
        xPosition += distance;
        totalDistance += Math.abs(distance);
    }
    
    public void moveY(int distance){
        yPosition += distance;
        totalDistance += Math.abs(distance);
    }
    

    then you need to do something with all of that in your main class

    import java.util.Scanner;
    public class Prog3 {
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Robot robo = new Robot();
    
        //asks the user for robots name
        System.out.print("Enter robots name: ");
        String myName = in.next();
        robo.setName(myName);
    
    
        while( true ){
        //asks the user to move the robot
        System.out.print("Direction of move (x/y/q): ");
        String direction = in.next();
        if(direction.equals("q"))
            break;
    
        System.out.println("Distance of move: ");
        int distance = in.nextInt();
    
    
        if (direction.equals("x")) {
            robo.moveX(distance);
            System.out.println("moved" + distance +"units along the x axis");
        }
        else if (direction.equals("y")) {
            robo.moveY(distance);
            System.out.println("moved " + distance +" units along the y axis");
        }
        else {
            System.out.println("error");
        }
        }
    
        System.out.println("moved " + robo.getTotalDistance() + " total distance");
    
    }
    }
    

    Let me know if you have any more questions.