Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this exercise, you will be building a simple text based animation in which yo

ID: 3638083 • Letter: I

Question

In this exercise, you will be building a simple text based animation in which you race two robots. Assume that the race tracks consist of 50 steps and the robots are named “A” and “B”. There are two race tracks each of which is an array of Strings of size 50. In each move, you generate two random numbers, one for direction (0 or 1) and one for number of places (0, 1, 2, or 3). If the direction is 0, then it means move backwards by the number of places. If the direction is 1, then it means move forwards by the number of places. Of course, the robots will move only if the move doesn’t take them out of the array bounds.

When you compile and run the following main:

//RobotRaceDemo.java
public class RobotRaceDemo
{
public static void main(String[] args)
{
RobotRace myRace = new RobotRace("A", "B");
myRace.play();
myRace.declareWinners();
}
}

here’s a sample run (only parts of the run shown):


Track1:
A-------------------------------------------------
Track2:
B-------------------------------------------------


Track1:
A-------------------------------------------------
Track2:
-B------------------------------------------------




Track1:
---A----------------------------------------------
Track2:
---B----------------------------------------------


Track1:
--A-----------------------------------------------
Track2:
---B----------------------------------------------


Track1:
---A----------------------------------------------
Track2:
------B-------------------------------------------

…. Etc.

Track1:
----------------------------------------------A---
Track2:
-----------------------------------------B--------


Track1:
-------------------------------------------------A
Track2:
---------------------------------------B----------


A wins


The program is modeled using two classes, Robot and RobotRace. RobotRace is aggregated from Robot. The following is the skeletal outline. Most of the code has been given. You need to understand the code, complete the missing methods, run the RaceDemo.java, and enjoy the Robot race.

//Robot.java
public class Robot
{

//attributes: name and a position
private String name;
private int xpos;
public Robot(String n)
{
//set the name to the given value
//set the xposition to 0

}
public int getX()
{
//get the xposition
}

public String getName()
{
//get the name
}
public void move()
{
//generate two random integers direction (0 or 1)
//and numPlaces (0,1,2 or 3).
//if the direction is 0, decrement xpos by numPlaces
//if it doesn’t go below 0
//if the direction is 1, increment xpos by numPlaces
//if it doesn’t go above 49.

}

}




//RobotRace.java

public class RobotRace
{
//attributes: two race tracks, two robots.
private String[] track1;
private String[] track2;
private Robot robot1;
private Robot robot2;

//constructor: creates the two robots with the given names
//places the robots in array position 0
//fills the remaining array positions with “-“
public RobotRace(String n1, String n2)
{
track1 = new String[50];
track2 = new String[50];
robot1 = new Robot(n1);
robot2 = new Robot(n2);
track1[0] = n1;
for(int i=1;i<50;i++)
track1[i]="-";
track2[0] = n2;
for(int i=1;i<50;i++)
track2[i] = "-";

}

//sets track1[x] to a given value
public void setTrack1(int x, String n)
{
track1[x] = n;
}

//resets track1[x] back to “-“
//this method is useful when the robot moves from its current position
public void resetTrack1(int x)
{
track1[x] = "-";
}

//sets track2[x] to a given value
public void setTrack2(int x, String n)
{
track2[x] = n;
}

//resets track1[x] back to “-“
//this method is useful when the robot moves from its current positio
public void resetTrack2(int x)
{
track2[x] = "-";
}




public void play()
{

//while neither robot has won
//{
//display
//move robot1
//set track1 for the new position
//reset track1 for the old position

//move robot2
//set track2 for the new position
//reset track2 for the old position

//}

}
public void declareWinners()
{
//print if robot1 has won or robot2 has won or if
//both have won.
}
public void display()
{
System.out.println("Track1: ");
for(int i=0;i<50;i++)
System.out.print(track1[i]);
System.out.println();

System.out.println("Track2: ");
for(int i=0;i<50;i++)
System.out.print(track2[i]);
System.out.println(" ");
}

}

Explanation / Answer

please rate - thanks

does not work awith play as specified. I left the code as a comment, and redid play, so the program would work as specified

