Results 1 to 4 of 4
  1. #1
    sara_magdy is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default polynomial division problem

    i want to divide a polynomial to a polynomial
    i wrote this code but it has a simple error but i don't know what is it anyone can help me
    thanks
    Java Code:
    public class divide {
        private  static ArrayList<Integer> e=new ArrayList<Integer>();
        private static  ArrayList<Integer> a=new ArrayList<Integer>();
        private static  ArrayList<Integer> b=new ArrayList<Integer>();
       private static ArrayList<Integer> dd= new ArrayList<Integer>();
          private static ArrayList<Integer>quotient= new ArrayList<Integer>();
       private static ArrayList<Integer> temp= new ArrayList<Integer>();
       private static ArrayList<Integer> temp2= new ArrayList<Integer>();
          private static int count =0;
        public static void main(String[] args) {
      
                ArrayList<Integer> ff=new ArrayList<Integer>();
    String s=JOptionPane.showInputDialog("enter a");
    String k=JOptionPane.showInputDialog("enter b");
    int ls=s.length();
    int lk=k.length();
    a=convertt(ls,s);
    b=convertt(lk,k);
    
    
             int resultsize=0;
             for(int i=0;i<a.size();i++)
             {
                 temp.add(i,a.get(i));
             }
           
             
                for(int l=0;l<a.size()-b.size()+1;l++)
                {
                     quotient.add(l,temp.get(0)/b.get(0));
                     if(quotient.size()<2)
                     {
                         for(int i=0;i<quotient.size();i++)
                         {
                             temp2.add(i,quotient.get(i));
                         }
                     }
                     else
                     {
                            for(int i=0;i<quotient.size();i++)
                         {
                             temp2.add(i,quotient.get(i));
                         }
                            resultsize=quotient.size();
                            
                       
                       for(int i=0;i<resultsize-1;i++)
                         {
             
                             temp2.remove(i);
                             
                         }
                        
                     }
              int [] terms;
              int power;
              power=temp2.size()+b.size()-1;
              terms=new int [power];
              for(int i=0;i<power;i++)
              {
                  terms[i]=0;
              }
             for(int i=0;i<temp2.size();i++)
             {
                 for(int j=0;j<b.size();j++)
                 {
                     terms[i+j]=terms[i+j]+temp2.get(i)*b.get(j);
                 }
             }
             int r=temp.size();
    int rr=terms.length;
    
    int w;
    if(r>rr)
    {    count=r-rr;
         w=1;
    }
    else if(r<rr)
    {count=rr-r;
     w=2;}
    else
    {    count=0;
    w=0;
    }
    fill(w,terms);
    
    
         if (w==1)   
         for(int i=0;i<temp.size();i++)
         {
             temp.add(i,temp.get(i)-dd.get(i));
         }  
          else if(w==2)
              for(int i=0;i<terms.length;i++)
              {
              temp.add(i,dd.get(i)-terms[i]);
              }
          else
               for(int i=0;i<temp.size();i++)
              {
              temp.add(i,temp.get(i)-terms[i]);
              }
    
                }
                        
             String uu="";
    
    System.out.println(quotient);
    uu=arrange(e.size(), e);
    System.out.println(uu);
    
             }
                
    
    
        
        
    public static ArrayList<Integer>convertt(int l, String O)
    {
        ArrayList<Integer> ARR=new ArrayList<Integer>();
    for (int i=0;i<l;i++)
    {
    String j=O.substring(i, i+1);
    i++;
     ARR.add(Integer.parseInt(j));
    }
    return ARR;   
    }
    public static void fill(int w,int []terms)
            
    { 
        if (w==1)
        {  for(int i=0;i<count;i++)
            {
               
                dd.add(0);
            }
        for(int i=0;i<terms.length;i++)
            dd.add(terms[i]);   
        }
        else if(w==2)
        { for(int i=0;i<count;i++)
            {
                dd.add(0);
            }
        for(int i=0;i<temp.size();i++)
            dd.add(temp.get(i));}
        else
            return;
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: polynomial division problem

    it has a simple error but i don't know what is
    But you do know what it is - it's just that you aren't telling us. It is the cause of the error (and for that matter whether it is simple) that has yet to be determined.

    Does that code compile? If not and there is a compiler message you can't understand, post that message.

    If the program compiles but does something bad or odd when you run it (including throwing a runtime exception) describe what it does as well as what you intended it to do. In the case of runtime exceptions copy and post the entire thing and say which lines of your code it is referring to.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: polynomial division problem

    There are a few things you could do to make the code simpler and less crypic. Most revolve around following Java coding conventions. Classes begin with a capital letter (Divide) and variables should be chosen so that they are descriptive of what they actually stand for. As far as variables go l and O should simply not be used because of the visual ambiguity they cause. Brace placement and indentation should be consistent. Braces around single line blocks of code is also a good idea.

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

    Default Re: polynomial division problem

    This implemenation needs each and every coefficient in the polynomial, even if they're zero ...

    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. int division
    By überfuzz in forum New To Java
    Replies: 5
    Last Post: 03-24-2011, 07:10 PM
  3. API polynomial java
    By mcherkao in forum Advanced Java
    Replies: 5
    Last Post: 09-10-2010, 01:49 PM
  4. Problem with division using doubles
    By chrismanahan in forum New To Java
    Replies: 3
    Last Post: 10-10-2009, 09:26 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
  •