Question: Make changes in the following code to get it to work class MainClass { public mainclass () { MArray = new CarClass [3]; for (int

Make changes in the following code to get it to work
class MainClass
{

public mainclass ()

{
MArray = new CarClass [3];
for (int i = 0; i < 5; i++)
{
 MArray[i] = null;
}

}
 
public boolean CarClass) (CarClass CarClass)
{
for (int i = 0; i < 5; i++)
{
if MArray[i] == null)
   {
       MArray[i] = MObject;
       return true;
        }
}
return false;
}

public boolean removeCarClass (int instanceNumber)
{
for (int i = 0; i < 5; i++)
{
 // note: if the first condtion is false, the second will
 // not be evaluated - callin method via an invalid
 // reference will cause an exception to be thrown
 if (MArray[i] !=null &&
  MArray[i].getInstanceNumber () == instanceNumber)
  {
  MArray[i] = null;
  return true;
  }
}
return false; // no object with that instance number
  }
 
}
 

class ClassCar
{
public ClassCar ()
{
inst = i++;
}

public void doSomething ()
{
System.out.println ("instance " + inst + " doing something"");
}
public int getInstanceNumber ()
{
return inst;

}

static private int i = 0;
private int inst;
}

 

 

private int i;


public void run ()
{
for (int i = 0; i < 5; i++)
{
     if (MArray[i] != null)
     {
      MArray[i].doSomething ();
     
     }
}
}

private classM [] MArray;
}

public class app
{
public static void main (String[] strArgs)
{
 classN NObject = newclassN ();
    NObject.addMObject (new classM ());
    NObject.addMObject (new classM ());
    NObject.addMObject (new classM ());
    NObject.removeMObject (1);
    NObject.run ();
}
}
     
     


Step by Step Solution

3.58 Rating (158 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

There are several issues in the provided code that need to be addressed to make it work properly Ill ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!