DIrections: For this part you will use the Bear you completed in Part 2 of Homew
ID: 639146 • Letter: D
Question
DIrections:
For this part you will use the Bear you completed in Part 2 of Homework 3. Copy the Bear_pt2 file from Homework 3 and save it in your Homework 4 folder as Bear_pt4.
In order to keep your Bear from dying, it needs to replenish its energy by eating. Make a Food class. The food class will need three attributes: x-location, y-location and the amount of food it holds. Again, build a constructor for your food class that will set the values of these attributes. It will also need methods to access the location of the food and to get and set the amount of food at a location.
This is my main code that i have so far:
import java.util.Random;
public class BearClassDriver3 {
public static void main(String[] args){
Bear_pt4 HungryBear = new Bear_pt4 ();
Food meat = new Food ();
while(HungryBear.energy>0)
{
Random rand = new Random(System.currentTimeMillis());
int bearAction;
bearAction = rand.nextInt(3);
if(bearAction==1)
{
HungryBear.rotateRight();
meat.getFood();
}
else if(bearAction==2)
{
HungryBear.rotateLeft();
meat.getFood();
}
else if(bearAction==3)
{
HungryBear.step();
meat.getFood();
}
System.out.println("Food placed at: ("+ meat.foodx+ ","+ meat.foody + ")");
System.out.println("Bear is at: ("+ HungryBear.x+ ","+ HungryBear.y+ ")");
}
}
}
THIS IS MY FOOD CODE:
import java.util.Random;
public class Food
{
public int foodx;
public int foody;
public int foodamount;
public Food(int foodx,int foody, int foodamount)
{
this.foodx=foodx;
this.foody=foody;
this.foodamount=foodamount;
}
public void setFood(Class Bear_pt4)
{
Random rand = new Random(System.currentTimeMillis()); // Seeds Random # with current time
int x=0;
int y=0;
int amount=0;
Food foodArray[] = new Food[5];
for (int i = 0; i < 5; i++)
{
x = rand.nextInt(20) - 10;
y = rand.nextInt(20) - 10;
amount = rand.nextInt(11) + 5;
foodArray[i] = new Food(x, y, amount);
}
foodx=x;
foody=y;
foodamount=amount;
System.out.println("Bear food now (" +foodx+ "," +foody+ ") foodamount is now :" +foodamount);
}
public int getFood(Class Bear_pt4)//pass the food and bear paramerters
{
Bear_pt4 Bear= new Bear_pt4 ();
if(foodx== Bear.x && foody==Bear.y)//if bear finds food it will eat it
{
Bear.energy= foodamount + Bear.energy;
}
System.out.println("Bear ate food at Position (" +foodx+ "," +foody+ ") Energy Level is now :" +Bear.energy);
return foodamount;
}
public void foodArray() //not sure if i need this food array or not
{
}
}
THIS IS THE ROTATING BEAR
public class Bear_pt4
{
public int x;
public int y;
public int HeadingDirection; /* HeadingDirection 1 means North , 2 means East, 3 means South, 4 means West*/
public int energy;
public Bear_pt4(int e, int setx, int sety, int Direction )
{
x=setx;
y=sety;
HeadingDirection=Direction;
energy=e;
}
public Bear_pt4()
{
this.x=x;
this.y=y;
this.HeadingDirection=HeadingDirection;
this.energy=energy;
}
public void rotateRight()
{
if(energy<2)
{
System.err.println("The Bear has insufficient energy!");
System.exit(1);
}
else if(energy>2);
{
energy = energy - 3;
}
if(HeadingDirection==1) /* Heading North */
HeadingDirection =2;
else if(HeadingDirection==2) /* Heading East */
HeadingDirection= 3;
else if(HeadingDirection==3) /* Heading South */
HeadingDirection=4;
else if(HeadingDirection==4) /* Heading West */
HeadingDirection=1;
}
public void rotateLeft()
{
if(energy<2)
{
System.err.println("The Bear has insufficient energy!");
System.exit(1);
}
else if(energy>2);
{
energy = energy - 3;
}
if(HeadingDirection==1) /* Heading North */
HeadingDirection =4;
else if(HeadingDirection==4) /* Heading West */
HeadingDirection= 3;
else if(HeadingDirection==3) /* Heading South */
HeadingDirection=2;
else if(HeadingDirection==2) /* Heading East */
HeadingDirection=1;
}
public void step()
{
if(energy<2)
{
System.err.println("The Bear has insufficient energy!");
System.exit(1);
}
else if(energy>2);
{
energy = energy - 3;
}
if(HeadingDirection==1) /* Adds one to the Y-axis */
y=y+1;
else if(HeadingDirection==4) /* Subtracts a step from X-axis */
x=x-1;
else if(HeadingDirection==2) /* Adds a step to X-axis */
x=x+1;
else if(HeadingDirection==3) /* Subtracts a step from Y-axis */
y=y-1;
}
}
Explanation / Answer
contact on timacs12@gmail.com
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.