Skip to content

Instantly share code, notes, and snippets.

Created April 15, 2012 04:48
Show Gist options
  • Save anonymous/2390123 to your computer and use it in GitHub Desktop.
Save anonymous/2390123 to your computer and use it in GitHub Desktop.
car array
public class Car2
{
private int yearModel; // The car's year model
private String make; // The car's make
private int speed; // The car's speed
private String license; // The car's license
/**
* Constructor that initializes the model, make and speed of the car.
*
* @param newYearModel - Year Model of the car
* @param newMake - Make of the car
*/
public Car2(int newYearModel, String newMake, String newLicense)
{
yearModel = newYearModel;
make = newMake;
license = newLicense;
speed = 0;
}
/**
* Changes Year Model
* @param yearmodel - new year model
*/
public void setYearModel(int y )
{
yearModel = y;
}
/**
* Accesses Year model
* @return year model of the car
*/
public int getYearModel()
{
return yearModel;
}
/**
* Changes Make
* @param make - new year model
*/
public void setMake(String m)
{
make = m;
}
/**
* Accesses Make
* @return make of the car
*/
public String getMake()
{
return make;
}
/**
*
*
*/
public void setLicense(String l)
{
license = l;
}
public String getLicense()
{
return license;
}
/**
* Changes Speed
* @param speed - new speed
*/
public void setSpeed (int s)
{
speed = s;
}
/**
* Accesses Speed
* @return speed of the car
*/
public int getSpeed()
{
return speed;
}
/**
* Changes Speed
* @param accelerates speed in increments of 5
*/
public void accelerate()
{
speed += 5;
}
/**
* Changes Speed
* @param decelerate speed in increments of 5
*/
public void brake()
{
speed -= 5;
}
public String toString()
{ String str = "Speed of the car \n";
if (speed == 0)
str = "\nNo speed that is in the car \n";
for (int index=0; index < speed; index++)
str += nums[index];
return str;
}
public boolean equals(Car2)
{ boolean success = true;
if (speed < size)
{ nums[speed] = newData;
speed++;
}
else
success = false;
return success;
}
}
2 service class, 2nd service below.
public class CarCollection
{
private Car2 collection[] ;
private int maxSize;
private int currentSize;
public CarCollection()
{
collection = new Car2[maxSize];
}
public CarCollection(int maxSize)
{
maxSize = maximum;
currentSize = current;
}
public boolean add(int newYearModel, String newMake, String newLicense)
{ boolean success;
if (currentSize < maxSize)
{ Collection collection3 = new Car2(newYearModel, newMake, newLicense);
collection[currentSize] = car3;
currentSize++;
success = true;
}
else
success = false;
return success;
}
public boolean remove(String license)
{
boolean found = false;
for (int i=0; i<collection.length && !found; i++)
{ if (license[i] == datatoberemoved)
{ location = i;
found = true;
}
}
if (found)
{ for (string i=location; i<collection-1; i++)
{ license[i] = license[i+1];
}
collection--;
}
}
public int getCurrentSize()
{
return currentSize;
}
public int getMaxSize()
{
return maxSize();
}
public string toString();
{
String str = "Number of cars that are required";
if (collection.length == 0)
str = "\n no cars are required \n:";
for (int index=0; index < collection.length; index++)
str += nums[index];
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment