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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:53:09+00:00 2026-05-13T12:53:09+00:00

i’m trying to write a small java class. I have an object called BigNumber.

  • 0

i’m trying to write a small java class. I have an object called BigNumber. I wrote method that add two positive numbers, and other method that subract two also positive numbers.

Now i want them to handle negative numbers. So the i wrote couple of ‘if’ statements eg.

if (this.sign == 1 /* means '+' */) {
    if (sn1.sign == 1) {
        if (this.compare(sn1) == -1 /* means this < sn1 */ ) return sn1.add(this);
        else return this.add(sn1);
    }

etc.

Unfortunately the code looks just ugly. Like a bush of if’s and elses. Is there a better way to write that kind of code?

Edit
i can’t just do this.add(sn1) beacuse sometimes i want to add positive number to negative one or negitve to negative. But add can handle only positive numbers. So i have to use basic math and for example: instead of add negative number to negative number i add this.abs() (absolute value of number) to sn1.abs() and return the result with opposite sign. Drew: this lines are from method _add. I use this method to decide what to do with numbers it receive. Send them to add method? Or send them to subract method but with different order (sn1.subtract(this))? And so on..

if (this.sign == 1) {
    if (sn1.sign == 1) {
        if (this.compare(sn1) == -1) return sn1.add(this);
        else return this.add(sn1);
    }
    else if (wl1.sign == 0) return this;
    else {
        if (this.compare(sn1.abs()) == 1) return this.subtract(sn1.abs());
        else if (this.compare(sn1.abs()) == 0) return new BigNumber(0);
        else return sn1.abs().subtract(this).negate(); // return the number with opposite sign;
    }
} else if (this.sign == 0) return sn1;
else {
    if (wl1.sign == 1) {
        if (this.abs().compare(sn1) == -1) return sn1.subtract(this.abs());
        else if (this.abs().compare(sn1) == 0) return new BigNumber(0);
        else return this.abs().subtract(sn1).negate();
    } else if (sn1.sign == 0) return this;
    else return (this.abs().add(wl1.abs())).negate();
}

As you can see – this code looks horrible..

  • 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-13T12:53:09+00:00Added an answer on May 13, 2026 at 12:53 pm

    I suggest you spend some more methods 😉 How about this:

    if (isPositive() && other.isPositive()) {
      if (this.isBiggerThen(other)) {
        return this.plus(other);
      } else {
        return other.plus(this);
      }
    }
    

    Note that I renamed sn1 to other and the add method to plus to indicate, that the method returns the sum to increase readability. add is usually used if something is added to the object itself (like in the BigInteger class).

    The implementations for isPositive and isBiggerThen are pretty simple:

    private boolean isPositive() {
      return sign == 1;
    }
    
    private boolean isBiggerThen(BigNumber other) {
      return this.compare(other) > 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 341k
  • Answers 341k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First get the index of the substring: int startIndex =… May 14, 2026 at 4:53 am
  • Editorial Team
    Editorial Team added an answer If you're using AS2, you can use the MovieClip.swapDepths() method… May 14, 2026 at 4:53 am
  • Editorial Team
    Editorial Team added an answer You probably want to subclass UIScrollView and intercept the tap,… May 14, 2026 at 4:53 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.