Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,149,922 members, 7,806,677 topics. Date: Tuesday, 23 April 2024 at 08:38 PM

Program To Add To Polynomials - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Program To Add To Polynomials (2001 Views)

Java Program To Solve Quadratic Equation-dealwap / Program To Sign The Greek Finance Minister's Signature - Contest For ₦20,000 / A Program To Tell Ur Zodiac Sign (2) (3) (4)

(1) (Reply) (Go Down)

Program To Add To Polynomials by logic101: 10:39pm On Jul 25, 2011
Hi am a computer science student working on a particular question to perform certain functions with polynomials.I have two classes, my monomial class and my polynomial class. I am trying to add two polynomials but i keep gtting a null pointer exception in my add method.Can the house help , Fayimora u seem well infromed can you chip in .



public class Polynimial {
Monomial [] polynomial;;
int degree;


Scanner in= new Scanner(System.in);
// int a is coefficient while int b is the exponent
public Polynimial(int c){

polynomial= new Monomial[c+1];
}

public static void main(String[] args) {

Polynimial c= new Polynimial(1);
c.setPolynomial();
Polynimial d = new Polynimial(1);
d.setPolynomial();
System.out.println(c.plus(d));
}


public void setPolynomial(){
int x=polynomial.length;
int count=0;
System.out.println("please enter in this particular order all the coefficient and exponent "wink;

while(count<x){

int coefficient=in.nextInt();
int exponent=in.nextInt();
polynomial[exponent]=new Monomial(coefficient,exponent);
count++;
}
}

public int getHighestDegree(){
int c= polynomial.length ;
return c;
}

public Polynimial plus(Polynimial c){
Polynimial result = new Polynimial(Math.max(this.getHighestDegree(), c.getHighestDegree()));
int x=0;
for(int i=0;i<result.getHighestDegree();i++){
x=polynomial[i].addMonomials( c.polynomial[i]);
result.polynomial[i].setCoefficient(x);


}
return result;
}


}




public class Monomial {
private int coefficient;
private int exponent;

public Monomial(int c,int e){
coefficient=c;
exponent=e;
}


public int getCoefficient(){
return coefficient;
}


public int getExponent(){
return exponent;
}

public int addMonomials(Monomial c){
int sum =0;
if(this.exponent==c.exponent)
sum= (this.coefficient + c.coefficient);
return sum;
}

public int subPolynomials(Monomial c){
int diff=0;
if(this.exponent==c.exponent)
diff= this.coefficient - c.coefficient;
return diff;
}

public void setCoefficient(int y){
coefficient=y;

}
}
Re: Program To Add To Polynomials by Fayimora(m): 10:49pm On Jul 25, 2011
How are we suposed to enter in the 'polynimial'. Use this

==> 2x3 + 2x2 + 4x + 1 = 0

So how do I key in the values? Also, you got some really bad names here that can cause a brain damage, lol Just kidding but your variable names are confusing,
Re: Program To Add To Polynomials by logic101: 10:54pm On Jul 25, 2011
lol fayimora yup my variable names are u knwww, i would take that into consideration lol, u can key in the values in the main program which is in the polynomial class,
Re: Program To Add To Polynomials by Fayimora(m): 10:56pm On Jul 25, 2011
No i mean during execution. You have this:


What do i key in(use the example above), that instruction is not descriptive enough!
Re: Program To Add To Polynomials by logic101: 11:04pm On Jul 25, 2011
oh alryt e.g lets say u want to add 5x^3 + 2X^2 + 2x +9 and 4x^3 + 2X^2+7x +5
for first polynomial u wld entr the max exponent of each polynomial into its object reference,
so it can be input in to ways frm exponent o which wld be coefficient, exponent

9
0
2
1
2
2
5
3
Re: Program To Add To Polynomials by Fayimora(m): 11:08pm On Jul 25, 2011
Well the bad news is that you have more problems than you thought



 public void setPolynomial(){
int x=polynomial.length;
int count=0;
System.out.println("Please enter in this particular order all the coefficient and exponent "wink;

while(count < x){

int coefficient=in.nextInt();
int exponent=in.nextInt();
polynomial[exponent]=new Monomial(coefficient,exponent);
count++;
}
}


Help me to understand why you have this:
int x=polynomial.length;
Re: Program To Add To Polynomials by logic101: 11:13pm On Jul 25, 2011
its used in the while loop as a condition to input the coefficientss so if the lenght of the polynomial is 6, it needs 6 coefficients starting frm index 0-5
Re: Program To Add To Polynomials by Fayimora(m): 11:18pm On Jul 25, 2011
But i dnt see where you check the length of the polynomial, Weird! Do you mind re-writing this code? They are easier ways to do this i guess. Do you have a requirement or you just have to implement in in anyway.

If you dont have a requirement then the fastest way to do this is with a Map. Do you want to re-write it? Don't worr about not knowing how to use a map I would walk you through the whole process, but you would do the reasoning part.

Reason why i want you to rewrite the code is because what you have here has a lot of redundancies and its very confusing(e.g you called a variable of type Monomial `polynomial`) Its just difficult to comprehend.
Re: Program To Add To Polynomials by Fayimora(m): 4:22am On Jul 26, 2011
Hey sorry was baned by the spambot, Have a look at what i posted which is marked as spam

Re: Program To Add To Polynomials by Nobody: 5:46pm On Aug 02, 2011
Hey i am not sure if this is what u want, but i once did a programming assignment like this in C++ for a polynomial class years ago, u gonna have to decipher it yourself cause i don't really have time, Below is the source code, If i recall, i think i used a lot of container classes and pointers

#include <iostream>
#include <cassert>
#include <math.h>
#include "Poly.h"

using namespace std;

//------------------------Term member functions------------------------------

Term::Term(double c, int e)
  : coeff(c), expon(e)
{
  assert(expon >= 0);
}

Term::Term()
  : coeff(0), expon(0)
{
}

//------------------------Poly member functions------------------------------

Poly::Poly()
{
}

Poly::Poly(Term t)
{
  //want to store non-zero co-efficients only in list of type Term
  if(t.coeff != 0)
    data.push_back(t);
}

list <Term> Poly::getdata()
{
  return data;
}

void Poly::operator += (const Poly &b)
{
  list<Term>::const_iterator bpos = b.data.begin(); //iterator returns pointer to current b element
  list<Term>::iterator apos = data.begin();
  double coefficient;
  int exponent;
  bool flag = 0;

  for( apos = data.begin(); apos!= data.end(); apos++)
    {
      if(apos->expon == bpos->expon && apos->coeff == bpos->coeff)
{
  flag = true;
  bpos++;
}
      else
{
  flag = false;
  break;
}
    }
 
  if( data.size() == b.data.size() && flag == true) //special case
    {
      //cout<<"equal list "<<end;
      list<Term>::iterator tempIter;
     
      for(apos = data.begin(); apos!= data.end();apos++)
{
  exponent = apos->expon;
  coefficient = 2*(apos->coeff);
  Term newterm(coefficient, exponent);
  //have to erase old value so do some pointer saving etc
  tempIter = data.erase(apos);
  apos = tempIter;
  // apos--;
  if(newterm.coeff != 0)
    {
      data.insert(tempIter, newterm);
      apos--;
    }
 
}

    }

  else{
   
    for(apos = data.begin(); bpos!= b.data.end() && apos != data.end()wink
      {

if( apos->expon < bpos->expon)
  {
    apos++;
    //cout<<"not inserting "<<endl;
  }

else if (apos->expon == bpos->expon)
  {
    //cout<<"here 1"<<endl;
    coefficient = apos->coeff + bpos->coeff;
    //cout<<"new coeff is "<<coefficient<<endl;
    exponent = apos->expon;
    //Now construct new data term
    Term newTerm(coefficient, exponent);
    //save current pointers
    list<Term>::iterator apos1 = apos;
    list<Term>::const_iterator bpos1 = bpos;
   
    apos1 = data.erase(apos); //removes current element and returns position of next
    apos = apos1;   //now assign apos back since we used in controlling loop
    apos--;
    bpos++; //advance bpos by 1 to ignore value that has been added to new term
   
    //check to make sure we only insert non-zero coefficients in list
    //cout<<"newterm coefficient is "<<newTerm.coeff<<endl;
    if(newTerm.coeff != 0)
      {
data.insert(apos1,newTerm);
      }
   
  }

else
  {
    data.insert(apos,*bpos);
    //cout<<"inserting "<<endl;
    bpos++;
  }
      }
   
    if(apos == data.end() && bpos != b.data.end()) //not done
      {
while(bpos != b.data.end())
  {
    data.insert(apos,*bpos);
    bpos++;
    //cout<<"here 2"<<endl;
  }
      }
   
  }
}


float Poly::eval(double x) const  //accessor function
{
  float value = 0;
 
  list<Term>::const_iterator epos = data.begin();
 
  while(epos != data.end())
     {
      value = value + (epos->coeff)*( pow(x,epos->expon));
      epos++;
     }
 
  return value;
}

void Poly::print(ostream &out) const
{
  list<Term>::const_iterator pos;

  for(pos = data.begin(); pos!=data.end(); pos++)
    {
      int e = pos->expon;
      double c = pos->coeff;
      if(c > 0)
out << " + ";
      out << c;
      if(e > 0)
out << "x";
      if(e > 1)
out <<"^"<<e;
    }

  out<<endl;
}


//--------------------Poly non-member functions-------------------------

// we're writing << operator for you in terms of the member function print
// you just need to write print

ostream & operator << (ostream &out, const Poly &p)
{
  p.print(out);
  return out;  // all overloaded << ops must return the stream
 
}

Poly operator + (const Poly &a, const Poly &b)  // add two Poly's
{
  Poly p = Poly(); //creates empty polynomial
  p+=a;
  p+=b;
  return p;
 
}

BELOW IS THE HEADER FILE Poly.h


#ifndef POLY_H
#define POLY_H

#include <iostream>
#include <list>

using namespace std;

struct Term
{
  Term();
  Term (double c, int e);
  double coeff;
  int expon;
};

class Poly
{
public:

  Poly ();    // creates 0 polynomial
 
  Poly(Term t);      // creates polynomial with single term t
 
  void operator += (const Poly &b);  // add b to polynomial
 
  float eval (double x) const;     // evaluate polynomial at given value of x

  void print(ostream &out) const;

  list <Term> getdata();   //returns list of current polynomial
 
private:
 
  list <Term> data;
 
  // you may want to add auxiliary member functions here

  void add_data(Term a);

};

// non member functions on Poly:

Poly operator + (const Poly &a, const Poly &b);  // add two Poly's

ostream & operator << (ostream &out,  const Poly &p);

#endif
Re: Program To Add To Polynomials by logic101: 7:47pm On Aug 02, 2011
Thanks for your contribution i appreciat eit

(1) (Reply)

Asp.net GRIDVIEW ERROR Using Vb.net / 17-year-old Boy Sells App For $30 Million To Yahoo Inc / Is Zend Php Certification Obtainable From Nigeria?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 32
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.