In this program I am asked to create a class called Robot and am supposed to hav
ID: 3529653 • Letter: I
Question
In this program I am asked to create a class called Robot and am supposed to have it move on a grid type system, either x or y. I have a tester that asks the user for the name of the robot then ask the direction(type x or y) then ask for direction and then ask direction again until user wants to quit and that is when they type "q" for quit the program and to return the results. The results are the name of the robot followed by where they are on the coordinates (x,y) and then the total distance they traveled. My problem on the total direction where I am getting really weird numbers and I am not quite sure where it is going wrong at. The total distance traveled should also include negative numbers to count the same as positive so I used Math.abs(). Some correct answers for the output if the user would enter this would be as followed: x = 5 and y = -10 and x = -2 and then q to quit. The coordinate system would be at (3, 10) and the total distanced traveled would be 17. My answers have been really weird on the total distance traveled so I am sure it is something in the methods I created. I should also note that I want to use the method system and not have an equation in the tester. The is the tester( http://codepad.org/uk5ihV2P ) and these are the methods/constructors/getters ( http://codepad.org/vvSFpw0A )Explanation / Answer
Hey I have Corrected Your Code.It is now working as you want..So Check it out..
Code Below
Class Prog3
import java.util.Scanner;
public class Prog3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Robots Name: ");
String name = in.nextLine();
Robot theRobot = new Robot(name);
System.out.print("Direction of move(x/y/q): ");
String direction = in.next();
int distance = 0;
int totalXdistance=0;
int totalYdistance=0;
while(direction.equals("x") || direction.equals("y"))
{
if(direction.equals("x"))
{
System.out.print("Distance: ");
distance = in.nextInt();
theRobot.moveX(distance);
theRobot.totalDistanceX(distance);
//theRobot.totalDistanceX(distance);
}
if(direction.equals("y"))
{
System.out.print("Distance: ");
distance = in.nextInt();
theRobot.moveY(distance);
theRobot.totalDistanceY(distance);
//theRobot.totalDistance(distance);
}
System.out.print("Direction of move(x/y/q): ");
direction = in.next();
}
distance =theRobot.getXdistance() + theRobot.getYdistance();
theRobot.totalDistance(distance);
System.out.println(theRobot.getName() + " is now at position ( " + theRobot.getXPosition() +
" , " + theRobot.getYPosition() + " ) and traveled a total of " +
theRobot.getDistanceTraveled() + " units");
}
}
Robot Class
public class Robot
{
private String name;
private int xPosition;
private int yPosition;
private int distance;
private int xdistance;
private int ydistance;
public Robot(String name)
{
super();
this.name = name;
this.xPosition = 0;
this.yPosition = 0;
this.distance = 0;
this.xdistance=0;
this.ydistance=0;
}
public String getName()
{
return name;
}
public int getXPosition()
{
return xPosition;
}
public int getYPosition()
{
return yPosition;
}
public int getDistanceTraveled()
{
return distance;
}
public int getXdistance() {
return xdistance;
}
public void setXdistance(int xdistance) {
this.xdistance = xdistance;
}
public int getYdistance() {
return ydistance;
}
public void setYdistance(int ydistance) {
this.ydistance = ydistance;
}
public void moveX(int xPosition)
{
this.xPosition += xPosition;
}
public void moveY(int yPosition)
{
this.yPosition += yPosition;
}
public void totalDistanceX(int distance)
{
this.xdistance += Math.abs(distance);
}
public void totalDistanceY(int distance)
{
this.ydistance += Math.abs(distance);
}
public void totalDistance(int distance)
{
this.distance = distance;
}
}
Sample OutPut
Robots Name: r1
Direction of move(x/y/q): x
Distance: 5
Direction of move(x/y/q): y
Distance: -10
Direction of move(x/y/q): x
Distance: -2
Direction of move(x/y/q): q
r1 is now at position ( 3 , -10 ) and traveled a total of 17 units
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.