2

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 ~_~.

3
  • 2
    I have no idea what you are trying to ask honestly. It doesn't make sense. You are trying to take a paramater from the parent class of car? Please be more clear? Commented Nov 8, 2012 at 4:58
  • Just What Exactly is a 'Car Park' --- A Parking Lot? If so why would a car lot have rooms and locations? If so then this 'Class' would not be a sub-class of Cars. Commented Nov 9, 2012 at 9:11
  • Instead of writing your own 'getAsString' method, look at overriding the 'toString' method that comes from Object. Commented Feb 13, 2013 at 14:45

2 Answers 2

7

You seem to have the wrong relationship here: a car park is not a car. I would recommend against using inheritance in either direction between these classes. And Carpark should probably just have an array or collection of cars.

Also note that the parameter carsIn isn't necessary - just get the length of the array of cars (or size() if it's a Collection).

Edit: Okay, ignoring the inheritance part, it seems like it makes sense to add cars when driveIn is called, and remove them when driveOut is called.

driveIn should probably take a Car as an argument, so the method can access the parameter you want to store (personally I would just store Car references, but fine). Since we're going to be adding and removing these parameters, it'll be much easier to use a List that can resize itself instead of an array, like ArrayList. For example:

private final List<String> carsRegNosParked = new ArrayList<String>();

public void driveIn(Car car) {
    carsRegNosParked.add(car.getRegNo());
}

It's less clear what driveOut should do. It could take a specific registration number to remove:

public void driveOut(String regNo) {
    carsRegNosParked.remove(regNo);
}

Or it could just indiscriminately remove a car, say the first car added:

public void driveOut() {
    if (!carsRegNosParked.isEmpty()) {
        carsRegNosParked.remove(0);
    }
}

Note the difference between remove(Object) and remove(int).

Sign up to request clarification or add additional context in comments.

2 Comments

Yep, it's like saying an EggCarton is an Egg. It makes no sense. 1+
Well the question asks me to put one parameter from class car and store it in carsparked . I added the inheritance btw
0

First change carsParked to a list. So:

private String[] carsParked;

becomes

private List<String> carsParked;

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:

public void driveIn(Car car) {
   carsParked.add(car.getRegNo());
}

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.

public Carpark (String locations, int room){
    location = locations;
    capacity = room;
    //this creates an array with the max number of spots
    carsParked = new String[capacity];
    //also good idea to init 
    carsIn = 0; //initial number of cars parked
}

then in your driveIn() method:

public void driveIn(Car car) {
   carsParked[carsIn] =car.getRegNo();
   carsIn=carsIn+1;
}

now driveOut()

public void driveOut(Car car) {
   //loop through the array until we find the car
   for (int i=0; i < carsParked.length; i=i+1){
     if (car.getRegNo().equals(carsParked[i])){
        //we found the car, so set the space null
        carsParked[i] = null;
        carsIn=carsIn-1;
        //stop looping now
        break;
     }
   }
}

Looks nice doesn't it. Well no it is not. Now the driveIn will not work, since we have null spots scattered all over the place. How do we fix it:

public void driveIn(Car car) {
   //loop through the array until we find a null spot, 
   //then park the car
   for (int i=0; i < carsParked.length; i=i+1){
     if (carsParked[i] == null){
        //we found the car, so set the space null
        carsParked[i] = car.getRegNo();
        carsIn=carsIn+1;
        //stop looping now
        break;
     }
   }
}

It could still be improved further. I would probably still change String[] carsParked to Car[] carsParked as to not throw away information. I would also change the driveIn and driveOut methods 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 call driveIn or driveOut you 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 car in 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.

13 Comments

I was going to delete this since I missed the obvious problem with the relationships, but once you fix that, then the rest of my post is valid. So I guess I will leave it for now.
Is there anyway doing it without using list?
For the parameters for drivein method what is Car for? Can u explain Car car a little bit
@Aaron see my edit, hope that helps. If not, it will be tomorrow before I am back as I am going to sleep.
@Aaron In regards to your comment, that is why you should make String[] carsParked Car[] carsParked instead. Then you could loop through the array, if the spot is not null, check if the color is equal to the one passed in. If it is add it to a local list. Then return the list. I am not going to write it for you, as I have a feeling this is a homework assignment, and think I have probably already given you to much. Sorry. Go try and write it. If you run into a specific problem ask a new question about it. Good luck!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.