Solved

java timetabling problems

Posted on 2006-11-20
1
184 Views
Last Modified: 2010-03-31
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");
      }
    }
}







0
Comment
Question by:vrameen
1 Comment
 
LVL 10

Accepted Solution

by:
ADSLMark earned 500 total points
ID: 17982188
Well you probably won;t get this error all the time, but the idea is that you have a semi-recursive method, which in theory should end, but in practice, the computer won't be able to handle it.
Problem is in 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);
}

if the if-check fails, then the else is done, which is a recursive call with the same parameters to allocateTimeSlot. Let's assume the if-check fails.. 1.000.000 times after eachother, then java has to call allocateTimeSlot 1.000.000 times. There is something like a call-stack which keeps track on what method is called and from which method.. You can imagine something like:

allocateTimeSlot
--> allocateTimeSlot
    --> allocateTimeSlot
        --> allocateTimeSlot
            --> allocateTimeSlot
                --> allocateTimeSlot
                    --> etc.

Well that's where the stack overflow comes from.
A solution would be to use pseudo random numbers. Create a list of all the possible numbers/slots and then make a limited number of swaps. Next you iterate over this list and take the current number as index.

int capacity = 100;
int[] randomNumbers = new int[capacity];
for(int i=0;i<capacity;i++)
    randomNumbers[i] = i;

int numSwaps = 10000;
Random r = new Random();
for(int i=0;i<numSwaps;i++)
{
    int r1 = r.nextInt(capacity);
    int r2 = r.nextInt(capacity);

    int tmp = randomNumbers[r1];
    randomNumbers[r1] = randomNumbers[r2];
    randomNumbers[r2] = tmp;
}

for(int i=0;i<capacity;i++)
{
    index = randomNumbers[i];
    //Do whatever you want with the index.
}

I hope this explains the problem.
Mark
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now