Hi am new here and as well to java.. Let me come straight to my problem.. My Problem is as follows:
There are a number of customers waiting to use a machine. Each customer is allotted a time for which he can use the machine.. The customer with the minimum time is allowed to use the machine first followed by the next customer. this process continues till all have finished their alloted times.
the ques is using time of customers are int[] using_time{10,7,3,4,11}
the time alloted for the machine is int time=5;
Now i have to return an array in the order in which the customer with the minimum balance started to use that is in this example first the customer at position 3 will use then cust at pos 4 followed by7 and the customer at the last position 5 will use last.. Again the cycle continues with the customer at position 2 who has a balance of 2 will use the machine.. This is more like a Round Robin algorithm problem... The difficulty i am having is in returning an array of the position of the customers who first started to use the machine with the minimum balance... The answer to this one is
{3,4,2,1,5,2,1,5,5}---the index numbers of the persons starting with the minimum balance
well the code i wrote is very very very very bad but this is what i could manage
class Machine { int[] get_time(int b[],int t) { int[] index=new int[b.length]; // array to store index positions int[] diff=new int[b.length]; //array to store difference for(int i=0;i<b.length;i++) { if(b[i]<=t) { index[i]=i+1; } } for(int i=0;i<b.length;i++) { if(b[i]>t) { diff[i]=b[i]-5; if(diff[i]<5) { index[i]=i+1; } } } for(int i=0;i<b.length;i++) { if(diff[i]>t) { index[i]=i+1; diff[i]=diff[i]-5; } } return index; } public static void main(String args[]) { int[] bt = {10, 7, 3, 4, 11}; int time_alloted=5; int[] output=new Machine().get_time(bt,time_alloted); if(output.length>0){ for(int i=0;i<output.length;i++) { System.out.println(output[i]); } } } }
The answer i am getting is 0,1,2,3,4,5.. I know my method is wrong but when i break the code down into jus the if-conditions i am getting the index positions like
I dont know how to combine them and how to reduce the if conditions...Should i use a recursive function so that i can check to see if the difference is greater than time allotted ??? Please help me and i am very very sorry if my question is long as i wanted to give all the details.. Am really sorry for the long quest but plz help meif(b[i]>t) temp[i]=i+1; println(temp[i]);