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

  • SEARCH
  • Home
  • 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 7503639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:18:03+00:00 2026-05-29T21:18:03+00:00

package polynomial; /** * * @author Steven */ public class Polynomial { private float

  • 0
package polynomial;

/**
 *
 * @author Steven
 */

public class Polynomial
{
private float data;
protected static Polynomial head;
private Polynomial link;

/**
 * @param args the command line arguments
 */    
public Polynomial(float[] data)
{
    head = null;

    if(data.length == 1)
        head = insertAtFront(head, data[0]);

    for(int i = data.length-1; i >= 0; i--)
    {
        System.out.println(head);
        head = insertAtFront(head, data[i]);
    }
}

public Polynomial(float data, Polynomial link)
{
    this.link = link;
    this.data = data;
}

public static Polynomial add(Polynomial p, Polynomial p2)
{
    if(p.length() > p2.length())
    {
        while(p.length() != p2.length())
            {
                insertAtFront(p2, 0);
            }

        for(Polynomial poly = p; poly != null && p2 != null; poly = poly.link)
        {
            p2.data = (p2.data + poly.data);
            p2 = p2.link;
        }
    }
    else if(p2.length() > p.length())
    {
        while(p2.length() != p.length())
            {
                insertAtFront(p, 0);
            }

        for(Polynomial poly = p; poly != null && p2 != null; poly = poly.link)
        {
            p2.data = (p2.data + poly.data);
            p2 = p2.link;
        }
    }
    else
    {
        for(Polynomial poly = p; poly != null && p2 != null; poly = poly.link)
        {
            p2.data = (p2.data + poly.data);
            p2 = p2.link;
        }
    }
    return p2.head;
}

public float evaluate(float x)
{
    int i = head.length()-1;
    float y = 0;
    for(Polynomial poly = head; poly != null; poly = poly.link)
    {
        if(poly.link == null)
            y += poly.data;
        else
            y += (poly.data * (float)(Math.pow(x, i)));
        i -= 1;
    }
    return y;
}

@Override
public String toString()
{
    int i = 1;
    String polyString = "blank";
    for(Polynomial poly = head; poly != null; poly = poly.link)
    {
        if(polyString.equalsIgnoreCase("blank"))
        {
            if(poly.data != 0)
                    polyString = poly.data + "x^" + (poly.length()-i) + " + ";
            else if(poly.data == 1)
                polyString = "x^" + (poly.length()-i) + " + ";
            else
                polyString = poly.data + "x^" + (poly.length()-i) + " + ";
        }
        else
        {
            if(poly.link == null)
            {
                if(poly.data != 0)
                    polyString = polyString + poly.data;
                else if(poly.data == 1)
                    polyString = polyString + "x";
                else
                    polyString = polyString + poly.data + "x^" + (poly.length()-i);
            }
            else
            {
                if(poly.data != 0)
                    polyString = polyString + poly.data + "x^" + (poly.length()-i) + " + ";
                else if(poly.data == 1)
                    polyString = polyString + "x^" + (poly.length()-i) + " + ";
                else
                    ;
            }
        }
        i = i + 1;
    }
    return polyString;
}

public int length()
{
    int answer = 0;

    for(Polynomial poly = head; poly != null; poly = poly.link)
    {
        answer++;
    }
    return answer;
}

private static Polynomial insertAtFront(Polynomial head, float data)
{
    return new Polynomial(data, head);
}

}

I have a second class that is just a program to run these methods, and when I create a new Polynomial, and run associated methods, everything appears to run successfully. After I create a new Polynomial, the first Polynomial takes the second one’s values. I don’t understand 1. where the first-created polynomial’s values are going and 2. why it’s doing this at all.

  • 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-29T21:18:05+00:00Added an answer on May 29, 2026 at 9:18 pm
    protected static Polynomial head;
    

    your head is declared as static – thus there is only one copy of it for all classes! Once your second Polynomial is created, it overrides the head of the first one.

    You probably want it to be an instance variable and not a class variable.

    Though, doing it will also have its own problems of infinite recursive constructor invokation. To overcome it, you might want to have a different class that holds a Polynomial head as an instance variable, and initialized it only once per list.

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

Sidebar

Related Questions

package org.study.algos; public class Study { public static void main(String[] args) { A a
package com.test01; public class test01 { public static void main(String[] args) { System.out.println(hi); }
package com.mycontainer; public class MyContainer { private static ContainerConfig cConfig; private MyContainer() { }
package portale.interfaccia; public class PageIndex extends FlowPanel { protected Integer prova; private PageCenter center;
package testing.project; public class PalindromeThreeDigits { public static void main(String[] args) { int value
package hw3; public class Main { public static void main(String[] args) { final int
package javaapplication8; public class Main { public static void main(String[] args) { int[] list1
package javaapplication1; import java.io.*; public class Main { /** * @param args the command
package pack; public class sample{ public static void main(String input[]) { NumberFormat numberFormat =
package { import mx.controls.LinkButton; import flash.text.TextLineMetrics; public class multiLineLinkButton extends LinkButton { override protected

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.