Welcome to Dream.In.Code
Become a Java Expert!

Join 149,507 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,331 people online right now. Registration is fast and FREE... Join Now!




Linked List To Evaluate Polynomials Problem

 
Reply to this topicStart new topic

Linked List To Evaluate Polynomials Problem

Samer
10 Jul, 2007 - 05:13 AM
Post #1

New D.I.C Head
*

Joined: 7 Jul, 2007
Posts: 11


My Contributions
guys I have to write a code and create an applet that asks the user to enter a seq of doubles and give a value for X0 and the program should create a a polynomial and evaluate it for the given X0.

For example: if the user enter 4.3 2 6.8 8 -3 1 5 0, the polynomial is 4.3X^2 + 6.8X^8 -3X + 5.

The compilation goes fine, but when I execute, it gives me the following exception:

java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.

CODE
public class DoubleNode
{
    private double data;
    private DoubleNode link;

    public DoubleNode(double d, DoubleNode l)
    {
        data = d;
        link = l;
    }
    public DoubleNode(double d)
    {
        data = d;
    }
    
    // access to fields
    public double getData()
    {
        return data;
    }

    public DoubleNode getLink()
    {
        return link;
    }

    // modify fields
    public void setData(double dd)
    {
        data = dd;
    }

    public void setLink(DoubleNode dl)
    {
        link = dl;
    }
}



CODE
public class DoubleSeq implements Cloneable
{

    private DoubleNode head;
    private int listcount;
    private DoubleNode link;
    private DoubleNode current;


    public DoubleSeq()
    {
        head = null;
        listcount = 0;
    }
    
    public DoubleSeq(double data)
    {
        head = new DoubleNode(data);
        listcount = 1;
    }
    
    public DoubleNode getHead()
    {
        return head;
    }
    
    public DoubleNode getCurrent()
    {
        return current;
    }
        
    public void addAfter(double element)
    {
        link = new DoubleNode(element, link);
    }
    
    public void addAll(DoubleSeq ds)
    {
        DoubleNode cursor = head;
        while(cursor.getLink() != null)
            cursor = cursor.getLink();
            
        cursor.setLink(ds.getHead());
        int dscount = 0;
        while(cursor.getLink() != null)
        {
            dscount++;
        }
        listcount = listcount + dscount;
    }
    
    public void addBefore(double element)
    {
        DoubleNode cur = head;
        while(cur.getData() == element)
        {
            cur = new DoubleNode(element, cur);
        }
    }
    
    public void advance()
    {
            current = current.getLink();
    }
    
    public DoubleSeq catenation(DoubleSeq s1, DoubleSeq s2)
    {
        DoubleSeq s;
        if(s1 == null)
            s = s2;
        if(s2 == null)
            s = s1;
        else
        {
            s = s1;
            while((s.getCurrent()).getLink() != null)
                s.advance();
                
            (s.getCurrent()).setLink(s2.getHead());
        }
        return s;
    }
    
    public DoubleSeq clone()
    {
        if (head == null)
            return null;
        else
        {
            DoubleSeq s = new DoubleSeq(head.getData());
            DoubleNode sc = s.getHead();
            DoubleNode nc = sc;
            DoubleNode current = head;
            while (current.getLink() != null)
            {
                current = current.getLink();
                nc.setLink(new DoubleNode(current.getData()));
                nc = nc.getLink();
            }
            return s;
        }
    }
    
    public boolean isCurrent()
    {
        if(current != null)
            return true;
        else
            return false;
    }
    
    public void removeCurrent()
    {
        DoubleNode temp;
        for(temp = head; temp.getLink() != current; temp = temp.getLink());
        temp.setLink(current.getLink());     
    }
    
    public int size()
    {
        return listcount;
    }
    
    public void start()
    {
        current = head;
    }
    
}


CODE
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Seq extends Applet
{
    DoubleSeq DS = new DoubleSeq();
    double answer = 0;
    Label Header1 = new Label("You are supposed to provide a list of double numbers and a value of Xo.");
    Label Label1 = new Label("The list:");
    TextField list = new TextField(15);
    Label Label2 = new Label("Xo:");
    TextField X = new TextField(5);
    Button eval = new Button("Evaluate Your Polynomial");
    TextArea output = new TextArea(10,70);
    
    public void init()
    {
        setSize(new Dimension(500,400));
        add(Header1);
        add(Label1);
        add(list);
        add(Label2);
        add(X);
        add(eval);
        add(output);
        EvaluateListener el = new EvaluateListener();
        eval.addActionListener(el);
    }
    class EvaluateListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String s = list.getText();
            double Xo = Double.parseDouble(X.getText());
            StringTokenizer tokenizer = new StringTokenizer(s," ");
            String s1;
            while(tokenizer.hasMoreTokens())
            {

                DS.addAfter(Double.parseDouble(tokenizer.nextToken()));
    
                DS.advance();
            }
            DS.start();
            while(DS.getHead()!=null)
            {
                double C = DS.getCurrent().getData();
                DS.advance();
                double E = DS.getCurrent().getData();
                DS.advance();
                answer = answer + C * Math.pow(Xo, E);
            }
            output.append("The value of your polynomial is: "  + answer);
        }
    }
}

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Linked List To Evaluate Polynomials Problem
10 Jul, 2007 - 06:04 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
hi, how do you execute the code?
After you compile then you hit the Execute button?
If that is the case then you should not execute it, just make an html page where the applet will be loaded and just view it through a web browser.
User is offlineProfile CardPM
+Quote Post

Samer
RE: Linked List To Evaluate Polynomials Problem
10 Jul, 2007 - 06:43 AM
Post #3

New D.I.C Head
*

Joined: 7 Jul, 2007
Posts: 11


My Contributions
can you help me adding the applet to an html page?
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Linked List To Evaluate Polynomials Problem
10 Jul, 2007 - 07:12 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
I'll be happy to help, ok
this is the code of the html page:
CODE
<html>

    <head>
            <applet code="Seq.class" height=500 width=500 >

            </applet>
            
    </head>






    <body>
            

    </body>


</html>

Put this code in a text editor and save it as an html page.
You should save it in the same folder where the Seq.class file is which should be where the Seq.java file is.
Seq.class is made when you compile the Seq.java
so you do not have to create it.
Then all you have to do is view the html page with a web browser.
Please post again if you run into some problems.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 07:19PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month