Carの親クラスから1つのパラメーターを取得し、それを配列()に追加しようとしていますcarsParked
が、どうすればよいですか?
親クラス
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;
}
}
チャイルドクラス
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";
}
}
最後の質問方法
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;
}
これまでのところ、パラメータに「赤」を入力すると、すべての車が赤で表示され、対応する情報が表示されますが、機能しないようです〜_〜。