gracie
04-28-2003, 09:35 AM
I have a problem now , i need to incorporate an object of a type from java API,
and i wanted to change the array to the array list thing... but it stuff up when i did it .. >< code below
import java.util.*;
public class SheepFinal {
int ysheep;
int xsheep;
int dogxPos = 4; // x position of dog
int dogyPos = 5; // y position of dog
private int Energy = 10; //amount of energy in sheep
double Length;
int L;
public int[] xsheepPos = new int [8]; // a list of x co-ordinates
public int[] ysheepPos = new int [8]; // a list of y co-ordinates
public void sheepWalk() {
System.out.println("Sheep is walking");
Energy = Energy - 2;
}
public void sheepEat() {
Energy = Energy + 3;
System.out.println("Sheep is eating grass");
System.out.println("The sheep is at position" + "" + xsheepPos[xsheep] + "," + ysheepPos[ysheep]);
}
public void sheepDrink() {
Energy = Energy + 2;
System.out.println("Sheep is drinking water");
System.out.println("The sheep is at position" + "" + xsheepPos[xsheep] + "," + ysheepPos[ysheep]);
}
// calculate the distance between the sheep and dog
public void sheepDistance() {
sheepCord();
Length = Math.sqrt(Math.pow((xsheepPos[xsheep] - dogxPos),2) + Math.pow((ysheepPos[ysheep] - dogyPos),2));
Length = Math.abs(Length); // make it a positive number
L = (int)Math.round(Length); // rounds off to the nearest whole number
}
// stores some numbers into array
public void sheepCord() {
for (int xsheep= 0; xsheep < xsheepPos.length; xsheep++) {
xsheepPos[xsheep] =(int)(Math.random()*8);
}
for (int ysheep = 0; ysheep < ysheepPos.length; ysheep++) {
ysheepPos[ysheep] =(int)(Math.random()*8);
}
System.out.println("The sheep is at position" + "" + xsheepPos[xsheep] + "," + ysheepPos[ysheep]);
}
public void sheepRun() {
System.out.println("Sheep is moving around the pasture to avoid dog");
Energy = Energy - 5;
sheepDistance();
}
// determine the energy level left in the sheep and action the sheep
// takes if energy is high or low
public void changeEnergy() {
Energy = Energy - 1;
if (Energy <= 5) {
sheepEat();
sheepDrink();
} else {
sheepWalk();
}
}
// shows how close the dog is to the sheep and the action the sheep take when dog is too close
public void sheepClose() {
sheepDistance();
if (L <= 4) {
sheepRun();
} else {
sheepWalk();
}
}
public static void main (String[] args) {
SheepFinal s1 = new SheepFinal();
s1.sheepWalk();
s1.changeEnergy();
s1.sheepClose();
s1.changeEnergy();
}
}
and i wanted to change the array to the array list thing... but it stuff up when i did it .. >< code below
import java.util.*;
public class SheepFinal {
int ysheep;
int xsheep;
int dogxPos = 4; // x position of dog
int dogyPos = 5; // y position of dog
private int Energy = 10; //amount of energy in sheep
double Length;
int L;
public int[] xsheepPos = new int [8]; // a list of x co-ordinates
public int[] ysheepPos = new int [8]; // a list of y co-ordinates
public void sheepWalk() {
System.out.println("Sheep is walking");
Energy = Energy - 2;
}
public void sheepEat() {
Energy = Energy + 3;
System.out.println("Sheep is eating grass");
System.out.println("The sheep is at position" + "" + xsheepPos[xsheep] + "," + ysheepPos[ysheep]);
}
public void sheepDrink() {
Energy = Energy + 2;
System.out.println("Sheep is drinking water");
System.out.println("The sheep is at position" + "" + xsheepPos[xsheep] + "," + ysheepPos[ysheep]);
}
// calculate the distance between the sheep and dog
public void sheepDistance() {
sheepCord();
Length = Math.sqrt(Math.pow((xsheepPos[xsheep] - dogxPos),2) + Math.pow((ysheepPos[ysheep] - dogyPos),2));
Length = Math.abs(Length); // make it a positive number
L = (int)Math.round(Length); // rounds off to the nearest whole number
}
// stores some numbers into array
public void sheepCord() {
for (int xsheep= 0; xsheep < xsheepPos.length; xsheep++) {
xsheepPos[xsheep] =(int)(Math.random()*8);
}
for (int ysheep = 0; ysheep < ysheepPos.length; ysheep++) {
ysheepPos[ysheep] =(int)(Math.random()*8);
}
System.out.println("The sheep is at position" + "" + xsheepPos[xsheep] + "," + ysheepPos[ysheep]);
}
public void sheepRun() {
System.out.println("Sheep is moving around the pasture to avoid dog");
Energy = Energy - 5;
sheepDistance();
}
// determine the energy level left in the sheep and action the sheep
// takes if energy is high or low
public void changeEnergy() {
Energy = Energy - 1;
if (Energy <= 5) {
sheepEat();
sheepDrink();
} else {
sheepWalk();
}
}
// shows how close the dog is to the sheep and the action the sheep take when dog is too close
public void sheepClose() {
sheepDistance();
if (L <= 4) {
sheepRun();
} else {
sheepWalk();
}
}
public static void main (String[] args) {
SheepFinal s1 = new SheepFinal();
s1.sheepWalk();
s1.changeEnergy();
s1.sheepClose();
s1.changeEnergy();
}
}
