# Scheduling Problem

• February 19th, 2011, 07:21 PM
pmg
Scheduling Problem
This program creates an array of assignments and sorts them in order from the earliest end first to the latest end time last. Also there is a Boolean array to keep track of which assignments can actually be used(true if actually on the schedule). Once they are sorted, I am supposed to create a schedule method that will take the assignments and turn each assignments' corresponding Boolean to true if it can be placed on the schedule. The first assignment on the list of 12 assignments is the current assignment and its corresponding Boolean must be set to true, then every assignment after the current assignment whose start time is before the end time of the current assignment must be skipped, then once you reach the first assignment whose start time is after the end time of the current assignment that assignment's corresponding Boolean must be set to true, then the same process again, etc... I have a for loop in the Schedule method, because I'm pretty sure that's what I need but I can't figure out how to set it up. Thanks.

Here is the code:
Code Java:

```package homework4;   import java.io.*;   import java.util.Scanner;   public class Scheduler { Assignment[] assign = new Assignment[12]; boolean[] b = new boolean[12];   public void load(Scanner sc) { int numberOfAssign = sc.nextInt();   for (int i = 0; i < assign.length; i++) { assign[i] = new Assignment(sc.next(), sc.nextInt(), sc.nextInt()); // System.out.println(assign[i].getStartTime()); }   }   private void sort(Assignment[] array) { int i, j; int maxindex;   for (i = array.length - 1; i >= 1; i--) { maxindex = 0; for (j = 1; j <= i; j++) { if (array[j].getEndTime() > array[maxindex].getEndTime()) {   maxindex = j; } } // swap minindex and i Assignment temp = array[i]; array[i] = array[maxindex]; array[maxindex] = temp;   }   }   public void Schedule() { sort(assign); for (int i = 0; i < assign.length; i++) {     }   }```
• February 19th, 2011, 07:35 PM
javapenguin
Re: Scheduling Problem
I don't know what's in your assignment class, but you don't need that load method.

2nd, only a class with a main method will even be able to run the console. I don't see a main method.

You should put a Scanner

Scanner console = new Scanner(System.in):

in your main method.

You have the right idea with the for loop.

But you should have each variable be read one at a time and then when you have enough to satisfy your constructor, then you create the new Assignment object. Right now it's going to be confused.

And ask for this in the main method.

Also, ti appears that you'll be missing index 0 of your array with your sort method.

Edit---Oh, you're assign variable is ok after all.

But your for loop is doing nothing in that Schedule method.

Instead of having boolean array, which not have a boolean variable as part of your Assignment class? It would make things easier.

You never called Load method anywhere. So as far as the compiler knows, your array is empty.

You'll be sorting an empty array.
• February 19th, 2011, 08:26 PM
aussiemcgr
Re: Scheduling Problem
Quote:

Originally Posted by javapenguin
I don't know what's in your assignment class, but you don't need that load method.

Also, as it's not in a constructor, your method that calls sort won't know what that array is and will say

cannot find symbol: assign

What is wrong with that? Assign is a variable whose scope is within the view of the load method.

Quote:

2nd, only a class with a main method will even be able to run the console. I don't see a main method.
The OP could have a main in another class for all we know. There may be a main that just has not been posted.

Quote:

You should put a Scanner

Scanner console = new Scanner(System.in):

in your main method.
Entirely stylistic, and it would appear that the Scanner is in the main since it is being sent to the load method. It is debatable how to go about this, but what is currently being done is not wrong.

Quote:

You have the right idea with the for loop.

But you should have each variable be read one at a time and then when you have enough to satisfy your constructor, then you create the new Assignment object. Right now it's going to be confused.
There shouldnt be any real confusion by the JVM. It will read in each value one at a time like it would if it was spread out to multiple lines like you suggest.

Quote:

And ask for this in the main method.

Also, ti appears that you'll be missing index 0 of your array with your sort method.
It would appear that the array index 0 is taken care of at the first iteration of the loop that uses i as a variable. Either way, I am unclear if the intent of the OP is to miss the index 0, so this could be a real concern.

javapenguin, word of advice, when providing advice be wary if what you are suggesting is fixing the problem or just stylistic changes that will more than likely not fix the problem at hand.