115k views
5 votes
The IntList class contains code for an integer list class. Study it; notice that the only things you can do are: create a list of a fixed size and add an element to a list. If the list is already full, a message will be printed. List Test is a driver class that creates an IntList, puts some values in it, and prints it. Compile and run it to see how it works.

1 Answer

2 votes

Step-by-step explanation:

public class Int_List

{

protected int[] list;

protected int numEle = 0;

public Int_List( int size )

{

list = new int[size];

public void add( int value )

{

if ( numEle == list.length )

{

System.out.println( "List is full" );

}

else

{

list[numEle] = value;

numEle++;

}

}

public String toString()

{

String returnStr = "";

for ( int x = 0; x < numEle; x++ )

{

returnStr += x + ": " + list[x] + "\\";

}

return returnStr;

}

}

public class Run_List_Test

{

public static void main( String[] args )

{

Int_List myList = new Int_List( 7 );

myList.add( 102 );

myList.add( 51 );

myList.add( 202 );

myList.add( 27 );

System.out.println( myList );

}

}

Note: Use appropriate keyword when you override "tostring" method

User DoppyNL
by
7.5k points