This is my car class - users create an object of this class and set is speed, acceleration, and handling. The rest of the methods are for my own use when I am running a simulation.
public class Car implements Vehicle
{
// field(s)
public int speed = 0;
public int cSpeed = 0;
public int handling = 0;
public int acceleration = 0;
// helper fields
private int points = 10;
private int maxSpeed = 0;
private int distanceTraveled; // distance from the end, look at there methods how they turn
protected int xPos;
protected int yPos;
protected int xSize; // do we need these??
protected int ySize; /// ???
protected int xDirection;
protected int yDirection;
boolean nearOpponent, nearBoundary, nearTurn; // add getters and setters
/****** Setter methods *******/
/**
* Sets the speed of the vehicle.
* @param newSpeed - the speed you want to travle at.
*
*/
public void setSpeed(int newSpeed)
{
points -= newSpeed;
if(points >= 0)
maxSpeed = newSpeed * 10;
else
System.out.println("You don't have any more points to spend!");
}
/**
* Sets the acceleration for the Vehicle object.
* @param newAcceleration - the handling of the vehicle in the range of 0-10.
*
*/
public void setAccel(int newAcceleration)
{
points -= newAcceleration;
if(points >= 0)
acceleration = newAcceleration;
else
System.out.println("You don't have any more points to spend!");
}
/**
* Sets the handling for the Vehicle object.
* @param newHandling - the handling of the vehicle in the range of 0-10.
*
*/
public void setHandl(int newHandling)
{
points -= newHandling;
if(points >= 0)
handling = newHandling;
else
System.out.println("You don't have any more points to spend!");
}
/**
* Sets the x position for the vehicle object.
* @param newX - the new x position for your vehicle object.
*
*/
public void setXPosition(int newX)
{
xPos = newX;
}
/**
* Sets the y position for the vehicle object.
* @param newY - the new y position for your vehicle object.
*
*/
public void setYPosition(int newY)
{
yPos = newY;
}
/**
* Sets the x direction for the vehicle object.
* @param newXDirection - the new x direction for your vehicle object.
*
*/
public void setXDirection(int newXDirection)
{
xDirection = newXDirection;
}
/**
* Sets the y direction for the vehicle object.
* @param newYDirection - the new y direction for your vehicle object.
*
*/
public void setYDirection(int newYDirection)
{
yDirection = newYDirection;
}
/****** Getter methods *******/
/**
* Returns the currently set speed of the Vehicle
* @return maxSpeed the top speed of the specified Vehicle object
*
*/
public int getTopSpeed()
{
return maxSpeed;
}
/**
* Returns the currently set speed of the Vehicle
* @return cSpeed the current speed of the specified Vehicle object
*
*/
public int getCurrentSpeed()
{
return cSpeed;
}
/**
* Returns the current handling rating.
* @return acceleration the acceleration rating of the Vehicle object.
*
*/
public int getAccel()
{
return acceleration;
}
/**
* Returns the current handling rating.
* @return handling the handling rating of the Vehicle object.
*
*/
public int getHandl()
{
return handling;
}
/**
* Returns the current x position of the Vehicle.
* @return xPos the current x position of the Vehicle.
*
*/
public int getXPosition()
{
return xPos;
}
/**
* Returns the current y position of the Vehicle.
* @return yPos the current y position of the Vehicle.
*
*/
public int getYPosition()
{
return yPos;
}
/**
* Returns the current x direction of the Vehicle.
* @return xDirection the current x direction of the Vehicle.
*
*/
public int getXDirection()
{
return xDirection;
}
/**
* Returns the current y direction of the Vehicle.
* @return yDirection the current y direction of the Vehicle.
*
*/
public int getYDirection()
{
return yDirection;
}
/****** helper methods *******/
// Decreases the speed of the Vehicle, not for user use.
// Mehtod to be used in simulation
public void applyBrake(int decrement)
{
if (speed > 0)
speed -= decrement;
else
System.out.println("You're already stopped!");
}
// Increases speed of the Vehicle based on its acceleration rating.
// not for user use, to be used for simulation
public void speedUp()
{
// you should only be able to speed up
// as fast as your acceleration allows
// maybe speedUp <= acceleration, not just arbitrary increment?
if (cSpeed < maxSpeed)
cSpeed += acceleration;
else
System.out.println("Your car can't go any faster!");
}
public void turn()
{
// turn method?
// based on handling, the better your handling,
// the less you have to slow down to make the turn?
}
/**
* Update our postion based on our own direction. If the
* update causes us to move outside the bounds given as
* arguments, then "bounce" back in and change our direction
* to the opposite. Our most recent change was to adjust the
* right and top edge bounces to account for the size of the
* shape.
**/
public void move(int minX, int minY, int maxX, int maxY)
{
xPos += xDirection;
yPos += yDirection;
if (xPos < minX) // left window edge
{
xPos = minX;
xDirection *= -1;
}
else if (xPos+xSize > maxX) // right window edge
{
xPos = maxX-xSize;
xDirection *= -1;
}
if (yPos-ySize < minY) // top window edge
{
yPos = minY+ySize;
yDirection *= -1;
}
else if (yPos > maxY) // bottom window edge
{
yPos = maxY;
yDirection *= -1;
}
} // end move
}
This is my interface - I need the user to program each of these methods and be able to access how they handle each of these situations in my simulation.
public interface Player
{
void start();
void nearingLeftBoundary();
void nearingRightBoundary();
void nearingOponent();
void startingToDriftLeft();
void startingToDriftRight();
String myName();
}
Here is an example of an implementation a user might write: (notice they create a car object in here as well and I also need to be able to access what they set there speed and stuff to)
public class Test implements Player
{
Car tom = new Car();
public void start()
{
tom.setSpeed(4);
tom.setAccel(3);
tom.setHandl(3);
}
public void nearingLeftBoundary()
{
tom.speedUp(); // how do we access object?
}
public void nearingRightBoundary()
{
tom.speedUp(); // how do we access object?
}
public void nearingOponent()
{
tom.speedUp();
}
public void startingToDriftLeft()
{
tom.applyBrake(5);
}
public void startingToDriftRight()
{
tom.applyBrake(5);
}
public String myName()
{
return "tom";
}
}
Finally here is my (in progress)simulation class, I dynamically load their implementation of my interface and I am able to access anything i need from the methods I laid out in the interface but I also need to be able to access the car object they create. I have no idea how to do it.
public class Simulation
{
public static void main(String[] args)
{
double timer = 0;
double distance = 0;
int[] place = new int[10];
Class<Player> player1class;
Player player1;
ClassLoader classLoader = Simulation.class.getClassLoader();
try
{
// the typecast to Class<Player> generates a warning but works ok
// - there do seem to be ways of getting rid of the warning, see
// http://stackoverflow.com/questions/4865153/loading-a-class-from-a-string
// http://www.velocityreviews.com/forums/t143120-generics-and-classloaders.html
player1class = (Class<Player>) classLoader.loadClass("Test");
player1 = player1class.newInstance();
}
catch (Exception e)
{
// need more than ClassNotFoundException for newInstance()
System.err.println("Error: cannot load and instantiate player class: " + e);
e.printStackTrace();
return;
}
System.out.println("Player 1's name is " + player1.myName()); // this works
System.out.println("Player 1's car speed is " + player1.getTopSpeed() ); // says it cant find symbol, because that method isnt in their class,
// I get that, but I do not see how to access the data i need from there created object
// infinite loop for simulation
//for (; ;)/>
//{
// timer++;
//end when all cars cmplete race
// as cars finish print time and place to terminal
// update car postions, based on handling, speed, accel. . .
//}
// coordinates checker, check coordinates of each object - check collisions
// handle (method)response for collisions, turns etc
// print race stats, collisions, car speeds, positions
}
} // end Simulation

New Topic/Question
Reply




MultiQuote


|