message me if any problems

//RobotRaceDemo.java
public class RobotRaceDemo
{
public static void main(String[] args)
{
RobotRace myRace = new RobotRace("A", "B");
myRace.play();
myRace.declareWinners();
}
}

------------------------------------------

//RobotRace.java

public class RobotRace
{
//attributes: two race tracks, two robots.
private String[] track1;
private String[] track2;
private Robot robot1;
private Robot robot2;

//constructor: creates the two robots with the given names
//places the robots in array position 0
//fills the remaining array positions with “-“
public RobotRace(String n1, String n2)
{
track1 = new String[50];
track2 = new String[50];
robot1 = new Robot(n1);
robot2 = new Robot(n2);
track1[0] = n1;
for(int i=1;i<50;i++)
track1[i]="-";
track2[0] = n2;
for(int i=1;i<50;i++)
track2[i] = "-";

}

//sets track1[x] to a given value
public void setTrack1(int x, String n)
{
track1[x] = n;
}

//resets track1[x] back to “-“
//this method is useful when the robot moves from its current position
public void resetTrack1(int x)
{
track1[x] = "-";
}

//sets track2[x] to a given value
public void setTrack2(int x, String n)
{
track2[x] = n;
}

//resets track1[x] back to “-“
//this method is useful when the robot moves from its current positio
public void resetTrack2(int x)
{
track2[x] = "-";
}
/*
public void play()
{

//while neither robot has won
while(robot1.getX()!=49&&robot2.getX()!=49)
{
//{
//display
display();


//move robot1
robot1.move();
//set track1 for the new position
setTrack1(robot1.getX(),robot1.getName());
//reset track1 for the old position
resetTrack1(robot1.getX());

//move robot2
robot2.move();
//set track2 for the new position
setTrack2(robot2.getX(),robot2.getName());
//reset track2 for the old position
resetTrack2(robot2.getX());

//}

}
}

*/
public void play()
{
display();
//while neither robot has won
while(robot1.getX()!=49&&robot2.getX()!=49)
{
//{
//display
resetTrack1(robot1.getX());
resetTrack2(robot2.getX());

//move robot1
robot1.move();
//set track1 for the new position
setTrack1(robot1.getX(),robot1.getName());
//System.out.println(robot1.getX()+" "+robot1.getName());

//move robot2
robot2.move();
//set track2 for the new position
setTrack2(robot2.getX(),robot2.getName());
//reset track2 for the old position
//System.out.println(robot2.getX()+" "+robot2.getName());
display();
//reset track1 for the old position
//}

}
}

public void declareWinners()
{
//print if robot1 has won or robot2 has won or if
//both have won.
if(robot1.getX()==49)
     if(robot2.getX()==49)
           System.out.println("It's a tie");
        else
            System.out.println(robot1.getName()+" wins");
else if(robot2.getX()==49)
          System.out.println(robot2.getName()+" wins");

}
public void display()
{
System.out.println("Track1: ");
for(int i=0;i<50;i++)
System.out.print(track1[i]);
System.out.println();

System.out.println("Track2: ");
for(int i=0;i<50;i++)
System.out.print(track2[i]);
System.out.println(" ");
}

}

-----------------------------------

//Robot.java
public class Robot
{

//attributes: name and a position
private String name;
private int xpos;
public Robot(String n)
{
//set the name to the given value
name=n;
//set the xposition to 0
xpos=0;
}
public int getX()
{
//get the xposition
return xpos;
}

public String getName()
{
//get the name
return name;
}
public void move()
{int direction, numplaces;

//generate two random integers direction (0 or 1)
direction = ((int)(Math.random()*10)%2);

//and numPlaces (0,1,2 or 3).
numplaces = (int)(Math.random()*10)%4;

//if the direction is 0, decrement xpos by numPlaces
//if it doesn’t go below 0
if(direction==0)
     {if(xpos-numplaces>=0)
             xpos-=numplaces;
        }
//if the direction is 1, increment xpos by numPlaces
//if it doesn’t go above 49.
else
     if(xpos+numplaces<=49)
             xpos+=numplaces;
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote