QUESTION: I am very confused on how to start programing for this.....I am having
ID: 638854 • Letter: Q
Question
QUESTION: I am very confused on how to start programing for this.....I am having trouble setting up how the bear would rotate and how i would monitor the steps...YOU DONT HAVE TO WRITE THE WHOLE PROGRAM I JUST NEED SOMEWHERE TO START PLEASE!
Our bear lives in a two-dimensional (x-y) coordinate system and should be able to rotate left, rotate right and step. When it steps, it will move one whole unit in the direction it is facing.
Create a UML class diagram for your bear. It should have three behaviors: rotate left, rotate right and step. Your bear will also need some attributes, namely its x-y location and a heading.
Implement your Bear class in Java, saving it in file Bear_pt1. Do not include a main method in your Bear.
Implement a main class to test your Bear. Test your Bear by giving it a series of step/rotate commands and then print its final position. Note that your Bear
Explanation / Answer
public class Bear
{
int x,y;
char faceDirection; //it can be N,w,E,S
public Bear() //By default
{
x = 0;
y = 0;
faceDirection = 'E';
}
//this method will change the direction of Bear. If bear is facing North, then rotate left will make it direction as West.
public rotateLeft()
{
if(faceDirection == 'N')
faceDirection = 'W';
else if(faceDirection == 'W')
faceDirection = 'S';
else if(faceDirection == 'E')
faceDirection = 'N';
else if(faceDirection == 'S')
faceDirection = 'E';
}
public rotateRight()
{
if(faceDirection == 'N')
faceDirection = 'E';
else if(faceDirection == 'W')
faceDirection = 'N';
else if(faceDirection == 'E')
faceDirection = 'S';
else if(faceDirection == 'S')
faceDirection = 'W';
}
//increase 1 unit in the direction it is facing.
public step()
{
if(faceDirection == 'N')
y = y + 1;
else if(faceDirection == 'W')
x = x - 1;
else if(faceDirection == 'E')
x = x + 1;
else if(faceDirection == 'S')
y = y - 1;
}
public getLocation()
{
System.out.println("("x+","+y+") facing "+faceDirection);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.