Prog: Assume a hotel having 5 floors with each floor 10 rooms. No of people allowed in each room is 4. Fill the matrix using random and print it in matrix format. and 1. Count total number of customers in the hotel 2. Display which room is free 3. If no of customers in a room is 4, print a message saying that extra bed charges!
import java.util.Random;
class HRooms
{
public static void main( String v[ ] )
{
int Tcount=0;
Random rnd = new Random();
int avail[ ][ ] = new int [5][10];
for( int r=1; r<6; r++)
{
for( int c = 1; c<11; c++)
{
avail[r][c] = rnd.nextInt( 5 );
Tcount=Tcount+avail[r][c];
}
}
System.out.println("Rooms avail. list");
System.out.println("----------------------------------------");
for( int r=1; r<6; r++)
{
for( int c = 1; c<11; c++)
{
System.out.print(avail[r][c] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
System.out.println("Total number of customers in the hotel is "+Tcount);
System.out.print("\n");
for( int r=1; r<6; r++)
{
for( int c=1; c<11; c++)
{
if( avail[r][c]==0)
{
System.out.println("The room "+c+" in floor "+r+" is empty");
}
}
}
System.out.print("\n");
for( int r=1; r<6; r++)
{
for( int c = 1; c<11; c++)
{
if( avail[r][c]==4)
{
System.out.println("The room "+c+" in floor "+r+" has 4 customers, extra bed charges");
}
}
}
}
}
The output says Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at HRooms.main(HRooms.java:18)
Whats the solution??