Solved

java timetabling problems

Posted on 2006-11-20
1
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Certificat to iSeries KeyStore 1 31
by zero exception 10 83
Running JavaFX on JDeveloper 12C 1 116
import as existing maven project 3 78
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

752 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