I’m trying to take one parameter from the parent class of Car and add it to my array (carsParked), how can i do this?
Parent Class
public class Car
{
protected String regNo; //Car registration number
protected String owner; //Name of the owner
protected String carColor;
/** Creates a Car object
* @param rNo - registration number
* @param own - name of the owner
**/
public Car (String rNo, String own, String carColour)
{
regNo = rNo;
owner = own;
carColor = carColour;
}
/** @return The car registration number
**/
public String getRegNo()
{
return regNo;
}
/** @return A String representation of the car details
**/
public String getAsString()
{
return "Car: " + regNo + "\nColor: " + carColor;
}
public String getColor()
{
return carColor;
}
}
Child Class
public class Carpark extends Car
{
private String location; // Location of the Car Park
private int capacity; // Capacity of the Car Park - how many cars it can hold
private int carsIn; // Number of cars currently in the Car Park
private String[] carsParked;
/** Constructor for Carparks
* @param loc - the Location of the Carpark
* @param cap - the Capacity of the Carpark
*/
public Carpark (String locations, int room)
{
location = locations;
capacity = room;
}
/** Records entry of a car into the car park */
public void driveIn()
{
carsIn = carsIn + 1;
}
/** Records the departure of a car from the car park */
public void driveOut()
{
carsIn = carsIn - 1;
}
/** Returns a String representation of information about the carpark */
public String getAsString()
{
return location + "\nCapacity: " + capacity +
" Currently parked: " + carsIn +
"\n*************************\n";
}
}
Last Question Method
public String getCarsByColor (String carColour)
{
for (int num = 0; num < carsParked.length; num++) { if ( carColour.equals(carsParked[num]) ) { System.out.print (carsParked[num]);
}
}
return carColour;
}
I have this so far so that if “red” is put in the parameters, it would list all the cars with the color red and it’s corresponding information but does not seem to work ~_~.
First change
carsParkedto a list. So:becomes
Then in you constructor initialize it to an empty list by doing:
carsParked = new ArrayList();
Then in your drive in method, make it take a car parameter and pull the param you want:
Also you do not need to keep track of the number of cars this way. Since you could always do
carsParked.size()to find out.Now I would probably change that list to be
List<Car>instead of string and just dump the whole car in there. Sure you may only need one item right now, but who knows down the road, maybe you will need something else.EDIT:
Sure you could do it with an simple array. The issue with that is sizing. Say you initially create an array of size 5, when you go to add the 6 item you will need to create a new larger array, copy the original data, then add the new item. Just more work. Now if the idea is you have a carpark, and it can have X number of spots then you initilize your array to that size from the begining.
then in your
driveIn()method:now
driveOut()Looks nice doesn’t it. Well no it is not. Now the
driveInwill not work, since we have null spots scattered all over the place. How do we fix it:It could still be improved further. I would probably still change
String[] carsParkedtoCar[] carsParkedas to not throw away information.I would also change the
driveInanddriveOutmethods to return booleans to indicate if the successfully parked or un-parked a car.Final Edit:
Okay, if you want to keep track of what cars are parked in the car park and which spot they are in you need to know enough about each car to make it unique. In your case you may only need
regNo. So when you calldriveInordriveOutyou have to pass that information so we can store it at the appropriate index (parking spot) in the array. Otherwise all you will know is a car was parked somewhere, or that a car left. Not which spots are open.So in short the parameter
Car carin those two methods contain the information needed to uniquely identify each car that is being parked, or is leaving. Without it the car park instance would have no clue who is currently parked, or where they are parked.