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 8839751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:18:29+00:00 2026-06-14T10:18:29+00:00

I have a Term class to define a polynomial: public class Term { final

  • 0

I have a Term class to define a polynomial:

public class Term
{
    final private int coef;
    final private int expo;

    private static Term zero, unit;

    static
    {
        try
        {
            zero = new Term(0, 0); // the number zero
            unit = new Term(1, 0); // the number one
        }
        catch (Exception e)
        {
            // constructor will not throw an exception here anyway
        }
    }

    /**
     * 
     * @param c
     *            The coefficient of the new term
     * @param e
     *            The exponent of the new term (must be non-negative)
     * @throws NegativeExponent
     */
    public Term(int c, int e) throws NegativeExponent
    {
        if (e < 0)
            throw new NegativeExponent();
        coef = c;
        expo = (coef == 0) ? 1 : e;
    }

    final public static Term Zero = zero;
    final public static Term Unit = unit;

    public boolean isConstant()
    {
        boolean isConst = false;
        if (this.expo == 0)
        {
            isConst = true;
        }
        return isConst;
    }
}

And I have the JUnit test as follows:

 /*
 *      const1      isConstant(zero)        =>      true    (0,0)
 *      const2      isConstant(unit)        =>      true    (1,0)
 *      const3      isConstant(0,5)         =>      true
 *      const4      isConstant(5,1)         =>      false
 */

 @Test 
 public void const1() throws TError { assertTrue(Term.Zero.isConstant()); }

 @Test 
 public void const2() throws TError { assertTrue(Term.Unit.isConstant()); }

 @Test 
 public void const3() throws TError { assertTrue(new Term(0,5).isConstant()); }

 @Test 
 public void const4() throws TError { assertFalse(new Term(5,1).isConstant()); }

Tests 2 and 4 pass as they should, but Tests 1 and 3 come up as failures and I cannot work out why, “Zero” is defining the polynomial as (0,0) and the other is defining it as (0,5). So by my thinking, the 1st one should give a green tick and the 3rd test should give a red cross as it has 5 as the exponent.

Any ideas?

  • 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-06-14T10:18:30+00:00Added an answer on June 14, 2026 at 10:18 am

    Please review your constructor:

    public Term(int c, int e) throws NegativeExponent{
        if (e < 0) throw new NegativeExponent();
        coef = c;
        expo = (coef == 0) ? 1 : e;
    }
    

    When coef == 0, then exp is assigned with 1.

    This makes zero as (0,1) not (0,0). this is the reason for test outcome as below:

        const1 -> false as expo = 1-->failure as expecting true
        const2 -> true as expo = 0 -->success as expecting true
        const3 -> false as expo = 5.-->failure as expecting true
        const4 -> false as expo = 1.-->success as expecting false
    

    EDIT: To reverse fix your code to make the test cases pass, I think you may update your constructor as below:

    public Term(int c, int e) throws NegativeExponent{
        if (e < 0) throw new NegativeExponent();
        coef = c;
        expo = (c == 0 && e != 0) ? 0 : e;
    }
    

    Here, I have updated the constructor to set expo as 0, if coef is 0 as any expo for coef 0 is immaterial.

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

Sidebar

Related Questions

I have to do a final project for my computational linguistics class. We've been
so in php, you don't have define the field in a class first like
I have some class file.h where public: bool frameSendingFinished; is defined. So in class
I want to read multiple objects (my own class Term) that I have output
i have a class: Class MyClass { void myMember(); ///code etc private: QFile fileMBox;
I have an entity with the following attributes public class DimensionElement { public string
Long term lurker, first time poster here. I have written a class to model
I have heard the term connection pooling and looked for some references by googling
I have a vector of objects (objects are term nodes that amongst other fields
I have text like this format term: 156^^^:^^59 datainput Or term: 156^^^:59 datainput or

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.