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

  • SEARCH
  • Home
  • 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 8992045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:51:55+00:00 2026-06-15T22:51:55+00:00

I have a final for my java class soon and I was hoping I

  • 0

I have a final for my java class soon and I was hoping I could get help with a question. It is regarding sample code below:

package test;

public class Exam2Code
{

    public static void main ( String[ ] args )
    {
        Lot parkingLot = new Lot();

        Car chevy = new Car( );
        Car camry = new Car( );
        MotorCycle harley = new MotorCycle( 3 );
        MotorCycle honda = new MotorCycle( );

        parkingLot.park ( chevy );
        parkingLot.park ( honda );
        parkingLot.park ( harley );
        parkingLot.park ( camry );

        System.out.println( parkingLot.toString( ) );
        System.out.println(chevy.getClass());
        System.out.println(camry.getClass());
        System.out.println(harley.getClass());
        System.out.println(honda.getClass());
    }


} 

package test;

public class Vehicle
{

    private int nrWheels;

    public Vehicle( )
        { this( 4 ); }
    public Vehicle ( int nrWheels )
        { setWheels( nrWheels); }
    public String toString (  )
        { return "Vehicle with " + getWheels()
                 + " wheels"; }
    public int getWheels ( )
        { return nrWheels; }
    public void setWheels ( int wheels )
        { nrWheels = wheels; }
}

package test;

public class MotorCycle extends Vehicle
{

    public MotorCycle ( )
        { this( 2 ); }
    public MotorCycle( int wheels )
        { super( wheels ); }


}

package test;

public class Car extends Vehicle
{

    public Car ( )
        { super( 4 ); }
    public String toString( )
    {
        return "Car with " + getWheels( ) + " wheels";
    }
}

package test;

public class Lot
{

    private final static int MAX_VEHICLES = 20;
    private int nrVehicles;
    private Vehicle [] vehicles;

    public Lot (  )
    {
        nrVehicles = 0;
        vehicles = new Vehicle[MAX_VEHICLES];
    }

    public int nrParked (  )
        { return nrVehicles; }
    public void park ( Vehicle v )
        { vehicles[ nrVehicles++ ] = v; }
    public int totalWheels ( )
    {
        int nrWheels = 0;
        for (int v = 0; v < nrVehicles; v++ )
                nrWheels += vehicles[ v ].getWheels( );
        return nrWheels;
    }
    public String toString( )
    {
        String s = "";

        for (Vehicle v : vehicles){
                if(v != null){
                        s += v.toString( ) + "\n";
        }
    }
        return s;
    }
}

The question is “Identify the common coding mistake in Lot’s park method. How would you correct this coding error?”.I have no idea what the mistake is. My original answer was that it is a copy constructor and use the clone() method to correct it. But I am not even sure if it is a copy constructor. I checked their classes using getClass() and they all seem to be of the correct type. I would appreciate if anyone could help me out with this. Thank you all!

EDIT: added the code.

  • 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-15T22:51:56+00:00Added an answer on June 15, 2026 at 10:51 pm

    Identify the common coding mistake in Lot’s park method. How would you correct this coding error?

    Here is the relevant piece of code:

    private int nrVehicles;
    private Vehicle [] vehicles;
    
    public Lot (  )
    {
        nrVehicles = 0;
        vehicles = new Vehicle[MAX_VEHICLES];
    }
    
    public void park ( Vehicle v ) { 
        vehicles[ nrVehicles++ ] = v; 
    }
    

    The only “problem” I see here is the lack of range checking when you park more than nrVehicles cars (which is done implicitly by the JVM, but I guess your teacher is not satisfied by that), so:

    public void park ( Vehicle v ) { 
        if(nrVehicles < MAX_VEHICLES) {
          vehicles[nrVehicles++] = v; 
        } else {
          throw new IllegalStateException("Lot full, go find another one!");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Final goal: Have a few java objects sharing the same base class persisted into
I have the following Java code: final Future future = exeService.submit( new Runnable() {
Does making the below class final (adding public final class) have any real impact
I have created a Java class MyObject: public class MyObject { public final String
I have a Java class with a static variable package com.mytest public class MyClass{
I have Java class with JList and ListSelectionListener: final JList myList = new JList();
I have a java class that looks like public class MyClass { private final
I have my own little serialiser class package mypackage.shared; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import
I have several final properties defined in a Java class with constructor which has
I have an Java class with a static final method getAll: public static final

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.