Hello all, I am a beginner at Java and taking a class which is moving at a fast pace.
So I have this Animal class and Fox class that must access the AnimalColor class through an API. Originally the AnimalColor object in the Animal class was public(accessible by all) but I now must change that to Private and force the Fox and Animal class to access it through an API. I am new to Java and having trouble locating info on API's used in this format. Here is the code for Animal:
import java.util.Random;
public abstract class Animal{
/**
* myColor is protected so that subclasses can access them directly.
* Not very good programming practice (but helps to illustrate private)
* Better would be, like location, to have API methods to handle color.
*/
private Point location;
private AnimalColor myColor; //Changed from public to private
Animal
/**
* The constructor sets up the animal at a random location
*/
Animal(){
Random ranGen = new Random();
location = new Point(10 + ranGen.nextInt(380), 10 + ranGen.nextInt(360));
}
/**
* The API allows the animal's location to be altered. This uses the Point API.
*/
public void setLocationX(int x){
location.setX(x);
}
public void setLocationY(int y){
location.setY(y);
}
public int getLocationX(){
return location.getX();
}
public int getLocationY(){
return location.getY();
}
public AnimalColor getColor(){
return myColor;
}
/**
* Abstract method
*/
public abstract void move();
}
Here is the code for Fox:
public class Fox extends Animal{
/**
* All the constructor does is to set the color in which the fox should be
* displayed.
*/
Fox(){
super();
myColor = AnimalColor.ORANGE;
}
/**
* Right now, foxes don't know how to move. This is not an abstract method because
* it has a (null) body
*/
public void move(){
;
}
}
Finally the AnimalColor class which needs to be accessed by Fox and Animal through an API:
public enum AnimalColor {BLACK, WHITE, ORANGE};
I will be using this (API's) a great deal in this class and would love to be able to not only code it, but understand how to apply it. I know the reason for an API, but simply cannot apply it.
Thank you in advance for all your advice and help.