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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:50:52+00:00 2026-05-26T06:50:52+00:00

I’m in the process of trying to make an immutable class in which represents

  • 0

I’m in the process of trying to make an immutable class in which represents natural numbers. I’m using recursion in order to handle the Increment and Decrement methods. Since the fields are final, I made a private constructor to assign new values to the necessary fields when decrementing/incrementing. After testing this implementation, I can’t seem to pin point the problem. If I decrement 100, it will be 10. If I increment 99, it will be 9. If I increment/decrement a number not on the bound, I will get a long string of gibberish. I guess I need a nudge in the right direction. I’m able to get it to work fine if it’s mutable because i don’t have to worry about the final fields.

public final class SlowBigNatural implements BigNatural{
final private int natural[];
final private int nSize;
final private int HIGHEST = 9;

public SlowBigNatural() {
    this.nSize = 1;
    this.natural = new int[1];
    this.natural[0] = 0;
}

public SlowBigNatural(int p) {
    this(Integer.toString(p));
}

public SlowBigNatural(String s) {
    this.nSize = s.length();
    this.natural = new int[nSize];
    for (int i = 0; i < nSize; i++) {
        this.natural[i] = Character.digit(s.charAt(i), 10);
    }
}

public SlowBigNatural(BigNatural c) {
    this(c.toString());
}

private SlowBigNatural(int[] natural, int nSize){
    this.nSize = nSize - 1;
    this.natural = new int[this.nSize];
    for (int i = 0; i < this.nSize; i++) {
        this.natural[i] = natural[i];
    } 
}

public BigNatural increment() {
    int[] nClone = new int[nSize];
    System.arraycopy(natural, 0, nClone, 0, nSize);
    if (nSize == 1 || nClone[nSize - 1] != HIGHEST) {
        nClone[nSize - 1]++;
        BigNatural nInc = new SlowBigNatural(nClone.toString());
        return nInc;
    } 

    else {
        nClone[nSize - 1] = 0;
        BigNatural temp = new SlowBigNatural(nClone, nSize);
        temp.increment();
        return temp;
    }
}

public BigNatural decrement() {
    int[] nClone = natural.clone();
    if (nClone[nSize - 1] != 0) {
        nClone[nSize - 1]--;
        BigNatural nDec = new SlowBigNatural(nClone.toString());
        return nDec;
    } 
    else {
        if (nSize != 1) {
            nClone[nSize - 1] = HIGHEST;
            BigNatural temp = new SlowBigNatural(nClone, nSize);
            temp.decrement();
            return temp;
        }
        else{
            BigNatural nDec = new SlowBigNatural(0);
            return nDec;
        }
    }
}

public String toString() {
    String nString = "";
    for (int i = 0; i < nSize; i++) {
        nString += String.valueOf(natural[i]);
    }
    return nString.replaceFirst("^0+(?!$)", "");
}
}

I stepped through my code, and the error seems to occur when I convert the array to a string and pass it through the constructor. It turns the array into a bunch of craziness. Continuing to investigate.

  • 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-26T06:50:52+00:00Added an answer on May 26, 2026 at 6:50 am

    Haven’t fully looked into it but if SlowBigNatural is really correctly immutable, then the following:

    BigNatural temp = new SlowBigNatural(nClone, nSize);
    temp.increment();
    return temp;
    

    is unlikely to be useful as far as I can see. The above call to temp.increment() creates a new object that you ignore, seen that you return temp itself and not the result of temp.increment().

    Could you try changing the above to this:

    BigNatural temp = new SlowBigNatural(nClone, nSize);
    return temp.increment();
    

    And if works, do the same for decrement().

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... 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.