Search code examples
javainterfacequeueadtabstract-data-type

Code seems out-of-scope, How to fix? (Simple Queue-ADT program)


Im not sure how to exactly explain this problem, but Im pretty sure Im making a very simple mistake that can be corrected quite quickly. Also, I thought it would be more convenient if this was shown off as a screenshot. The first two tabs are my interface and error catching classes.

As you can see, the code for methods to use in my Queue ADT seems to be out of scope. So I can move on and complete this bit of coursework, can someone explain to me why it is out of scope?

Thanks for any help!


Solution

  • You declare those variables in main method, so only main local scope know them. Move the declaration to class level

    public class QueueProgram {
        private static int queuesize = 10;
    
        public static void main(String[] args) {
        }
    }
    

    Note I declared queuesize as static since the main uses it. Another option is to create getters and setters and call them with an instance of QueueProgram

    public class QueueProgram {
        private int queuesize = 10;
    
        public int getQueuesize() {
            return queuesize;
        }
    
        public void setQueuesize(int size) {
            queuesize = size;
        }
    
        public static void main(String[] args) {
            QueueProgram program = new QueueProgram();
            program.getQueuesize(); // return 10;
            program.setQueuesize(5);
            program.getQueuesize(); // now it is 5;
       }
    }