11 Replies - 1199 Views - Last Post: 31 March 2014 - 06:46 AM Rate Topic: -----

#1 careswho   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-March 14

Queue and A. Practice exercise

Posted 25 March 2014 - 07:39 AM

I think I did everything already, however i still get the wrong answer.
The error that i was getting is that, all agents become idle at some point.
however if i eliminate the attribute that checks for the idleness of agents, i get a little more off the desired output.
I got the problem from UVA
http://uva.onlinejud...lem&problem=763


package queuea;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    
    
    public static void main(String[] args) {
        class RequestSet{
            private int id;
            private int qty;
            private int firstReq;
            private int timeReq;
            private int nextReq;
            private int sucReq;
            private int initialized;
            
            public RequestSet(int id,int qty,int firstReq,int timeReq,int nextReq){
                this.id=id;
                this.qty=qty;
                this.firstReq=firstReq;
                this.timeReq=timeReq;
                this.nextReq=nextReq;
                sucReq=nextReq;
                initialized=0;
            }
            
            public void decReq(){//Decreases the time attributes
                if(firstReq!=0)
                    firstReq-=1;
                if(nextReq!=0 && firstReq==0)
                    nextReq-=1;
            }

            public int getInitial(){//Determines if the Request set has already enqueued its first element
                return initialized;
            }

            public void setInitial(){
                initialized=1;
            }

            public void decQty(){//Decreases the requests qty of the Request set(When the set enqueues a request to the spool)
                qty--;
            }

            public void restoreReq(){//Replenishes the time remaining until the next Request
                nextReq=sucReq;
            }

            public int getID(){
                return id;
            }

            public int getQty(){
                return qty;
            }

            public int getFReq(){
                return firstReq;
            }

            public int getTimeReq(){
                return timeReq;
            }

            public int getNReq(){
                return nextReq;
            }
        }//End of Class "Request Set"
        
        class Request{
            
            private int topicID;
            private int minsNeeded;

            public Request(int id, int mins){
                topicID = id;
                minsNeeded = mins;
            }

            public void Process(){
                minsNeeded-=1;
            }

            public int getMins(){
                return minsNeeded;
            }

            public int getId(){
                return topicID;
            }
        }//End of Class "Request"
        
        class Server{
            
            private int id;
            private int topicsQty;
            private ArrayList<Integer> specialty;
            private int available;
            private int recentJobAgo;
            private int occTil;
            private int listStanding;

            public Server(int id, int qty, int stand){
                this.id=id;
                topicsQty=qty;
                available=1;
                specialty = new ArrayList();
                occTil=0;
                listStanding=stand;
            }

            public Server(){

            }

            public int getPosition(){//Returns the position of the agent in the input list(Agent which was inputted first)
                return listStanding;
            }

            public int getRecJ(){//Return the time elapsed after the most recent job
                return recentJobAgo;
            }


            public void display(){
                int i;
                for(i=0;i<topicsQty;i++){
                    System.out.printf("%d ",specialty.get(i));
                }
            }

            public void progress(){
                if(available==1)//if agent is available, the time elapsed after the most recent job is incremented
                    recentJobAgo+=1;
                else if(available==0 && occTil==0){//case wherein an agent finishes a request. "occTil==0"-occupied til 0 mins
                    available=1;
                }
                else//case where in the agent's still occupied, so we decrease the "Occupied Until" attribute
                    occTil--;
            }

            public int isAvailable(){
                return available;
            }

            public void occupy(int time){//Agent begins servicing a request
                available=0;
                recentJobAgo=0;
                occTil=time;
            }

            public void doneReq(){
                available=1;
            }

            public void learnTopics(int topicID){//Gets the input from the 3rd to the nth input, for agents. Priority topics.
                int i;
                specialty.add(topicID);
            }

            public int canDo(int id){//returns 1 if the topicID is in the agent's ArrayList of topic names
                int i, result=0;
                for(i=0;i<topicsQty;i++){
                    if(id==specialty.get(i) && available==1){
                        result=1;
                    }
                }
                return result;
            }
        }//End of Class "Server"

        int topics, servers, i, minutes=0, result=0, check=0;
        Scanner sc = new Scanner(System.in);
        int topicID, noReqs, bFirst, timeReq, betReqs, set=0;
        //topicID,# of Requests, time before the 1st request, time requirement to process a request, succeeding time between reqs
        int agentID, lrndTopics, j, aTopic;
        //agentID, # of priority topics
        RequestSet[] requests;
        Server[] agent;
        Server temp = new Server();
        Queue<Request> spool = new LinkedList<Request>();
        
        topics=sc.nextInt();
        requests = new RequestSet[topics];
        for(i=0; i<topics; i++)
        {
            topicID=sc.nextInt();
            noReqs=sc.nextInt();
            bFirst=sc.nextInt();
            timeReq=sc.nextInt();
            betReqs=sc.nextInt();
            requests[i] = new RequestSet(topicID,noReqs,bFirst,timeReq,betReqs);
        }//Gets inputs for the request sets
        
        servers=sc.nextInt();
        agent = new Server[servers];
        for(i=0; i<servers; i++)
        {
            agentID=sc.nextInt();
            lrndTopics=sc.nextInt();
            agent[i] = new Server(agentID, lrndTopics,i);
            for(j=0;j<lrndTopics; j++)
            {
                aTopic=sc.nextInt();
                agent[i].learnTopics(aTopic);
            }
        }//Gets inputs for the agent set
        
        
        
        //Request processing section
        do{
            if(set==1)
                minutes++;
            
            System.out.println("minute: "+minutes);
            
            for(i=0;i<topics;i++)
            {
                if(requests[i].getFReq()==0 && requests[i].getInitial()==0)
                {
                    spool.add(new Request(requests[i].getID(),requests[i].getTimeReq()));
                    requests[i].decQty();
                    requests[i].restoreReq();
                    requests[i].setInitial();
                    System.out.println("Request["+i+"] added at ["+minutes+"]");
                }
                else if(requests[i].getInitial()==1 && requests[i].getNReq()==0 && requests[i].getQty()>0)
                {
                    spool.add(new Request(requests[i].getID(),requests[i].getTimeReq()));
                    requests[i].decQty();
                    requests[i].restoreReq();
                    System.out.println("Request["+i+"] added at ["+minutes+"]");
                }
                else
                {
                    requests[i].decReq();
                }
            }
            
            //Sorts the agents according to their idle time. Most idle places first
            for(i=1;i< servers;i++)
            {
                for(j=0;j< servers-1;j++)
                    if(agent[j].getRecJ()<agent[j+1].getRecJ())
                    {
                        temp=agent[j];
                        agent[j]=agent[j+1];
                        agent[j+1]=temp;
                    }
            }
            
            //Swaps agents when they have equal idle time; however, the agent that was inputted first places first
            for(i=0;i< servers-1;i++)
            {
                    if(agent[i].getRecJ()==agent[i+1].getRecJ()){
                        if(agent[i].getPosition()>agent[i+1].getPosition()){
                            temp=agent[i];
                            agent[i]=agent[i+1];
                            agent[i+1]=temp;
                        }
                    }
            }
            
            //Decreases all the time attributes
            for(i=0;i<servers;i++)
            {
                agent[i].progress();
            }
            
            //Checks all agents if they're qualified to service a topic
            for(i=0;i<servers;i++)
            {
                if(spool.peek()!=null)
                {
                    if(agent[i].canDo(spool.peek().getId())==1)
                    {
                        agent[i].occupy(spool.peek().getMins());
                        spool.remove();
                    }
                }
            }
            
            
            
        
            //minutes++;
            set=1;
            
            check=0;
            result=0;
            
            //Checks all the requests sets' number of remaining topics
            for(j=0;j<topics;j++){
                if(requests[j].getQty()!=0)
                    check=1;
            }
            
            //Checks if all the agents are occupied
            for(i=0;i<servers;i++)
            {
                if(agent[i].isAvailable()==0)
                    result=1;
            }
            
        }while(check==1 && result==1);
        
        System.out.println("Minutes: "+minutes+" "+check+" "+result);
        
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Queue and A. Practice exercise

#2 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Queue and A. Practice exercise

Posted 25 March 2014 - 08:01 AM

Can you give a better example of how and when it fails. If you can figure where I might lead to which method is the cause.

You may want to rethink the structure of your classes.
Check the methods does it do what you expect it to?

And do you need a class inside main()??
Was This Post Helpful? 0
  • +
  • -

#3 careswho   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-March 14

Re: Queue and A. Practice exercise

Posted 25 March 2014 - 08:25 AM

hi!, i was just practicing, thats why i just created 1 java class but contained many other. I just eliminated the checking of (If all agents are idle, end). I think the decrement of the time attributes like "Time until next request in requestSet class", "Remaining processing time of a request in Server class" may be having conflicts. But i checked them many times already. I still can't pinpoint where it is going wrong
Was This Post Helpful? 0
  • +
  • -

#4 careswho   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-March 14

Re: Queue and A. Practice exercise

Posted 25 March 2014 - 09:19 AM

UPDATE: My code is now producing the correct output; however, UVA still doesn't accept it. Saying it has a runtime error? please help. What could be causing it? It runs fine on netbeans but why am I getting this result..

new Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package queuea;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
 *
 * @author Christian Gabriel
 */
class Main {
    
    
    public static void main(String[] args) {
        class RequestSet{
            private int id;
            private int qty;
            private int firstReq;
            private int timeReq;
            private int nextReq;
            private int sucReq;
            private int initialized;
            
            public RequestSet(int id,int qty,int firstReq,int timeReq,int nextReq){
                this.id=id;
                this.qty=qty;
                this.firstReq=firstReq;
                this.timeReq=timeReq;
                this.nextReq=nextReq;
                sucReq=nextReq;
                initialized=0;
            }
            
            public void decReq(){//Decreases the time attributes
                if(firstReq!=0)
                    firstReq-=1;
                else if(nextReq!=1 && firstReq==0)
                    nextReq-=1;
            }

            public int getInitial(){//Determines if the Request set has already enqueued its first element
                return initialized;
            }

            public void setInitial(){
                initialized=1;
            }

            public void decQty(){//Decreases the requests qty of the Request set(When the set enqueues a request to the spool)
                qty--;
            }

            public void restoreReq(){//Replenishes the time remaining until the next Request
                nextReq=sucReq;
            }

            public int getID(){
                return id;
            }

            public int getQty(){
                return qty;
            }

            public int getFReq(){
                return firstReq;
            }

            public int getTimeReq(){
                return timeReq;
            }

            public int getNReq(){
                return nextReq;
            }
        }//End of Class "Request Set"
        
        class Request{
            
            private int topicID;
            private int minsNeeded;

            public Request(int id, int mins){
                topicID = id;
                minsNeeded = mins;
            }

            public void Process(){
                minsNeeded-=1;
            }

            public int getMins(){
                return minsNeeded;
            }

            public int getId(){
                return topicID;
            }
        }//End of Class "Request"
        
        class Server{
            
            private int id;
            private int topicsQty;
            private ArrayList<Integer> specialty;
            private int available;
            private int recentJobAgo;
            private int occTil;
            private int listStanding;

            public Server(int id, int qty, int stand){
                this.id=id;
                topicsQty=qty;
                available=1;
                specialty = new ArrayList();
                occTil=0;
                listStanding=stand;
            }

            public Server(){

            }

            public int getPosition(){//Returns the position of the agent in the input list(Agent which was inputted first)
                return listStanding;
            }

            public int getRecJ(){//Return the time elapsed after the most recent job
                return recentJobAgo;
            }


            public void display(){
                int i;
                for(i=0;i<topicsQty;i++){
                    System.out.printf("%d ",specialty.get(i));
                }
            }

            public void progress(){
                if(available==1)//if agent is available, the time elapsed after the most recent job is incremented
                    recentJobAgo+=1;
                else if(available==0 && occTil==1){//case wherein an agent finishes a request. "occTil==0"-occupied til 0 mins
                    available=1;
                    occTil=0;
                }
                else//case where in the agent's still occupied, so we decrease the "Occupied Until" attribute
                    occTil--;
            }

            public int isAvailable(){
                return available;
            }

            public void occupy(int time){//Agent begins servicing a request
                available=0;
                recentJobAgo=0;
                occTil=time;
            }

            public void doneReq(){
                available=1;
            }

            public void learnTopics(int topicID){//Gets the input from the 3rd to the nth input, for agents. Priority topics.
                int i;
                specialty.add(topicID);
            }

            public int canDo(int id){//returns 1 if the topicID is in the agent's ArrayList of topic names
                int i, result=0;
                for(i=0;i<topicsQty;i++){
                    if(id==specialty.get(i) && available==1){
                        result=1;
                    }
                }
                return result;
            }
        }//End of Class "Server"

        int topics, servers, i, minutes=0, result=0, check=0;
        Scanner sc = new Scanner(System.in);
        int topicID, noReqs, bFirst, timeReq, betReqs, set=0;
        //topicID,# of Requests, time before the 1st request, time requirement to process a request, succeeding time between reqs
        int agentID, lrndTopics, j, aTopic;
        //agentID, # of priority topics
        RequestSet[] requests;
        Server[] agent;
        Server temp = new Server();
        Queue<Request> spool = new LinkedList<Request>();
        int spoolSize=0;
        Request reqTemp;
        
        topics=sc.nextInt();
        requests = new RequestSet[topics];
        for(i=0; i<topics; i++)
        {
            topicID=sc.nextInt();
            noReqs=sc.nextInt();
            bFirst=sc.nextInt();
            timeReq=sc.nextInt();
            betReqs=sc.nextInt();
            requests[i] = new RequestSet(topicID,noReqs,bFirst,timeReq,betReqs);
        }//Gets inputs for the request sets
        
        servers=sc.nextInt();
        agent = new Server[servers];
        for(i=0; i<servers; i++)
        {
            agentID=sc.nextInt();
            lrndTopics=sc.nextInt();
            agent[i] = new Server(agentID, lrndTopics,i);
            for(j=0;j<lrndTopics; j++)
            {
                aTopic=sc.nextInt();
                agent[i].learnTopics(aTopic);
            }
        }//Gets inputs for the agent set
        sc.nextInt();
        
        
        //Request processing section
        do{
            if(set==1)
                minutes++;
            
            //System.out.println("minute: "+minutes);
            
            for(i=0;i<topics;i++)
            {
                if(requests[i].getFReq()==0 && requests[i].getInitial()==0)
                {
                    spool.add(new Request(requests[i].getID(),requests[i].getTimeReq()));
                    requests[i].decQty();
                    requests[i].restoreReq();
                    requests[i].setInitial();
                    //System.out.println("Request["+i+"] added at ["+minutes+"]");
                    spoolSize++;
                }
                else if(requests[i].getInitial()==1 && requests[i].getNReq()==1 && requests[i].getQty()>0)
                {
                    spool.add(new Request(requests[i].getID(),requests[i].getTimeReq()));
                    requests[i].decQty();
                    requests[i].restoreReq();
                    //System.out.println("Request["+i+"] added at ["+minutes+"]");
                    spoolSize++;
                }
                else
                {
                    requests[i].decReq();
                }
            }
            
            //Sorts the agents according to their idle time. Most idle places first
            for(i=1;i< servers;i++)
            {
                for(j=0;j< servers-1;j++)
                    if(agent[j].getRecJ()<agent[j+1].getRecJ())
                    {
                        temp=agent[j];
                        agent[j]=agent[j+1];
                        agent[j+1]=temp;
                    }
            }
            
            //Swaps agents when they have equal idle time; however, the agent that was inputted first places first
            for(i=0;i< servers-1;i++)
            {
                    if(agent[i].getRecJ()==agent[i+1].getRecJ()){
                        if(agent[i].getPosition()>agent[i+1].getPosition()){
                            temp=agent[i];
                            agent[i]=agent[i+1];
                            agent[i+1]=temp;
                        }
                    }
            }
            
            //Decreases all the time attributes
            for(i=0;i<servers;i++)
            {
                agent[i].progress();
            }
            
            //Checks all agents if they're qualified to service a topic
            for(i=0;i<servers;i++)
            {
                if(spool.peek()!=null)
                {
                    
                    for(j=0;j<spoolSize;j++){
                        if(agent[i].canDo(spool.peek().getId())==1)
                        {
                            agent[i].occupy(spool.peek().getMins());
                            spool.remove();
                            spoolSize--;
                            //System.out.println("Agent["+i+"] got a request");
                        }
                        else{
                        reqTemp = spool.remove();
                        spool.add(reqTemp);
                        }
                    }
                }
            }
            
            
            
        
            //minutes++;
            set=1;
            
            check=0;
            result=0;
            
            //Checks all the requests sets' number of remaining topics
            for(j=0;j<topics;j++){
                if(requests[j].getQty()!=0)
                    check=1;
            }
            
            //Checks if all the agents are occupied
            for(i=0;i<servers;i++)
            {
                if(agent[i].isAvailable()==0)
                    result=1;
            }
            
            if(check==0 && result==1)
                check=1;
        }while(check==1);
        
        System.out.println("All requests are serviced within "+minutes+" minutes.");
        
    }
}



Was This Post Helpful? 0
  • +
  • -

#5 careswho   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-March 14

Re: Queue and A. Practice exercise

Posted 25 March 2014 - 09:39 AM

UVA finally didnt say runtime error, but said that I had the wrong answer.... Any ideas? I'm lost. My program produces the correct output but why.
Was This Post Helpful? 0
  • +
  • -

#6 careswho   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-March 14

Re: Queue and A. Practice exercise

Posted 27 March 2014 - 02:37 AM

anyone? please, any tips?
Was This Post Helpful? 0
  • +
  • -

#7 careswho   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-March 14

Re: Queue and A. Practice exercise

Posted 28 March 2014 - 10:45 PM

Hi, the I've been having is that, when i put the "do{}while(rounds!=0);" the UVa verdict judges my code as having a "Runtime error" but when i remove it , i get "Wrong answer" can you guys please help me with test cases? Because i think my program outputs the correct output already. I just dont know how to test it out on more inputs. Any kind of help will be very much appreciated thank you! My code runs well in my netbeans. Please give me tips.

The problem:
uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=763

My code:


import java.util.Scanner;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

class Main {
    
    
    public static void main(String[] args) {
        class RequestSet{
            private int id;
            private int qty;
            private int firstReq;
            private int timeReq;
            private int nextReq;
            private int sucReq;
            private int initialized;
            
            public RequestSet(int id,int qty,int firstReq,int timeReq,int nextReq){
                this.id=id;
                this.qty=qty;
                this.firstReq=firstReq;
                this.timeReq=timeReq;
                this.nextReq=nextReq;
                sucReq=nextReq;
                initialized=0;
            }
            
            public void decReq(){//Decreases the time attributes
                if(firstReq!=0)
                    firstReq-=1;
                else if((nextReq!=1 || nextReq!=0) && firstReq==0)
                    nextReq-=1;
            }

            public int getInitial(){//Determines if the Request set has already enqueued its first element
                return initialized;
            }

            public void setInitial(){
                initialized=1;
            }

            public void decQty(){//Decreases the requests qty of the Request set(When the set enqueues a request to the spool)
                qty--;
            }

            public void restoreReq(){//Replenishes the time remaining until the next Request
                nextReq=sucReq;
            }

            public int getID(){
                return id;
            }

            public int getQty(){
                return qty;
            }

            public int getFReq(){
                return firstReq;
            }

            public int getTimeReq(){
                return timeReq;
            }

            public int getNReq(){
                return nextReq;
            }
        }//End of Class "Request Set"
        
        class Request{
            
            private int topicID;
            private int minsNeeded;

            public Request(int id, int mins){
                topicID = id;
                minsNeeded = mins;
            }

            public void Process(){
                minsNeeded-=1;
            }

            public int getMins(){
                return minsNeeded;
            }

            public int getId(){
                return topicID;
            }
        }//End of Class "Request"
        
        class Server{
            
            private int id;
            private int topicsQty;
            private ArrayList<Integer> specialty;
            private int available;
            private int recentJobAgo;
            private int occTil;
            private int listStanding;

            public Server(int id, int qty, int stand){
                this.id=id;
                topicsQty=qty;
                available=1;
                specialty = new ArrayList();
                occTil=0;
                listStanding=stand;
            }

            public Server(){

            }

            public int getPosition(){//Returns the position of the agent in the input list(Agent which was inputted first)
                return listStanding;
            }

            public int getRecJ(){//Return the time elapsed after the most recent job
                return recentJobAgo;
            }


            public void display(){
                int i;
                for(i=0;i<topicsQty;i++){
                    System.out.printf("%d ",specialty.get(i));
                }
            }

            public void progress(){
                if(available==1)//if agent is available, the time elapsed after the most recent job is incremented
                    recentJobAgo+=1;
                else if(available==0 && (occTil==1 || occTil==0)){//case wherein an agent finishes a request. "occTil==0"-occupied til 0 mins
                    available=1;
                    occTil=0;
                }
                else//case where in the agent's still occupied, so we decrease the "Occupied Until" attribute
                    occTil--;
            }

            public int isAvailable(){
                return available;
            }

            public void occupy(int time){//Agent begins servicing a request
                if(time>0){
                    available=0;
                    recentJobAgo=0;
                    occTil=time;
                }
            }

            public void doneReq(){
                available=1;
            }

            public void learnTopics(int topicID){//Gets the input from the 3rd to the nth input, for agents. Priority topics.
                int i;
                specialty.add(topicID);
            }

            public int canDo(int id, int i){//returns 1 if the topicID is in the agent's ArrayList of topic names
                int result=0;
                //for(i=0;i<topicsQty;i++){
                    if(id==specialty.get(i) && available==1){
                        result=1;
                    }
                //}
                return result;
            }
        }//End of Class "Server"

        int topics, servers, i, result, check, rounds=0;
        String dump;
        ArrayList<Integer> minutes;
        int minute;
        Scanner sc = new Scanner(System.in);
        int topicID, noReqs, bFirst, timeReq, betReqs, set=0;
        //topicID,# of Requests, time before the 1st request, time requirement to process a request, succeeding time between reqs
        int agentID, lrndTopics, j, aTopic, k=0;
        //agentID, # of priority topics
        RequestSet[] requests;
        Server[] agent;
        Server temp;
        String input;
        Queue<Request> spool = new LinkedList<Request>();
        int spoolSize=0;
        Request reqTemp;
        minutes=new ArrayList();
        
        do{
            if(k==0){
                do{
                    //System.out.println("Enter 0");
                    topics=sc.nextInt();
                }while(topics>20 || topics<=0);
            }
            else{
                do{
                    //System.out.println("Enter 0");
                    if(rounds<0){
                        topics=sc.nextInt();
                    }
                    else
                        topics=rounds;
                }while(topics>20 || topics<=0);
            }
            dump=sc.nextLine();
            //input=sc.nextLine();
            
            requests = new RequestSet[topics];
            for(i=0; i<topics; i++)
            {
                input=sc.nextLine();
                StringTokenizer strToken = new StringTokenizer(input); 
                do{
                    //System.out.println("Enter 1");
                topicID=Integer.parseInt((String)strToken.nextElement());
                }while(topicID<=0);
                do{
                    //System.out.println("Enter 2");
                noReqs=Integer.parseInt((String)strToken.nextElement());
                }while(noReqs<=0);
                do{
                    //System.out.println("Enter 3");
                bFirst=Integer.parseInt((String)strToken.nextElement());
                }while(bFirst<0);
                do{
                    //System.out.println("Enter 4");
                timeReq=Integer.parseInt((String)strToken.nextElement());
                }while(timeReq<=0);
                do{
                    //System.out.println("Enter 5");
                betReqs=Integer.parseInt((String)strToken.nextElement());
                }while(betReqs<=0);
                requests[i] = new RequestSet(topicID,noReqs,bFirst,timeReq,betReqs);
            }//Gets inputs for the request sets
            do{
            servers=sc.nextInt();
            }while(servers>5 || servers<=0);
            input=sc.nextLine();
            agent = new Server[servers];
            for(i=0; i<servers; i++)
            {
                input=sc.nextLine();
                StringTokenizer strToken = new StringTokenizer(input);
                do{
                agentID=Integer.parseInt((String)strToken.nextElement());
                }while(agentID<=0);
                do{
                lrndTopics=Integer.parseInt((String)strToken.nextElement());
                }while(lrndTopics<=0);
                agent[i] = new Server(agentID, lrndTopics,i);
                for(j=0;j<lrndTopics; j++)
                {
                    do{
                    aTopic=Integer.parseInt((String)strToken.nextElement());
                    }while(aTopic<=0);
                    agent[i].learnTopics(aTopic);
                }
            }//Gets inputs for the agent set

            set=0;
            minute=0;
            //Request processing section
            do{
                if(set==1)
                    minute++;

//                System.out.println("minute: "+minute);

                for(i=0;i<topics;i++)
                {
                    if(requests[i].getFReq()==0 && requests[i].getInitial()==0)
                    {
                        spool.add(new Request(requests[i].getID(),requests[i].getTimeReq()));
                        requests[i].decQty();
                        requests[i].restoreReq();
                        requests[i].setInitial();
                        //System.out.println("1Request["+i+"] added at ["+minutes+"]");
                        spoolSize++;
                    }
                    else if(requests[i].getInitial()==1 && (requests[i].getNReq()==1 || requests[i].getNReq()==0)  && requests[i].getQty()>0)
                    {
                        spool.add(new Request(requests[i].getID(),requests[i].getTimeReq()));
                        requests[i].decQty();
                        requests[i].restoreReq();
                        //System.out.println("2Request["+i+"] added at ["+minutes+"]");
                        spoolSize++;
                    }
                    else
                    {
                        requests[i].decReq();
                        //System.out.println("decReq");
                    }
                }

                //Sorts the agents according to their idle time. Most idle places first
                for(i=1;i< servers;i++)
                {
                    for(j=0;j< servers-1;j++)
                        if(agent[j].getRecJ()<agent[j+1].getRecJ())
                        {
                            temp=agent[j];
                            agent[j]=agent[j+1];
                            agent[j+1]=temp;
                        }
                }

                //Swaps agents when they have equal idle time; however, the agent that was inputted first places first
                
                for(i=0;i< servers-1;i++)
                {
                        if(agent[i].getRecJ()==agent[i+1].getRecJ()){
                            if(agent[i].getPosition()>agent[i+1].getPosition()){
                                temp=agent[i];
                                agent[i]=agent[i+1];
                                agent[i+1]=temp;
                            }
                        }
                }
                
                for(i=0;i<servers;i++)
                {
                    agent[i].progress();
                }

                //Decreases all the time attributes
                
                //Checks all agents if they're qualified to service a topic
                for(j=0;j<topics;j++){
                    for(i=0;i<servers;i++)
                    {
                        if(spool.peek()!=null)
                        {
                            //for(j=0;j<spoolSize;j++){
                                if(agent[i].canDo(spool.peek().getId(),j)==1)
                                {
                                    agent[i].occupy(spool.peek().getMins());
                                    spool.remove();
                                    spoolSize--;
                                    //System.out.println("Agent["+i+"] got a request");
                                }
    //                            else{
    //                            reqTemp = spool.remove();
    //                            spool.add(reqTemp);
    //                            }
                            //}
                        }
                    }
                }
                
              
                
                set=1;

                check=0;
                result=0;

                //Checks all the requests sets' number of remaining topics
                for(j=0;j<topics;j++){
                    if(requests[j].getQty()!=0)
                        check=1;
                }

                //Checks if all the agents are occupied
                for(i=0;i<servers;i++)
                {
                    if(agent[i].isAvailable()==0)
                        result=1;
                }

                if(check==0 && result==1)
                    check=1;
                
            }while(check==1);
            minutes.add(minute);
            //System.out.println("Scenario 1: All requests are serviced within "+minute+" minutes.");
           
            
            rounds=sc.nextInt();
            if(rounds!=0)
                k++;
//            
//            
//            
        }while(rounds>0);
//        
        for(i=0;i<minutes.size();i++)
            System.out.println("Scenario "+(i+1)+": All requests are serviced within "+minutes.get(i)+" minutes.");
        
        
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Queue and A. Practice exercise

Posted 28 March 2014 - 11:02 PM

Please get those classes out of main, file->new class

I don't know what it is you have to do, but 400 lines of code seems excessive. It is hard for me to follow all those do loops.
Please try come up with better methods, and maybe you won't have so much trouble debugging.
Was This Post Helpful? 0
  • +
  • -

#9 careswho   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-March 14

Re: Queue and A. Practice exercise

Posted 28 March 2014 - 11:55 PM

hi, i placed all the classes in one file, because UVa only accepts 1 file, and instructed me to put all the classes inside main.
I'm really confused with the runtime error though, because it ran fine in my compiler.
Was This Post Helpful? 0
  • +
  • -

#10 careswho   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-March 14

Re: Queue and A. Practice exercise

Posted 31 March 2014 - 03:12 AM

I need more test cases for my code, it is displaying the correct answers however UVa still does not accept it.

The instruction is,
As requests are received, they are classified according to a predetermined list of topics. Each member of the support staff has responsibility for one or more of these topics and each topic has one or more support personnel assigned to it. Because staff members have different levels of expertise, each staff member has a prioritized list of topics that he or she can handle. Staff personnel are not permitted to handle requests outside their specified areas.

As staff members become available, they select from the pool of waiting requests according to their priority list of topics. All requests arriving at time t are available for allocation at time t. If two staff members are simultaneously available, scheduling preference is given to the one whose most recent job was scheduled earliest. If there is still a tie, scheduling preference is given to the person whose id number appears earlier in the input list of staff people. At the opening of business, all personnel are available to handle requests.

Input consists of a number of scenarios. Each scenario begins with the number of request topics, a positive integer no larger than 20. This is followed by a description of each topic. Each description consists of five integer values: a unique topic identifier, the number of requests for that topic, the elapsed time before the first request for that topic is received, the time needed to service a request, and the time between successive requests. All but the third of these values are positive integers; the elapsed time until the first request could be zero. Following this, the number of personnel is given. This will be a positive integer not to exceed 5. Finally, a description of each person is given in the form of three or more positive integer values: a unique identifying number for the person, the number of topics covered by this person, and a list of the topic identifiers arranged from highest priority to lowest priority for that person. A zero follows the last scenario.

The output is the sum of total minutes for each scenario.


Here is an example run of my program:
Input:
    3
    128 20 0 5 10
    134 25 5 6 7
    153 30 10 4 5
    4
    10 2 128 134
    11 1 134
    12 2 128 153
    13 1 153
    1
    128 5 0 1 10
    1
    11 1 128
    0


Output:
    Scenario 1: All requests are serviced within 195 minutes.
    Scenario 2: All requests are serviced within 41 minutes.



My Code:


    import java.util.Scanner;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.StringTokenizer;
    
    class Main {
        
        
        public static void main(String[] args) {
            class RequestSet{//Structure for RequestSets
                private int id;
                private int qty;
                private int firstReq;
                private int timeReq;
                private int nextReq;
                private int sucReq;
                private int initialized;
                
                public RequestSet(int id,int qty,int firstReq,int timeReq,int nextReq){
                    this.id=id;
                    this.qty=qty;
                    this.firstReq=firstReq;
                    this.timeReq=timeReq;
                    this.nextReq=nextReq;
                    sucReq=nextReq;
                    initialized=0;
                }
                
                public void decReq(){//Decreases the time attributes
                    if(firstReq>1)
                        firstReq-=1;
                    else if((nextReq!=1) && (firstReq==0 || firstReq==1))
                        nextReq-=1;
                }
    
                public int getInitial(){//Determines if the Request set has already enqueued its first element
                    return initialized;
                }
    
                public void setInitial(){
                    initialized=1;
                }
    
                public void decQty(){//Decreases the requests qty of the Request set(When the set enqueues a request to the spool)
                    qty--;
                }
    
                public void restoreReq(){//Replenishes the time remaining until the next Request
                    nextReq=sucReq;
                }
    
                public int getID(){
                    return id;
                }
    
                public int getQty(){
                    return qty;
                }
    
                public int getFReq(){
                    return firstReq;
                }
    
                public int getTimeReq(){
                    return timeReq;
                }
    
                public int getNReq(){
                    return nextReq;
                }
            }//End of Class "Request Set"
            
            class Request{//Structure for a Request
                
                private int topicID;
                private int minsNeeded;
    
                public Request(int id, int mins){
                    topicID = id;
                    minsNeeded = mins;
                }
    
                public int getMins(){
                    return minsNeeded;
                }
    
                public int getId(){
                    return topicID;
                }
            }//End of Class "Request"
            
            class Server{//Structure for Agents
                
                private int id;
                private int topicsQty;
                private ArrayList<Integer> specialty;
                private int available;
                private int recentJobAgo;
                private int occTil;
                private int listStanding;
    
                public Server(int id, int qty, int stand){
                    this.id=id;
                    topicsQty=qty;
                    available=1;
                    specialty = new ArrayList();
                    occTil=0;
                    listStanding=stand;
                }
    
                public Server(){
    
                }
                
                public int getOcc(){
                    return occTil;
                }
    
                public int getPosition(){//Returns the position of the agent in the input list(Agent which was inputted first)
                    return listStanding;
                }
    
                public int getRecJ(){//Return the time elapsed after the most recent job
                    return recentJobAgo;
                }
    
    
                public void display(){
                    int i;
                    for(i=0;i<topicsQty;i++){
                        System.out.printf("%d ",specialty.get(i));
                    }
                }
    
                public void progress(){
                    if(available==1)//if agent is available, the time elapsed after the most recent job is incremented
                        recentJobAgo+=1;
                    else if(available==0 && (occTil==1 || occTil==0)){//case wherein an agent finishes a request. "occTil==0"-occupied til 0 mins
                        available=1;
                        occTil=0;
                    }
                    else//case where in the agent's still occupied, so we decrease the "Occupied Until" attribute
                        occTil--;
                }
    
                public int isAvailable(){
                    return available;
                }
    
                public void occupy(int time){//Agent begins servicing a request
                    if(time>0){
                        available=0;
                        recentJobAgo=0;
                        occTil=time;
                    }
                }
    
                public void doneReq(){
                    available=1;
                }
    
                public void learnTopics(int topicID){//Gets the input from the 3rd to the nth input, for agents. Priority topics.
                    int i;
                    specialty.add(topicID);
                }
    
                public int canDo(int id, int i){//returns 1 if the topicID is in the agent's ArrayList of topic names
                    int result=0;
                    //for(i=0;i<topicsQty;i++){
                    if(i<specialty.size())
                        if(id==specialty.get(i) && available==1){
                            result=1;
                        }
                    //}
                    return result;
                }
            }//End of Class "Server"
    
            int topics, servers, i, result, check, rounds=0, minute;
            String dump;
            ArrayList<Integer> minutes;
            Scanner sc = new Scanner(System.in);
            int j, k=0;
            int topicID, noReqs, bFirst, timeReq, betReqs, set;
            //topicID,# of Requests, time before the 1st request, time requirement to process a request, succeeding time between requests
            int agentID, lrndTopics, aTopic;
            //agentID, # of priority topics, used to hold a topicID
            ArrayList<RequestSet> requests;
            Server[] agent;//Could have also been an arraylist
            Server temp;//for sorting the agents
            String input;
            Queue<Request> spool = new LinkedList<>();
            minutes=new ArrayList();//Used to hold several results. i.e. if the user inputs several scenarios
            
            int firstInput=sc.nextInt();
            
            if(firstInput!=0){
            do{
                if(k==0)
                {
                    topics=firstInput;
                }
                else
                {
                    topics=rounds;
                }
                dump=sc.nextLine();//Filters out "Enter"/"NextLine"
                
                requests = new ArrayList();
                for(i=0; i<topics; i++)
                {
    //                input=sc.nextLine();//Gets a string input containing 5 numbers
    //                StringTokenizer strToken = new StringTokenizer(input);
    //                topicID=Integer.parseInt((String)strToken.nextElement());//Gets the first number from the input
    //                noReqs=Integer.parseInt((String)strToken.nextElement());//Gets the 2nd
    //                bFirst=Integer.parseInt((String)strToken.nextElement());//3rd
    //                timeReq=Integer.parseInt((String)strToken.nextElement());//4th
    //                betReqs=Integer.parseInt((String)strToken.nextElement());//5th
    //                requests.add(new RequestSet(topicID,noReqs,bFirst,timeReq,betReqs));//Adds the request topic into the RequestSet array
                    //input=sc.nextLine();//Gets a string input containing 5 numbers
                    //StringTokenizer strToken = new StringTokenizer(input);
                    topicID=sc.nextInt();//Gets the first number from the input
                    noReqs=sc.nextInt();//Gets the 2nd
                    bFirst=sc.nextInt();//3rd
                    timeReq=sc.nextInt();//4th
                    betReqs=sc.nextInt();//5th
                    requests.add(new RequestSet(topicID,noReqs,bFirst,timeReq,betReqs));//Adds the request topic into the RequestSet array
                    
                }
                servers=sc.nextInt();//Gets the number of Agents
                
                dump=sc.nextLine();//Filters out "Enter"/"NextLine"
                
                agent = new Server[servers];//Creates an array of agents
                for(i=0; i<servers; i++)
                {
    //                input=sc.nextLine();//Accepts a line of input
    //                StringTokenizer strToken = new StringTokenizer(input);
                    //System.out.println("agent section");
                    agentID=sc.nextInt();//Gets the first input
                    lrndTopics=sc.nextInt();//Gets the 2nd input
                    agent[i] = new Server(agentID, lrndTopics,i);//Creates an agent in the array
                    
                    for(j=0;j<lrndTopics; j++)//Gets the priority list of topics, loops n times based on the 2nd input
                    {
                        aTopic=sc.nextInt();//Gets the 3rd input until the nth input
                        agent[i].learnTopics(aTopic);//calls a function that adds the topic into the agent's priority list.
                    }
                }//Gets inputs for the agent set
    
                set=0;//prevents the "minute" variable from increasing at the beginning
                minute=0;
                //Request processing section
                
                //PROCESSING PART
                do{
                    if(set==1)
                        minute++;
    
                    
                    for(i=0;i<topics;i++)//Checks if a request set is able to add a request into the Queue
                    {
                        if((requests.get(i).getFReq()==0 || requests.get(i).getFReq()==1) && requests.get(i).getInitial()==0)
                        {
                            spool.add(new Request(requests.get(i).getID(),requests.get(i).getTimeReq()));
                            requests.get(i).decQty();
                            requests.get(i).restoreReq();
                            requests.get(i).setInitial();
                        }
                        else if(requests.get(i).getInitial()==1 && (requests.get(i).getNReq()==1)  && requests.get(i).getQty()>0)
                        {
                            spool.add(new Request(requests.get(i).getID(),requests.get(i).getTimeReq()));
                            requests.get(i).decQty();
                            requests.get(i).restoreReq();
                        }
                        else
                        {
                            requests.get(i).decReq();
                        }
                    }
    
                    //Sorts the agents according to their idle time. Most idle places first
    //                for(i=0;i< (servers-1);i++)
    //                {
    //                    for(j=0;j< servers-i-1;j++)
    //                        if(agent[j].getRecJ()<agent[j+1].getRecJ())
    //                        {
    //                            temp=agent[j];
    //                            agent[j]=agent[j+1];
    //                            agent[j+1]=temp;
    //                        }
    //                }
                    
                    
    
                    //Swaps agents when they have equal idle time. The agent that was inputted first places first
                    for(i=0;i< servers-1;i++)
                    {
                            if(agent[i].getRecJ()==agent[i+1].getRecJ()){
                                if(agent[i].getPosition()>agent[i+1].getPosition()){
                                    temp=agent[i];
                                    agent[i]=agent[i+1];
                                    agent[i+1]=temp;
                                }
                            }
                    }
                    
                    //Decreases all the time attributes of all the agents
                    for(i=0;i<servers;i++)
                    {
                        agent[i].progress();
                    }
    
                    
                    
                    
                    //Checks all agents if they're qualified to service a topic
                    for(j=0;j<topics;j++)
                    {
                        for(i=0;i<servers;i++)
                        {
                            if(spool.peek()!=null)
                            {
                                    if(agent[i].canDo(spool.peek().getId(),j)==1)
                                    {
                                        agent[i].occupy(spool.peek().getMins());
                                        spool.remove();
                                    }
                            }
                        }
                    }
                    
                    set=1;
                    check=0;
                    result=0;
    
                    //Checks all the requests sets' number of remaining topics
                    for(j=0;j<topics;j++)
                    {
                        if(requests.get(j).getQty()!=0)
                            check=1;
                    }
                    //Checks if all the agents are occupied
                    for(i=0;i<servers;i++)
                    {
                        if(agent[i].isAvailable()==0)
                            result=1;
                    }
    
                    if(check==0 && result==1)//if all request sets are empty, check if an agent is still servicing a topic
                        check=1;
                    
                    
    //                System.out.println(requests.get(0).getQty()+" QTY");
    //                System.out.println(agent[0].getOcc()+" OCCTIL");
                }while(check==1);//End of PROCESSING PART
                
                
                minutes.add(minute);//Adds the result into the results array
                rounds=sc.nextInt();//If '0' is inputted, the program ends, otherwise, it will be stored into the "topics" variable
                if(rounds!=0)
                    k++;
                
                for(i=topics-1;i>=0;i--)//Removes all the request sets from the requests array, to prepare for a next scenario
                    requests.remove(i);
                
                
            }while(rounds!=0);      
            }
            //Displays all the results
            for(i=0;i<minutes.size();i++)
                System.out.println("Scenario "+(i+1)+": All requests are serviced within "+minutes.get(i)+" minutes.");
            
            
        }
        
    }


Was This Post Helpful? 0
  • +
  • -

#11 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Queue and A. Practice exercise

Posted 31 March 2014 - 06:43 AM

Sounds like it is an input problem.
How does UVa give you input? Does it run it ,or just sees if the code correct? Can you display the values after each time you do nextInt() to see if that data is correct?
Right now, you don't show any prompt messages so how are people suppose to know when to put in data.
Also look at line 201 k will always be 0.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Queue and A. Practice exercise

Posted 31 March 2014 - 06:46 AM

Related threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1