Results 1 to 2 of 2
  1. #1
    itsmyday7 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Polynomial multiplication

    Hey guys, I am stuck in making a polynomial multiplication... please help me out.... how to multiply two polynomials...your little help would be much appreciated


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package dataStructures;



    import java.util.Scanner;



    class PolynomialNode{
    int power;
    int coefficient;
    PolynomialNode next;

    public PolynomialNode(int coefficient, int power){
    this.coefficient = coefficient;
    this.power = power;
    next = null;
    }
    public int getX()
    {
    return this.power;
    }
    public int getA()
    {
    return this.coefficient;
    }

    public String toString(){
    return ""+ coefficient + "*X^" + power;
    }

    public static PolynomialNode listSearch(PolynomialNode head, int target_power)
    {
    PolynomialNode cursor;
    if (head == null) return null;
    for (cursor = head; cursor.next != null; cursor = cursor.next)
    if ((cursor.power >= target_power)&&(cursor.next.power < target_power))
    return cursor;

    return cursor;
    }

    }


    class PolynomialEquation{
    PolynomialNode head;

    public PolynomialEquation(){
    head = null;
    }
    public PolynomialNode getHead()
    {
    return this.head;
    }

    public void add (int coefficient, int power){
    if(head!=null)
    {

    if(power>head.power)
    {
    PolynomialNode newN=new PolynomialNode(coefficient,power);
    PolynomialNode tempN=this.head;
    head=newN;
    head.next=tempN;
    }
    }
    PolynomialNode newNode = new PolynomialNode(coefficient,power);
    PolynomialNode pos = PolynomialNode.listSearch(head, power);

    if(pos == null) head = newNode;
    else if(pos.power == power) pos.coefficient+=coefficient;
    else {
    PolynomialNode temp = pos.next;
    pos.next = newNode;
    newNode.next = temp;
    }
    }
    public static PolynomialEquation Addpoly(PolynomialEquation one, PolynomialEquation two)
    {
    PolynomialEquation result=one;
    PolynomialNode temp=two.getHead();
    while(true)
    {
    result.add(temp.coefficient, temp.coefficient);
    if(temp.next==null)return result;
    temp=temp.next;
    }



    }

    public static PolynomialEquation MultiplyPolyByConstant (PolynomialEquation value1, int value2)
    {
    PolynomialEquation result=new PolynomialEquation();
    PolynomialNode temp=value1.getHead();
    while(true)
    {
    result.add(temp.coefficient*value2, temp.power);
    if(temp.next==null)return result;
    temp=temp.next;
    }
    }

    public void printEquation(){
    for(PolynomialNode n = head; n != null; n = n.next){
    System.out.println(n);
    }
    }

    }

    public class ITC322Assignment1 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    PolynomialEquation pe = new PolynomialEquation();



    Scanner input = new Scanner(System.in);
    System.out.println("Please Input your first coordinates:");

    int m = input.nextInt();
    int n = input.nextInt();
    System.out.println("Please Input your second coordinates:");
    int o = input.nextInt();
    int p = input.nextInt();
    System.out.println("Please Input your third coordinates:");
    int q = input.nextInt();
    int r = input.nextInt();
    System.out.println("Please Input your fourth coordinates:");
    int s = input.nextInt();
    int t = input.nextInt();

    pe.add(m,n);
    pe.add(o,p);
    pe.add(q,r);
    pe.add(s,t);

    System.out.println("Printing PE");
    pe.printEquation();

    PolynomialEquation pe1 = new PolynomialEquation();
    pe1.add(2,8);
    pe1.add(1,3);
    pe1.add(2,7);
    System.out.println("Printing PE1: ");
    pe1.printEquation();
    PolynomialEquation added=PolynomialEquation.Addpoly(pe, pe1);
    System.out.println("\nAdditon of Polynomial: ");
    added.printEquation();

    PolynomialEquation multiply = PolynomialEquation.MultiplyPolyByConstant(pe1, 2);
    System.out.println("\nMultiplication by Constant 2: ");
    multiply.printEquation();

    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Polynomial multiplication

    Go check this thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Polynomial Graphing help
    By captain in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 12:53 PM
  2. 2nd Order Polynomial Regression/Best Fit
    By Junki3JJC in forum New To Java
    Replies: 0
    Last Post: 03-29-2011, 01:49 PM
  3. [Help] Polynomial Class in Java
    By AndrewSD in forum New To Java
    Replies: 3
    Last Post: 01-11-2011, 04:24 AM
  4. API polynomial java
    By mcherkao in forum Advanced Java
    Replies: 5
    Last Post: 09-10-2010, 01:49 PM
  5. Help with polynomial program
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •