Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:43:31+00:00 2026-06-12T15:43:31+00:00

When I try to run(use) the last method, it start running and never stops.

  • 0

When I try to run(use) the last method, it start running and never stops. Also When I try to print out aJob the element does not print out correctly. It comes out with odd letters. I am posting the whole class because I am guessing that I am making a mistake somewhere else apart from runJod and runAll methods. could anyone tell me what I need to do to fix this please?

import java.util.ArrayList;

/**
*/

public class JobQueue

{
private ArrayListmyJobInQueue; //a list of job to be done

private ArrayList<Job>myFinishedJobs;// a list of compleated job

private int myJobDuration; //duration if one job

private int myTimeLeft;//total time left 
/**
 * Constructor for objects of class JobQueue
 */
 public JobQueue()
{
  myJobInQueue = new ArrayList<Job>();
  myFinishedJobs = new ArrayList<Job>();
  myJobDuration =0; 
  myTimeLeft=0;  
}

/**
 * Return the list of jobs that have not been completed (including the current job).
 */
 public ArrayList<Job> getPendingJobs()
 {
  return myJobInQueue;  
 }

/**
 * Return the list of jobs that have been completed.
 */
 public ArrayList<Job> getCometedJobs()
 {
  return myFinishedJobs;
 } 

/**
 * Return the job at the front of the pending queue, or null if the queue is empty.
 */
 public Job getCurrentJob()
 {
   if(myJobInQueue!=null)
   { 
    Job FirstJobInTheQueue = myJobInQueue.get(0);   
    return FirstJobInTheQueue;
   }
   else
   {
    return null;
   }

 } 

/**
 * Return the amount of time left on the clock (as an integer)
 */
 public int getTimeLeft()//Ok
 {
   return myTimeLeft; 
 }

/**
 * Return the total duration of all the pending jobs(as an integer).
 */

 public int getTotalDuration()
 {
   int myTimeLeft= 0;
   for(int i = 0; i<myJobInQueue.size();i++)
   {
       int num = myJobInQueue.getDuration(i); //I think this line is wrong. 
       myTimeLeft = myTimeLeft + num ;
   }
   return myTimeLeft;
 }

/**
 * Add a Job to the end of the Queue
 */ 
 public void addJob(Job job)
 {   
    if(job!=null)
    {
      myJobInQueue.add(job);
    }
 }

/**
  * Add the specified number of seconds to the clock.
  */
 public void addTime(int seconds)
 {
    if(seconds>0)
    {
      myTimeLeft = myTimeLeft  + seconds;
    }
 }

 /**
  * Run the first job on the queue if there is enough time on the clock and the job queue list is not empty.
  * And move the job to the finished jobs list.
  */

 public void runAJob(){
   if(!myJobInQueue.isEmpty())
   {
        myJobDuration = myJobInQueue.get(0).getDuration();
        if (myJobDuration < myTimeLeft)
        {
            myTimeLeft = myTimeLeft - myJobDuration;
            myFinishedJobs.add(myJobInQueue.get(0));
            System.out.println("A job is running: " + myJobInQueue.get(0).getName());
            myJobInQueue.remove(0);
        }
        else 
        {
            System.out.println("Not enogth running time left, please add time on the clock.");             
        }
   }
   else 
   {
     System.out.println("No pending job on the list.");
   }
}

/**
  * Run all the jobs on the queue in order until it runs out of time.
 */   


public void runAll()
{
  for(int i = 0; myTimeLeft > 0 && myTimeLeft > myJobDuration;i++);
  {
    runJob();

  }
   System.out.println("Job can not be run, not enough time left." );   
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:43:32+00:00Added an answer on June 12, 2026 at 3:43 pm

    Based on your comment, the line System.out.println("Ajob is running"+ myJobInQueue.get(0)); is printing out the object reference for the Job returned by myJobInQueue.get(0). In order for that line to print out meaningful information, just make sure Job has a toString method that returns a String that describes the object.

    I think your second problem is caused by the myTimeLeft variable dropping below myJobDuration, but not below 0. I don’t see where you decrement myTimeLeft when it’s in this window.

    Side Note: Do all jobs have the same duration? If not, then you should probably be using the duration for the next Job instead of a global myJobDuration variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to run my project in windows. Project use android ndk. I install
I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
I try to use the winDBG to debug a dump file. When I run
If I run a server with the port 80, and I try to use
My last question asked for running once without the use of booleans. I decided
I try run jconsole with jar that contains my classes like: cd C:\Program Files\Java\jdk1.6.0_26\bin
I'm getting this error when I try run DB:Rake : ** Invoke db:migrate (first_time)
I try to run a medium sized programm with a lot of external jars
I try to run the following raw query in android, it seems not work
I try to run jsf application in myeclipse using jboss web server and following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.