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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:38:32+00:00 2026-05-17T23:38:32+00:00

I have the following code, which does not work correctly for some reason that

  • 0

I have the following code, which does not work correctly for some reason that I am trying to figure out:

public static int score(String gene1, String gene2){
    char[] a=new char[gene1.length()];
    char[] b=new char[gene2.length()];
    a=gene1.toCharArray();
    b=gene2.toCharArray();
    return score(a, b, 0,0);
}

private static int score(char[] a, char[] b, int i, int j){
    if(a[i]=='\0' || b[j]=='\0')
        return 0;
    else if (a[i]==b[j])
        return 1+score(a, b, i+1, j+1);
    else 
        return max(score(a, b,i+1, j),score(a, b, i, j+1));
}

private static int max (int a, int b){
    if (a<b) return b;
    else return a;
}

Here is where it fails:

assertEquals(2, GeneAnalysis.score("ACGT","AC")); 

I get an IndexOutofBoundsError

Any ideas? Also, when offering help, please do not change method parameters. They are supposed to be the way they are.

  • 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-17T23:38:32+00:00Added an answer on May 17, 2026 at 11:38 pm

    Part of this seems to be a confusion between C and Java….

    if(a[i]=='\0' || b[j]=='\0')
            return 0;
    

    C has a null terminator for strings, Java does not. Instead to check for the end of a Java array you will need to use the .length attribute… something like:

       if(i >= a.length || j >= b.length) 
    

    Edit based on comment.

    Really, you should ask a separate question on this… but here is a stab at how it works. First off you are using recursion, which, yes, is a tricky concept. Wikipedia probably can help you with the basics of recursion.

    Here is the code closer to how I would write it, with comments showing you the order that things occur in:

    public class Main
    {
        public static void main(String[] args)
        {
            final int value;
    
            value = score("ACGT", "AC");
            System.out.println(value);
        }
    
        public static int score(final String gene1,
                                final String gene2)
        {
            final char[] a;
            final char[] b;
            final int    s;
    
            a = gene1.toCharArray();
            b = gene2.toCharArray();
            s = score(a, b, 0, 0);
    
            return (s);
        }
    
        private static int score(final char[] a,
                                 final char[] b,
                                 final int    idxA,
                                 final int    idxB)
        {
            final int value;
    
            // for all calls: a.length == 4 and b.length == 2
            // first call:  idxA == 0 and idxB == 0 - false || false == false
            // second call: idxA == 1 and idxB == 1 - false || false == false
            // third call:  idxA == 2 and idxB == 2 - false || true  == true      
            if(idxA >= a.length || idxB >= b.length)
            {
                // third call: will return 0            
                value = 0;
            }
            // first call:  a[idxA] == A and b[idxB] == A - true
            // second call: a[idxB] == C and b[idxB] == C - true 
            else if(a[idxA] == b[idxB])
            {
                // this is recursion
                // first call:  1 + score(a, b, 1, 1)
                // second call: 1 + score(a, b, 2, 2)
    
                // after the third call the call stack starts unwinding
                // second call: 1 + 0 == 1
                // first call:  1 + 1 == 2
                value = 1 + score(a, b, idxA + 1, idxB + 1);
            }
            else
            {
                final int x;
                final int y;
    
                x = score(a, b, idxA + 1, idxB);
                y = score(a, b, idxB,     idxB + 1);
                value = max(x, y);
            }
    
            // third call:  0
            // second call: 1
            // first call:  2
            return (value);
        }
    
        // Can you use Math.max(int, int) instad?
        private static int max(final int x, final int y)
        {
            final int value;
    
            if(x < y)
            {
                value = y;
            }
            else
            {
                value = x;
            }
    
            return (value);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Runnable implementation that does some work which might take some time
I have the following Code which I know doesn't work correctly. Yes I know
I have the following code which does a bit of housekeeping on a database:
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var
Using the following steps: (I have checked this similar post , which does not
I have some PHP code which runs a loop, spitting out a button and
I have following code which works for radio buttons but need to be changed
I want to know is below code correct ? I have following code which
I have the following code which definitely returns a proper data result if I
I have the following code which is working, I was wondering if this can

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.