Anda di halaman 1dari 11

import java.util.

*;
class ttgen{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int[] pracs_sessions=new int[6];
int daily_hrs;
int week_hrs,prac_count,start_time,break1_start,break1_end,break
2_start,break2_end,subjects,i,j,k,p,total_hrs,same_subject;
System.out.println("Enter the number of subjects:");
subjects=sc.nextInt();
int subject_prac[] = new int[subjects];
int hrs[] = new int[subjects];
int theory[] = new int[subjects];
String subject_name[] = new String[subjects];
String teacher_name1[] = new String[subjects];
int teacher_hrs1[]=new int[subjects];
int teacher_hrs2[]=new int[subjects];
String teacher_name2[] = new String[subjects];
String practical_teacher1[]=new String[6];
String practical_teacher2[]=new String[6];
int[] pracs=new int[6];
do{
System.out.println("Enter the details for generating timetable f
or your institution:");
/*
Inputs to be taken/assumptions
1.Number of subjects(max 7)-each subject has two teachers each
2.Names of subjects
3.Name of teachers
4.Is it a practical subject or not?
5.Number of hours required for that subject in one week
6.Number of days is fixed-6
6.Each day timing is fixed-take it from user
7.Each day only one break of one hour-at fixed timing-take it fr
om user
8.practical is always of two hours continuous in a week.
9.There are two divisions of the same department with the same s
ubjects and teachers.
10.Theory,termwork,practical
*/
System.out.println("Enter the number of hours per day (Monday to
Saturday):");
daily_hrs=sc.nextInt();
week_hrs=daily_hrs*6+12;
prac_count=0;
System.out.print("Enter the
start_time=sc.nextInt();
System.out.print("Enter the
break1_start=sc.nextInt();
System.out.print("Enter the
break1_end=sc.nextInt();
System.out.print("Enter the
break2_start=sc.nextInt();
System.out.print("Enter the
break2_end=sc.nextInt();

start time:");
break 1 start time:");
break 1 end time:");
break 2 start time:");
break 2 end time:");

System.out.println("There are two divisions of the same departme


nt class A and class B with the same subjects and teachers.");
System.out.println("Enter the following details : ");
total_hrs=0;
for(i=0,j=0;i<subjects;i++){
System.out.print("\nSubject Name : ");
subject_name[i]=sc.next();
System.out.print("THEORY ?yes(1) or no(0) :");
theory[i]=sc.nextInt();
if(theory[i]==1){
System.out.print("Theory Teacher Name 1 : ");
teacher_name1[i]=sc.next();
System.out.print("Theory Teacher Name 2 : ");
teacher_name2[i]=sc.next();
System.out.print("Hours required per week for th
eory:(only even number of hours and max 12) ");
hrs[i]=sc.nextInt();
teacher_hrs1[i]=hrs[i]/2;
teacher_hrs2[i]=hrs[i]/2;
total_hrs+=hrs[i];
System.out.print("Does it have practical ? Yes(1
) or No(0) : ");
subject_prac[i]=sc.nextInt();
if(subject_prac[i]==1){
System.out.print("\nenter number of prac
tical sessions for this subject:");
pracs_sessions[j]=sc.nextInt();
total_hrs=total_hrs+2*pracs_sessions[j];
pracs[j]=i;
System.out.print("Practical Teacher name
for class A:");
practical_teacher1[j]=sc.next();
System.out.print("Practical Teacher name
for class B:");
practical_teacher2[j]=sc.next();
j++;
}
}
else{
teacher_name1[i]="null";
teacher_name2[i]="null";
hrs[i]=0;
subject_prac[i]=1;
System.out.print("\nenter number of practical se
ssions for this subject:");
pracs_sessions[j]=sc.nextInt();
total_hrs=total_hrs+2*pracs_sessions[j];
pracs[j]=i;
System.out.print("Practical Teacher name for cla
ss A:");
practical_teacher1[j]=sc.next();
System.out.print("Practical Teacher name for cla
ss B:");
practical_teacher2[j]=sc.next();
j++;
}

}
//if(total_hrs!=week_hrs)
//
System.out.println("error");
//else{
for(i=0,j=0;i<subjects;i++){
System.out.print("\nSubject code:"+i+" Subject n
ame: "+subject_name[i]+" taught by "+teacher_name1[i]+" and "+teacher_name2[i]);
System.out.print((subject_prac[i]==1)?" practica
l subject taught by "+practical_teacher1[j]+" in class A and by "+practical_teac
her2[j++]+" in class B":" no practical ");
}
prac_count=j;
//}
}while((total_hrs==week_hrs)&&(prac_count<=6));
/*now devising the time table for class*/
int a[][] = new int[daily_hrs][6];//creating a 2d array to fit s
lots ignoring the break slot-->for class A
int b[][] = new int[daily_hrs][6];//creating a 2d array to fit s
lots ignoring the break slot-->for class B
//initialising
for(i=0;i<daily_hrs;i++){
for(j=0;j<6;j++){
a[i][j]=0;
b[i][j]=0;
}
}
//computing time table for class a(class b will have same timeta
ble with opposite faculty of that subject)
int current_fill;//,counter,empty;
int flagcolumn[]=new int[6];
int flagrow1[]=new int[daily_hrs];
int flagrow2[]=new int[daily_hrs];
//FOR CLASS A
//teacher1
for(i=0;i<subjects;i++){
current_fill=10+i;
//initializing all flags for this subject
for(j=0;j<6;j++){
flagcolumn[j]=0;
}
for(j=0;j<daily_hrs;j++){
flagrow1[j]=0;
flagrow2[j]=0;
}
for(j=0;j<daily_hrs;j++){
for(k=0;k<6;k++){
if(flagcolumn[k]==0){
if(flagrow1[j]==0 && flagrow2[j]
==0){
if(a[j][k]==0){
if(teacher_hrs1[
i]>0){
a[j][k]=
current_fill;

flagrow1
[j]=1;
flagcolu
mn[k]=1;
teacher_
hrs1[i]--;
}
}
}
else if(flagrow1[j]==1 && flagro
w2[j]==0){
if(a[j][k]==0){
if(teacher_hrs1[
i]>0){
a[j][k]=
current_fill;
flagrow2
[j]=1;
flagcolu
mn[k]=1;
teacher_
hrs1[i]--;
}
}
}
}
}
}
}
//teacher2
for(i=0;i<subjects;i++){
current_fill=20+i;
//initializing all flags for this subject
for(j=0;j<6;j++){
flagcolumn[j]=0;
}
for(j=0;j<daily_hrs;j++){
flagrow1[j]=0;
flagrow2[j]=0;
}
for(j=0;j<daily_hrs;j++){
for(k=0;k<6;k++){
if(flagcolumn[k]==0){
if(flagrow1[j]==0 && flagrow2[j]
==0){
if(a[j][k]==0){
if(teacher_hrs2[
i]>0){
//check
for first row
if(j==0)
{
if(a[j+1][k]==10+i){
//do nothing
}

else{
//assign
a[j][k]=current_fill;
flagrow1[j]=1;
flagcolumn[k]=1;
teacher_hrs2[i]--;
}
}
//check
for last row
else{
if(j==daily_hrs-1){
if(a[j-1][k]==10+i){
//do nothing
}
else{
//assign
a[j][k]=current_fill;
flagrow1[j]=1;
flagcolumn[k]=1;
teacher_hrs2[i]--;
}
}
//check for any other intermediate row
else{
if(a[j-1][k]==10+i || a[j+1][k]==10+i){
//do nothing
}
else{
//assign
a[j][k]=current_fill;
flagrow1[j]=1;

flagcolumn[k]=1;
teacher_hrs2[i]--;
}
}
}
}//
}
}
else if(flagrow1[j]==1 && flagro
w2[j]==0){
if(a[j][k]==0){
if(teacher_hrs2[
i]>0){
//check
for first row
if(j==0)
{
if(a[j+1][k]==10+i){
//do nothing
}
else{
//assign
a[j][k]=current_fill;
flagrow2[j]=1;
flagcolumn[k]=1;
teacher_hrs2[i]--;
}
}
//check
for last row
else{
if(j==daily_hrs-1){
if(a[j-1][k]==10+i){
//do nothing
}
else{
//assign
a[j][k]=current_fill;

flagrow2[j]=1;
flagcolumn[k]=1;
teacher_hrs2[i]--;
}
}
//check for any other intermediate row
else{
if(a[j-1][k]==10+i || a[j+1][k]==10+i){
//do nothing
}
else{
//assign
a[j][k]=current_fill;
flagrow2[j]=1;
flagcolumn[k]=1;
teacher_hrs2[i]--;
}
}
}
}
}
}
}
}
}
}
//clash check for teachers1
for(i=0;i<subjects;i++){
if(teacher_hrs1[i]>0){//remaining to be assigned
current_fill=10+i;
while(teacher_hrs1[i]!=0){
//search for an empty slot
for(j=0;j<daily_hrs;j++){
for(k=0;k<6;k++){
if(a[j][k]==0){/
/empty slot found
a[j][k]=
current_fill;
teacher_
hrs1[i]--;
}
}

}
}
}
}
//clash check for teachers2
for(i=0;i<subjects;i++){
if(teacher_hrs2[i]>0){//remaining to be assigned
current_fill=20+i;
while(teacher_hrs2[i]!=0){
//search for an empty slot
for(j=0;j<daily_hrs;j++){
for(k=0;k<6;k++){
if(a[j][k]==0){/
/empty slot found
a[j][k]=
current_fill;
teacher_
hrs2[i]--;
}
}
}
}
}
}
//FOR CLASS B
for(j=0;j<6;j++){
for(i=0;i<daily_hrs;i++){
if(a[i][j]/10==1){
b[i][j]=a[i][j]+10;
}
else{
b[i][j]=a[i][j]-10;
}
}
}
//PRINTING TEACHER HRS LEFT
/*
System.out.println();
for(i=0;i<subjects;i++){
System.out.print("\n "+teacher_hrs1[i]+" and "+teacher_h
rs2[i]);
}
//PRINTING CHECK AFTER ALLOCATION
System.out.println();
for(i=0;i<daily_hrs;i++){
for(j=0;j<6;j++){
System.out.print(" "+a[i][j]);
}
System.out.println();
}
*/
//PRACTICALS
//FOR CLASS A
int[] prac_alloc=new int[6];
for (i=0;i<6;i++){

prac_alloc[i]=0;//practical slots are initialized as emp


ty
}
for(j=0;j<prac_count;j++){
for(i=0;i<6;i++){
while(pracs_sessions[j]>0){
if(prac_alloc[i]==0){//if the current sl
ot is empty
if(i==0){//monday slot
prac_alloc[0]=0;
}
if(i>0 && i<5){//tuesday or wedn
esday or thursday or friday slot
if(prac_alloc[i-1]==j ||
prac_alloc[i+1]==j){
//do nothing
}
else{//allocate
prac_alloc[i]=j;
pracs_sessions[j
]--;
}
}
if(i==5){//saturday slot
if(prac_alloc[4]==j){
//do nothing
}
else{
//allocate
prac_alloc[5]=j;
pracs_sessions[j
]--;
}
}
}
}
}
}
//FOR CLASS B
//same as A.....only teacher's name will change accordingly
/*printing the time table*/
int teacherindex,subjectindex;
int time;
time=start_time;
System.out.print("\nTime Table for Class A :\n");//class A timet
able
System.out.print("\nTime Monday Tuesday Wednesday Thursday Fri
day Saturday\n\n");
for(i=0;i<daily_hrs;i++){
for(j=-1;j<6;j++){
if(j==-1){
if(time==13)
time=1;
if(time==break1_start){
System.out.print(time+":00\t\tB\
tR\tE\tA\tK\n");
time=break1_end;

}
else if(time==break2_start){
System.out.print(time+":00\t\tB\
tR\tE\tA\tK\n");
time=break2_end;
}
System.out.print(time+":00\t");
time++;
}
else{
if(a[i][j]!=0){
subjectindex=a[i][j]%10;
teacherindex=a[i][j]/10;
System.out.print(" "+subject_nam
e[subjectindex]+"(");
if(teacherindex==1){
System.out.print(teacher
_name1[subjectindex]+")");
}
else{
System.out.print(teacher
_name2[subjectindex]+")");
}
}
else
System.out.print("

-----

");

}
}
System.out.println();
}
System.out.print("\n"+time+":00");
for(i=0;i<6;i++){//practicals printing
if(practical_teacher1[prac_alloc[i]]!=null)
System.out.print(subject_name[pracs[prac_alloc[i
]]]+"("+practical_teacher1[prac_alloc[i]]+")");
else
System.out.print(" -------- ");
}
time=start_time;
System.out.print("\nTime Table for Class B :\n");//class B timet
able
System.out.print("\nTime Monday Tuesday Wednesday Thursday Frid
ay Saturday\n\n");
for(i=0;i<daily_hrs;i++){
for(j=-1;j<6;j++){
if(j==-1){
if(time==13)
time=1;
if(time==break1_start){
System.out.print(time+":00\t\tB\
tR\tE\tA\tK\n");
time=break1_end;
}
else if(time==break2_start){
System.out.print(time+":00\t\tB\
tR\tE\tA\tK\n");
time=break2_end;
}
System.out.print(time+":00\t");

time++;
}
else{
if(b[i][j]>0){
subjectindex=b[i][j]%10;
teacherindex=b[i][j]/10;
System.out.print(" "+subject_nam
e[subjectindex]+"(");
if(teacherindex==1){
System.out.print(teacher
_name1[subjectindex]+")");
}
else{
System.out.print(teacher
_name2[subjectindex]+")");
}
}
else
System.out.print("

-----

");

}
}
System.out.println();
}
System.out.print("\n"+time+":00");
for(i=0;i<6;i++){//practicals printing
if(practical_teacher2[prac_alloc[i]]!=null)
System.out.print(subject_name[pracs[prac_alloc[i
]]]+"("+practical_teacher2[prac_alloc[i]]+")");
else
System.out.print(" -------- ");
}
}
}