public class Stack extends LinkList{

public Stack(){

    public void push (Object element)
    {
        insertAtFront (element);
    }

    public Object pop(){
        return removeFromFront();
    }

    public Object peek(){
        return getFirst();
    }
}

Im trying to create a movieApp using pop push and peek method(Stack). But once the class got compiled, it shows a message "illegal start operation" regarding public void push (Object element). Here is my full coding for class Stack

Dani AI

Generated

Quick diagnosis: the compile error comes from declaring methods inside the constructor. In ’s posted Stack the constructor never closes before push/pop/peek are declared, so the compiler reports an "illegal start" style error. Close the constructor brace first, then declare the methods at class scope. Example (non-generic) fix:

public class Stack extends LinkList {
    public Stack() {
        // constructor body (optional)
    }

    public void push(Object element) {
        insertAtFront(element);
    }

    public Object pop() {
        return removeFromFront();
    }

    public Object peek() {
        return getFirst();
    }
}

Additional troubleshooting notes (derived from the thread and ’s comment):

  • Compiler output is authoritative: run javac (or use an IDE) and fix the first error reported — many later errors are cascading.
  • Common causes of "illegal start" errors: a method declared inside another method/constructor, mismatched or missing braces, stray tokens. Use an editor that highlights matching braces to spot these quickly.
  • Ensure the Node class used by LinkList is accessible (same package or public) and that field names (data, next) match its implementation.

Regarding the MovieVCDApp posted later: several syntax and scope issues must be fixed before logic can be tested — mismatched braces, undefined variables (temp, tempStack, out used outside scope), incorrect method syntax (m1.getPrice should be m1.getPrice()), and wrong loop/control structure placements. A safe workflow: 1) reduce main to a tiny test that pushes two simple objects and pops them while printing; 2) restore filters and counters one at a time; 3) fix variable scopes so outputs are declared where they’re later used.

Generics will simplify casting as and suggested. For example:

public class Stack<T> extends LinkList<T> {
    public void push(T element) { insertAtFront(element); }
    public T pop() { return removeFromFront(); }
    public T peek() { return getFirst(); }
}

Fix the constructor/braces first, get a minimal push/pop test compiling, then address the MovieVCDApp logic and optionally convert both LinkList and Stack to generics.

Recommended Answers

All 9 Replies

That doesn't look like the standard Java LinkedList. Is that a class you wrote/were given? If so, we'll need the code for that too.

Yep. And the LinkList codes :

public class LinkList
{
    private Node first;
    private Node last;
    private Node current;

    public LinkList()
    {
        first = null;
        last = null;
        current = null;
    }

    public boolean isEmpty()
    {
        return (first==null);
    }

    public void insertAtFront(Object insertItem)
    {
        Node newNode = new Node(insertItem);

        if (isEmpty())
        {   first = newNode;
            last = newNode;
        }
        else
        {
            newNode.next = first;
            first = newNode;
        }
    }

    public void insertAtBack(Object insertItem)
    {
        Node newNode = new Node(insertItem);

        if (isEmpty())
        {   first = newNode;
            last = newNode;
        }
        else
        {
            last.next = newNode;
            last = newNode;
        }
    }

    public Object removeFromFront()
    {   
        Object removeItem = null;
        if (isEmpty())
        {   
            return removeItem;
        }
        removeItem = first.data;
        if ( first == last)
        {   
            first = null;
            last = null;
        }
        else
            first = first.next;
        return removeItem;
    }

    public Object removeFromBack()
    {   
        Object removeItem = null;
        if (isEmpty())
        {   
            return removeItem;
        }
        removeItem = last.data;
        if ( first == last)
        {   
            first = null;
            last = null;
        }
        else
        {
            current = first;
            while (current.next != last)
                current = current.next;
            last = current;
            last.next = null;
        }         
        return removeItem;
    }

    public Object getFirst()
    {   
        if (isEmpty())
            return null;
        else
        {   
            current = first;
            return current.data;
        }
    }

    public Object getNext()
    {   
        if (current == last)
            return null;
        else
        {
            current = current.next;
            return current.data;
        }               
    }
}

... also, you can't define a method inside another method - you try to define push inside the constructor (that would have been more obvious had you posted the exact complete error message rather than your own summary of it)

This is my App :

import javax.swing.*;

public class MovieVCDApp{

    public static void main(String [] args){

        Stack tS = new Stack();

        for(int x=0; x<2; x++)
        {
            String title = JOptionPane.showInputDialog("Movie Tile:");
            String language = JOptionPane.showInputDialog("Language:");
            double price = Double.parseDouble(JOptionPane.showInputDialog("Price :"));
            int duration = Integer.parseInt(JOptionPane.showInputDialog("Time Duration :"));

            MovieVCD movie= new MovieVCD(title,language,price,duration);
            tS.push(movie);


        }

        String lui= "VCD in Bahasa:" ;
        int count = 0;
        Movie m1=null;

        while(!tS.isEmpty()){
        {

            m1=(Movie)tS.pop();
            if(m1.getLanguage().equalsIgnoreCase("Melayu")){
            {
                String out = "\n" + m1.getTitle() + "\t" + m1.getPrice() + "\t" + m1.getDuration();
                count++;

                System.out.println(temp.toString());
            }
            tempStack=push(m1);


        if(count>0)
        {   
            System.out.println("\n" + out);
        }    
        else 
        {    System.out.println("No movies found");
        }   
    }
        int kira=0;
        while(!tS.isEmpty())

        {
            m1=(Movie)tempStack.pop();
            if (m1.getPrice>15)
            {
               kira++ ;
               tS.push(m1);
            }
            System.out.println("VCDs that costs more than RM15 is about" + kira + "VCDs") ;
            System.out.println("\nTitle :"  + m1.getTitle) ;
        }   
            double sum=0.0;

        for(int k=0;k<2;k++)
        {
            sum=sum+m1[k].getPrice();
        }

        //average price

        double average=sum/2;
        {
        System.out.println("Average price for all DVDs are:" +average);
        }
    }
}
}
}

Assuming that by now you're fixed the original code problem...
Your LinkedList and Stack classes should be updated to support generics so youy can code something like
Stack<Movie> tS = new Stack<Movie>();
which will save you a load of uneccessary casts, and maybe some bugs.
... but if your course hasn't covereed that yet, just set this post aside for later.

I am not usulally using generics but thanks i'll try to apply it to my codes. Btw does generics could be imply on LinkList too?

Yes, your LinkedList class will greatly benefit from using generics.

does generics could be imply on LinkList too?

Yes, absolutely.
ps: You may be able to speed up the learning process by having a quick look at Java's own LinkedList implementation and how that uses generics.

Thank u so much for ur feedback! I like object oriented but it involve with too many methods.. Gotta deal with it. Anyway thanks again :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.