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

  • Home
  • SEARCH
  • 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 8734633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:52:57+00:00 2026-06-13T09:52:57+00:00

I have a program that implements a print queue. I have a StackClass that

  • 0

I have a program that implements a print queue. I have a StackClass that finds stackjob and stackcycle, but when I try to print these values, I get weird results, like hashtags. I know that I need to override toString(), but I can’t get my code to compile when I add in the extra code.

I can’t add the code inside my StackClass, because stackjob and stackcycle do not exist there. They exist in the PrintQueue file, which is my main method file. However, I cannot add the code within the main method because I get this error: PrintQ.java:10: ‘;’ expected public String toString() { ^ and I can’t add the code outside of the main method because I get: cannot find symbol error for both stackjob and stackcycle. Am I missing something?

I did the same in my Cycle class for jobNumber and cycleNumber and it works perfectly:

@Override
public String toString() 
{
    StringBuilder sb = new StringBuilder();
    sb.append(jobNumber + " ");
    sb.append(cycleNumber);
    return sb.toString();
}

Below, stackjob and stackcycle are the values I need to override toString for.. any advice?

StackClass<Integer> stackjob   = new StackClass();
StackClass<Integer> stackcycle = new StackClass();

Here is my StackClass

       public class StackClass<T> implements StackADT<T>
       {
       private int maxStackSize;  //variable to store the
                           //maximum stack size
       private int stackTop;      //variable to point to
                           //the top of the stack
        private T[] list;  //array of reference variables
   //Create an array of the size 100 to implement the stack.
   //Postcondition: The variable list contains the base
   //               address of the array, stackTop = 0,
   //               and maxStackSize = 100.
  public StackClass()
  {
     maxStackSize = 100;
     stackTop = 0;         //set stackTop to 0
     list = (T[]) new Object[maxStackSize]; //create the array
  }//end default constructor

   //Constructor with a parameter
   //Create an array of the size stackSize to implement the
   //stack.
   //Postcondition: The variable list contains the base
   //               address of the array, stackTop = 0,
   //               and maxStackSize = stackSize.
  public StackClass(int stackSize)
  {
     if (stackSize <= 0)
     {
        System.err.println("The size of the array to "
                         + "implement the stack must be "
                         + "positive.");
        System.err.println("Creating an array of the size 100.");

        maxStackSize = 100;
     }
     else
        maxStackSize = stackSize; //set the stack size to
                                  //the value specified by
                                  //the parameter stackSize
     stackTop = 0;    //set stackTop to 0
     list = (T[]) new Object[maxStackSize]; //create the array
  }//end constructor

   //Method to initialize the stack to an empty state.
   //Postcondition: stackTop = 0
  public void initializeStack()
  {
     for (int i = 0; i < stackTop; i++)
        list[i] = null;

     stackTop = 0;
  }//end initializeStack

   //Method to determine whether the stack is empty.
   //Postcondition: Returns true if the stack is empty;
   //               otherwise, returns false.
  public boolean isEmptyStack()
  {
     return (stackTop == 0);
  }//end isEmptyStack

   //Method to determine whether the stack is full.
   //Postcondition: Returns true if the stack is full;
   //               otherwise, returns false.
  public boolean isFullStack()
  {
     return (stackTop == maxStackSize);
  }//end isFullStack

   //Method to add newItem to the stack.
   //Precondition: The stack exists and is not full.
   //Postcondition: The stack is changed and newItem
   //               is added to the top of stack.
   //               If the stack is full, the method
   //               throws StackOverflowException
  public void push(T newItem) throws StackOverflowException
  {
     if (isFullStack())
        throw new StackOverflowException();

     list[stackTop] = newItem; //add newItem at the
                              //top of the stack
     stackTop++;               //increment stackTop
  }//end push

   //Method to return a reference to the top element of
   //the stack.
   //Precondition: The stack exists and is not empty.
   //Postcondition: If the stack is empty, the method
   //               throws StackUnderflowException;
   //               otherwise, a reference to the top
   //               element of the stack is returned.
  public T peek() throws StackUnderflowException
  {
     if (isEmptyStack())
        throw new StackUnderflowException();

     return (T) list[stackTop - 1];
  }//end peek

   //Method to remove the top element of the stack.
   //Precondition: The stack exists and is not empty.
   //Postcondition: The stack is changed and the top
   //               element is removed from the stack.
   //               If the stack is empty, the method
   //               throws StackUnderflowException
  public void pop() throws StackUnderflowException
  {
     if (isEmptyStack())
        throw new StackUnderflowException();

     stackTop--;       //decrement stackTop
     list[stackTop] = null;
  }//end pop

}

And here is my PrintQ :

             import java.io.*;
               import java.util.*;

            public class PrintQ {


  public static void main(String args[]) throws FileNotFoundException {

     String job1;

     int firstComma;
     int secondComma;

     QueueClass<Cycle> list= new QueueClass(100);
     QueueClass<Integer> cyclelist= new QueueClass(100);
     Cycle currentcycle= new Cycle();
     Cycle priorityCycle= new Cycle();
     Cycle Scycle= new Cycle();

     try{

        FileInputStream fstream = new FileInputStream("C:\\Users\\Whitney\\Desktop\\QueueIn.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        job1=br.readLine();
        while ((strLine = br.readLine()) != null)   {
           switch(job1.charAt(0)) {

              case 'q': 
                 {
                    System.out.println("loop q");
                    firstComma=job1.indexOf(',');
                    secondComma=job1.lastIndexOf(',');
                    currentcycle.jobNumber=Integer.parseInt(job1.substring(firstComma+1,secondComma));
                    currentcycle.cycleNumber=Integer.parseInt(job1.substring(secondComma+1));
                    cyclelist.addQueue(currentcycle.cycleNumber);
                    list.addQueue(currentcycle);

                    while(currentcycle.cycleNumber > 0)
                    {

                       System.out.println(currentcycle.jobNumber + " "  + currentcycle.cycleNumber);
                       currentcycle.cycleNumber--;
                    }
                    //list.print();
                    break;
                 }

              case 'p': 
                 {    System.out.println("loop priority");                
                    firstComma=job1.indexOf(',');
                    secondComma=job1.lastIndexOf(',');
                    priorityCycle.jobNumber=Integer.parseInt(job1.substring(firstComma+1,secondComma));
                    priorityCycle.cycleNumber=Integer.parseInt(job1.substring(secondComma+1));
                    cyclelist.addQueue(priorityCycle.cycleNumber);
                    list.priorityinsert(priorityCycle);
                    while(priorityCycle.cycleNumber > 0)
                    {
                       System.out.println(priorityCycle.jobNumber + " "  + priorityCycle.cycleNumber);
                       priorityCycle.cycleNumber--;

                    }
                    System.out.println(cyclelist);                     
                    //list.print();

                    break;
                 }

              case 's': 
                 {System.out.println("loop s");
                    firstComma=job1.indexOf(',');
                    Scycle.cycleNumber=Integer.parseInt(job1.substring(firstComma+1));
                    cyclelist.addQueue(Scycle.cycleNumber);                       
                    list.addQueue(Scycle);
                    while(Scycle.cycleNumber > 0)
                    {
                       System.out.println(Scycle.jobNumber + " "  + Scycle.cycleNumber);
                       Scycle.cycleNumber--;

                    }


                    break;
                 }

              case 'h': 
                 {
                    System.out.println("loop halt");
                    StackClass<Integer> stackjob= new StackClass();
                    StackClass<Integer> stackcycle= new StackClass();



                    job1=(String) br.readLine();
                    //list.print();

                    System.out.println(stackjob.toString() +" " + stackcycle.toString());


                    while((strLine = br.readLine()) != null){

                       switch(job1.charAt(0)) {

                          case 'q':
                             {

                                firstComma=job1.indexOf(',');
                                secondComma=job1.lastIndexOf(',');
                                currentcycle.jobNumber=Integer.parseInt(job1.substring(firstComma+1,secondComma));
                                currentcycle.cycleNumber=Integer.parseInt(job1.substring(secondComma+1));
                                stackjob.push(currentcycle.jobNumber);
                                stackcycle.push(currentcycle.cycleNumber);
                                System.out.println("hi");
                                System.out.println(currentcycle.jobNumber + " "  + currentcycle.cycleNumber);
                                //list.print();
                                break;
                             }
                          case 'p':
                             {

                                firstComma=job1.indexOf(',');
                                secondComma=job1.lastIndexOf(',');
                                priorityCycle.jobNumber=Integer.parseInt(job1.substring(firstComma+1,secondComma));
                                priorityCycle.cycleNumber=Integer.parseInt(job1.substring(secondComma+1));
                                stackjob.push(priorityCycle.jobNumber);
                                stackcycle.push(priorityCycle.cycleNumber);
                                System.out.println(priorityCycle.jobNumber-- + " "  + priorityCycle.cycleNumber--);
                                break;
                             }

                          case 's':
                             {
                                firstComma=job1.indexOf(',');
                                secondComma=job1.lastIndexOf(',');
                                Scycle.cycleNumber=Integer.parseInt(job1.substring(secondComma+1));
                                stackjob.push(0);
                                stackcycle.push(Scycle.cycleNumber);
                                System.out.println(Scycle.jobNumber + " " + Scycle.cycleNumber); 
                                break;
                             }

                          case 'h': 
                             {
                                System.out.println("Halt - " + list);
                                continue;
                             }

                       }
                       job1=(String) br.readLine();

                    }   
                    //System.out.println();

                    while((stackjob.isEmptyStack()==false) || (stackcycle.isEmptyStack()==false)) 
                    { 
                       int printjob;
                       int printcycle;
                       Object peek;

                       printjob=stackjob.peek();
                       printcycle=stackcycle.peek();
                       stackcycle.pop();
                       stackjob.pop(); 
                       System.out.println("Job Number: "+printjob+" "+"Cycles Remaining: "+printcycle);
                    } 


                    continue;
                 }           
           }
           job1=br.readLine();
        }
        in.close();
     }
        catch (Exception e){//Catch exception if any
           System.err.println("Error: " + e.getMessage());
        }


     Cycle whilecurrent= new Cycle();

     while(list.isEmptyQueue()==false) 
     {
        whilecurrent=list.front();
        int whilecurrentcycle= cyclelist.front();
        //list.print();
        //System.out.println();

        //while(whilecurrentcycle != 0)
        //{
           //System.out.println();//"loop "+whilecurrentcycle--);  
        //}
        //System.out.println();

        //System.out.println(whilecurrent);

        cyclelist.deleteQueue();
        list.deleteQueue();   
     }  

     list.print();

  }

}

  • 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-13T09:52:58+00:00Added an answer on June 13, 2026 at 9:52 am

    Loop through the stack array, building up a string:

    public String toString() {
      StringBuilder output = new StringBuilder("[");
      if ( stackTop > 0 ) {
         for (int i = stackTop - 1; i >= 0; i--) {
            if ( i == 0 ) {
               output.append(list[i].toString());
            }
            else {
               output.append(list[i].toString() + "|");
            }
         }
      }
      output.append("]");
      return output.toString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have program that requires Python 3, but I develop Django and it uses
I have a program that encrypts files, but adds the extension .safe to the
I have a vb.net 2.0 program, that has a batch/queue routine to execute tasks
I have a program that is been implemented in C++ which I now want
I have program that has a variable that should never change. However, somehow, it
I have program that runs fast enough. I want to see the number of
I have a program that gets a JSON from the server using getJSON and
I have a program that saves an image in a local directory and then
I have a program that reads from a file that grabs 4 bytes from
I have a program that I want to distribute, without giving the source code

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.