package uk.ac.reading.cs2ja16.rdrohan;
import java.util.Scanner;
public class AnEntity {
protected String species; // Species Name
protected char symbol; // Symbol for species is the species initial
protected int xPosition; // horizontal and vertical positions
protected int yPosition;
protected int energy;
public final int uniqueID; // UniqueID for each entity, increments from 0.
private static int counter = 0;
AnEntity() // Class constructor that assigns appropriate values to entity
{
species = "Dog";
symbol = 'D';
xPosition = 0;
yPosition = 0;
energy = 100;
this.uniqueID = counter++;
}
AnEntity(String speciesInput, int xPositionInput, int yPositionInput,
int energyInput) // Constructor with user inputed attributes.
{
species = speciesInput;
xPosition = xPositionInput;
yPosition = yPositionInput;
energy = energyInput;
symbol = speciesInput.charAt(0); // First character of species string is
// used as symbol
this.uniqueID = counter++;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public char getSymbol() {
return symbol;
}
public void setSymbol(char symbol) {
this.symbol = symbol;
}
public int getxPosition() {
return xPosition;
}
public void setxPosition(int xPosition) {
this.xPosition = xPosition;
}
public int getyPosition() {
return yPosition;
}
public void setyPosition(int yPosition) {
this.yPosition = yPosition;
}
public int getEnergy() {
return energy;
}
public void setEnergy(int energy) {
this.energy = energy;
}
public String toString() { // Outputs a string detailing some attributes
// (Species name + x and y position)
String speciesAndPostitionString = ("The species: " + species
+ " currently has x position: " + xPosition
+ " and y position: " + yPosition);
return speciesAndPostitionString;
}
public String toText() // Output the attributes of the entity.
{
String attributesAsString = ("Species: " + species + "\t\tSymbol: "
+ symbol + "\nHorizontal Pos: " + xPosition
+ "\tVertical Pos: " + yPosition + "\nEnergy: " + energy
+ "\t\tUnique ID: " + uniqueID);
return attributesAsString;
}
public boolean isHere (int sx, int sy) {
return sx == xPosition && sy == yPosition; // is robot at x,y?
}
public void displayEntity(AnInterface r) {
/**
* display robot in the interface r
*/
r.showRobot(xPosition, yPosition); // just send details of robot to interface
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
AnEntity[] entityArray = new AnEntity[3];
for (int i = 0; i < 3; i++) {
System.out.print("Species name: ");
String speciesInput = s.nextLine(); // Input for species name
System.out.print("Horizontal Position: ");
int xPositionInput = Integer.parseInt(s.nextLine()); // Input for horizontal position
System.out.print("Vertical Position: ");
int yPositionInput = Integer.parseInt(s.nextLine()); // Input for vertical position
System.out.print("Energy: ");
int energyInput = Integer.parseInt(s.nextLine()); // Input for energy value
entityArray[i] = new AnEntity(speciesInput, xPositionInput,
yPositionInput, energyInput); // Creates object with the user inputed attributes
}
for (int i = 0; i < 3; i++) {
System.out.println(entityArray[i].toText() + "\n"); // Outputs the entities attributes
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.