Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8805193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:48:11+00:00 2026-06-14T01:48:11+00:00

I’m trying to take one parameter from the parent class of Car and add

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:48:12+00:00Added an answer on June 14, 2026 at 1:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.