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

The Archive Base Latest Questions

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

I need some help. I am doing test-driven development. These are the tests: @Test

  • 0

I need some help. I am doing test-driven development. These are the tests:

@Test public void add01() throws TError { assertEquals(new Term(10,5),  new Term(4,5).add(new Term(6,5))); }
@Test public void add02() throws TError { assertEquals(new Term(6,5),   new Term(0,5).add(new Term(6,5))); }
@Test public void add03() throws TError { assertEquals(new Term(2,5),   new Term(-4,5).add(new Term(6,5))); }
@Test public void add04() throws TError { assertEquals(new Term(10,0),  new Term(4,0).add(new Term(6,0))); }
@Test public void add05() throws TError { assertEquals(new Term(4,5),   new Term(4,5).add(new Term(0,5))); }
@Test public void add06() throws TError { assertEquals(new Term(-2,5),  new Term(4,5).add(new Term(-6,5))); }

@Test (expected = IncompatibleTerms.class)
public void add07() throws TError { t = new Term(4,5).add(new Term(6,0)); }

@Test (expected = CoefficientOverflow.class)
public void add08() throws TError { t = new Term(min,4).add(new Term(-1,4)); }

@Test public void add09() throws TError { assertEquals(new Term(min,4), new Term(min+1,4).add(new Term(-1,4))); }
@Test public void add10() throws TError { assertEquals(new Term(-11,4), new Term(-10,4).add(new Term(-1,4))); }
@Test public void add11() throws TError { assertEquals(new Term(-2,4),  new Term(-1,4).add(new Term(-1,4))); }
@Test public void add12() throws TError { assertEquals(new Term(-1,4),  new Term(0,4).add(new Term(-1,4))); }
@Test public void add13() throws TError { assertEquals(Term.Zero,       new Term(1,4).add(new Term(-1,4))); }
@Test public void add14() throws TError { assertEquals(new Term(9,4),   new Term(10,4).add(new Term(-1,4))); }
@Test public void add15() throws TError { assertEquals(new Term(max,4), new Term(max-1,4).add(new Term(1,4))); }

@Test (expected = CoefficientOverflow.class)
public void add16() throws TError { t = new Term(max,4).add(new Term(1,4)); }

//Using domain-specific knowledge:  addition should be commutative

@Test public void add17() throws TError { assertEquals(new Term(-1,2).add(new Term(1,2)),       new Term(1,2).add(new Term(-1,2))); }
@Test public void add18() throws TError { assertEquals(new Term(min+1,2).add(new Term(-1,2)),   new Term(-1,2).add(new Term(min+1,2))); }
@Test public void add19() throws TError { assertEquals(new Term(min,2).add(Term.Zero),      Term.Zero.add(new Term(min,2))); }
@Test public void add20() throws TError { assertEquals(new Term(min,0).add(Term.Unit),      Term.Unit.add(new Term(min,0))); }
@Test public void add21() throws TError { assertEquals(new Term(max,0).add(Term.Zero),      Term.Zero.add(new Term(max,0))); }
@Test public void add22() throws TError { assertEquals(new Term(max-1,2).add(new Term(1,2)),    new Term(1,2).add(new Term(max-1,2))); }
@Test public void add23() throws TError { assertEquals(new Term(max,2).add(new Term(min,2)),    new Term(min,2).add(new Term(max,2))); }

This is my add method so far with the help of “Andrej Gajduk”.

public Term add(Term that) throws CoefficientOverflow, IncompatibleTerms {
    if (this.expo != that.expo) throw new IncompatibleTerms();
    return new Term (this.coef + that.coef,expo);
}

These are the tests that fail = 2, 5, 8, 12, 16, 19, 21. But I have doubts about tests 17-23; I know some of them pass but I don’t think they should because that is like x + y = y + x, but I am not sure how to implement it.

I need some help/guidance on why this tests are failing.

For tests 8 and 16 I have tried the code below. It does the job but then it creates more errors on the other tests. So then all together the fails become 2, 5, 8, 12, 16, 19, 21 + 20, 23 (two extra fails)

if (this.coef == Integer.MIN_VALUE || that.coef == Integer.MIN_VALUE) throw new CoefficientOverflow();
if (this.coef == Integer.MAX_VALUE || that.coef == Integer.MAX_VALUE) throw new CoefficientOverflow();
  • 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:53:11+00:00Added an answer on June 14, 2026 at 10:53 am

    Your equals method is incorrect. Please update it as below:

    @Override
    public boolean equals(Object that) {
        if (that == null) {
            return false;
        }else if (this.getClass() != that.getClass()) {
            return false;
        }else if (this == that) {
            return true;
        }else{
            Term term = (Term) that;
            if ((term.coef) == (this.coef) && (term.expo) == (this.expo)) {
                return true;
            }
        }
        return false;
    }
    

    To make you most of the test cases pass, update the add method as below:

    public Term add(Term that) throws IncompatibleTerms, CoefficientOverflow{
        //handle addition with term zero
            if(this.equals(Term.Zero)){
            return that;
        }else if(Term.Zero.equals(that)){
            return this;
        }
    
        if (this.expo != that.expo) throw new IncompatibleTerms();
        if((this.coef>= 0 && that.coef >= 0 && this.coef + that.coef <0) ||
                (this.coef<= 0 && that.coef <= 0 && this.coef + that.coef >0)){
                             throw new CoefficientOverflow();
            }    
        return new Term (this.coef + that.coef,expo);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am VERY new to JSF, and I need some help doing, what I
I am trying to learn by doing and need some help. So formtastic can
I am trying to extract a substring. I need some help with doing it
Hi so i'm doing some test prep and i need to figure out parts
Need some help on a script I am doing... what I want to accomplish
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help with Activerecord Querying in a has_many :through association. Model: Job class
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented

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.