Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

There are several issues in the provided code that need to be addressed to make it work properly Ill ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Java Programming And Data Structures Comprehensive Version

Authors: Y. Daniel Liang

12th Edition

0136520235, 978-0136520238

More Books

Students also viewed these Programming questions

Question

Question

Answered: 1 week ago