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

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.