Make changes in the following code to get it to work class MainClass { public mainclass ()
Question:
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 ();
}
}
Introduction To Java Programming And Data Structures Comprehensive Version
ISBN: 9780136520238
12th Edition
Authors: Y. Daniel Liang