Link to home
Start Free TrialLog in
Avatar of vrameen
vrameen

asked on

java timetabling problems

I've been trying to implement a timetable allocating system which allocates courses to timeslots randomly. i've just done 2 classes :-
Two_D_Allocation class which contains a main method for running the program and, Two_TimeArray class which is a 2-d array class which handles the timeslots, and methods to manipulate the timeslots. I, ve been having the following errors

cd /s_home1/tor3/project/
/usr/lib/j2sdk1.4-sun/bin/java Two_D_Allocation

Exception in thread "main" java.lang.StackOverflowError

Process Two_D_Allocation exited abnormally with code 1


the source code for both classes are shown below


/**
 * Two_D_Allocation.java
 *
 *
 * Created: Mon Nov 20 13:35:39 2006
 *
 * @author <a href="mailto:">T. O. ROGERS-HALLIDAY</a>
 * @version 1.0
 */
public class Two_D_Allocation {

   
    public static void allocateTimeSlot(String course, Two_TimeArray ts){
      int dayIdx = ts.generateDay(ts.getNoOfDays() - 1);
      int periodIdx = ts.generatePeriod(ts.getDailyCapacity() - 1);
      if(ts.availabilityCheck(dayIdx, periodIdx)== true){ts.assignCourse(course, dayIdx, periodIdx);}
      else allocateTimeSlot(course, ts);
      
      }

    public static void main (String [] args){
      
      Two_TimeArray cs = new Two_TimeArray(5);
      String [] courses  = {"maths" , "english", "physics", "chemistry"};

//       for(int i = 0; i <= courses.length - 1 ; i++) {
//           allocateTimeSlot(courses[i], cs);
//          //   System.out.println("just assigned " + courses[i]+ "/n" + "/n");
//       }

//       int d = cs.generateDay(4);
//       int p = cs.generatePeriod(4);
      allocateTimeSlot("maths",cs);
      allocateTimeSlot("english",cs);

      cs.printSlots();
    }
}

the class for handling timeslots is shown below

/**
 * Describe class 2-DTimeArray here.
 *
 *
 * Created: Fri Nov 17 13:08:46 2006
 *
 * @author <a href="mailto:">T. O. ROGERS-HALLIDAY</a>
 * @version 1.0
 */

import java.util.*;
import java.awt.*;

public class Two_TimeArray {
   
    private static String [] days = {"monday","tuesday","wednesday","thursday","friday"};
    private  int capacity;
   
    private  String slots [][];
   
    public Two_TimeArray(int slotcapacity) {
      
      capacity = slotcapacity;
      slots = new String[days.length][slotcapacity];
      
      for(int i = 0; i < days.length ; i++){

          int count = 0;
          while(count < capacity ){
            slots[i][count] = "free";
            //            System.out.println("just assigned to free");
            count++;
          }
      }
    }

    public  boolean availabilityCheck(int dayIdx, int periodIdx){
      
      if(slots[dayIdx][periodIdx] == "free"){return true;}
      else return false;
    }

    public  int generatePeriod(int no_of_periods){
      Random r = new Random();
      return r.nextInt(no_of_periods);
    }
    public  int generateDay(int no_of_days){
       Random r = new Random();
       return r.nextInt(no_of_days);
     }
   
    public  int getDailyCapacity(){return capacity;}
   
    public int getNoOfDays(){return days.length;}
   
    public  void assignCourse(String course, int dayIdx, int periodIdx){           slots[dayIdx][periodIdx] = course;
    }
   

    public void printSlots(){
      
      for(int i = 0; i < days.length ; i++){
          int count = 0;
          while(count < capacity ){
            System.out.println(slots[i][count]);
            count++;
          }
          System.out.println("\n" + "\n");
      }
    }
}







ASKER CERTIFIED SOLUTION
Avatar of ADSLMark
ADSLMark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial