Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5968169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:03:02+00:00 2026-05-22T20:03:02+00:00

I have a question to complete using Java Polynomials. I’ve set up all my

  • 0

I have a question to complete using Java Polynomials. I’ve set up all my code and it looks to be ok but with my composite method I receive a different output than is expected.

class Polyonmial
{ 
    final static private int mantissa=52;
    final static private double epsilon=Math.pow(2.0,-mantissa);
    private double coefficient=0.0;
    private int power=0;
    private Polynomial successor=null;
    public Polynomial(double coefficient, int power)
    { 
        if (Double.isNaN(coefficient)) return;
        if (Math.abs(coefficient)<epsilon) return;
        if (power<0) return;
        this.coefficient=coefficient;
        this.power=power;
    }

final public double coefficient(int power)
{ 
    if (power<0) return 0.0;
    Polynomial traverser=this;
    do
    { 
        if (traverser.power<power) return 0.0;
        if (traverser.power==power) return traverser.coefficient;
        traverser=traverser.successor;
    }
    while (traverser!=null);
    return 0.0;
}

final public Polynomial composite(Polynomial that)
{
     if (that==null) return null;
     Polynomial thisClone = this.clone();
     Polynomial thatClone = that.clone();
     Polynomial temp = new Polynomial(0.0,0);
     Polynomial result = new Polynomial(0.0,0);

     while(thisClone != null)
     {
         if (thisClone.power == 0) 
         {
             temp.coefficient = 1.0;
             temp.power = 0;
         }
         else
         {
             if (thisClone.power == 1) 
             temp = thatClone;
         }
         //System.out.println("temp:"+temp);

         while(temp != null)
         {
            temp.coefficient = thisClone.coefficient*temp.coefficient;
            result = result.plus(temp);
            temp = temp.successor;
         }
         temp = new Polynomial(0.0,0);
         thisClone=thisClone.successor;
     }
     return result;
}

final public Polynomial[] dividedBy(Polynomial that) 
{ 
    if (that==null) return null;
    if (that.coefficient==0.0) return null;
    Polynomial quotient=new Polynomial(0.0,0);
    Polynomial remainder=this.clone();

    Polynomial traverser = this.clone();
    Polynomial resultoftemp = new Polynomial(0.0, 0);
    Polynomial temp = new Polynomial(0.0, 0);

    double thiscoffe = this.coefficient(this.degree());
    double thatcoffe = that.coefficient(that.degree());

    if(that.coefficient !=0 && that.power !=0)
    {
        quotient.coefficient = thatcoffe / thiscoffe;
        quotient.power = that.power - this.power; 
    }

    while(traverser !=null)
    {
        temp.power = quotient.power + traverser.power;
        temp.coefficient = quotient.coefficient * traverser.coefficient;
        traverser=traverser.successor;

        resultoftemp = resultoftemp.plus(temp);

        remainder = that.minus(resultoftemp);
    }

    Polynomial[] result=new Polynomial[2];
    result[0]=quotient;
    result[1]=remainder;
    return result;
}

final public Polynomial integrate()
{ 
    if (this.coefficient==0.0) return new Polynomial(0.0,0);
    Polynomial result=this.clone();

    Polynomial temp = new Polynomial(0.0, 0);
    Polynomial rstemp = new Polynomial(0.0, 0);

    while(result!=null)
    {
        rstemp.power = result.power + 1;
        rstemp.coefficient = result.coefficient / (result.power +1);
        result = result.successor;
        temp = temp.plus(rstemp);
    }
    return result;
}

final public Polynomial minus(Polynomial that)
{ 
    if (that==null) return null;
    if (this.equals(that)) return new Polynomial(0.0,0);
    Polynomial result=this.clone();
    if (that.coefficient==0.0) return result;
    Polynomial traverser=that;
    do
    { 
        add(result,-traverser.coefficient,traverser.power);
        traverser=traverser.successor;
    }
    while (traverser!=null);
    return result;
}

final public int powerMax()
{ 
    int max=Integer.MIN_VALUE;
    Polynomial traverser=this;
    do
    {
        if (max<traverser.power) max=traverser.power;
        traverser=traverser.successor;
    }
    while (traverser!=null);
    return max;
}

final public int powerMin()
{ 
    int min=Integer.MAX_VALUE;
    Polynomial traverser=this;
    do
    {
        if (min>traverser.power) min=traverser.power;
        traverser=traverser.successor;
    }
    while (traverser!=null);
    return min;
}

}

In my expected output file I am supposed to receive this:

(-1.0*X^1+1.0).composite(-1.0*X^1+1.0)=
1.0*X^1

But instead I receive:

(-1.0*X^1+1.0).composite(-1.0*X^1+1.0)=
1.0*X^1+1.0

Any tips or help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:03:03+00:00Added an answer on May 22, 2026 at 8:03 pm

    I have slightly changed your composite method as so (logically equivalent, brace moves and println additions):

    final public Polynomial composite(Polynomial that) {
        if (that==null) return null;
        Polynomial thisClone = this.clone();
        Polynomial thatClone = that.clone();
        Polynomial temp = new Polynomial(0.0,0);
        Polynomial result = new Polynomial(0.0,0);
    
        while(thisClone != null) {
            System.out.println("composite with term degree: " + thisClone.power);
            if (thisClone.power == 0) {
                temp.coefficient = 1.0;
                temp.power = 0;
            } else if (thisClone.power == 1) {
                temp = thatClone;
            } else {
                for(int i=2; i<=thisClone.power; i++) {
                    temp = temp.plus(thatClone.times(thatClone));
                }
            }
            System.out.println("temp heading in: " + temp);
    
            while(temp != null) {
                temp.coefficient = thisClone.coefficient*temp.coefficient;
                result = result.plus(temp);
                temp = temp.successor;
            }
            System.out.println("result so far: " + result);
            temp = new Polynomial(0.0,0);
            thisClone=thisClone.successor;
        }
        return result;
    }
    

    and for the example (-1.0*X^1+1.0).composite(-1.0*X^1+1.0), this is the output:

    composite with term degree: 1
    temp heading in: -1.0*X^1+1.0
    result so far: 1.0*X^1
    composite with term degree: 0
    temp heading in: 1.0
    result so far: 1.0*X^1+1.0

    Am I correct to believe that the first “result so far” should be “-1.0*X^1-1.0”?

    If so, let’s examine the loop:

            while(temp != null) {
                temp.coefficient = thisClone.coefficient*temp.coefficient;
                result = result.plus(temp);
                temp = temp.successor;
            }
    

    I think this is your problem: result = result.plus(temp) is adding not just the “current term” to temp, but also the successor term. But then you loop on temp by setting it equal to its successor and do it again!

    I think the solution is something like this (first calculate all temp terms, then update result):

        Polynomial looptemp = temp;
        while(looptemp != null) {
            looptemp.coefficient = thisClone.coefficient*looptemp.coefficient;
            looptemp = looptemp.successor;
        }
        result = result.plus(temp);
    

    At the very least, it works for the single example I tried.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Question class: class Question { public int QuestionNumber { get; set;
I am java developer, (using Spring-MVC) never worked much on javascript, But I would
(See this question in ServerFault ) I have a Java client that uses Socket
Here is my question I have a WCF service that I implemented using a
very simple question, i have this: (only a part of the complete rss file)
Noob question, using Win7 64-bit, Clojure 1.2.0, Java 1.6.0_22 When I start clojure from
This might be duplicate of this and this question . I have a java
I have a web application written using Java servlets. I am using Tomcat 7
The qustion was modified by me to show a complete picture. I have a
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.