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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:39:43+00:00 2026-05-17T19:39:43+00:00

I have never used ruby but need to translate this code to java. Can

  • 0

I have never used ruby but need to translate this code to java.

Can anyone help me.

This is the code in Ruby.

DEFAULT_PRIOR = [2, 2, 2, 2, 2]
## input is a five-element array of integers
## output is a score between 1.0 and 5.0
def score votes, prior=DEFAULT_PRIOR
posterior = votes.zip(prior).map { |a, b| a + b }
sum = posterior.inject { |a, b| a + b }
posterior.
map.with_index { |v, i| (i + 1) * v }.
inject { |a, b| a + b }.
to_f / sum
end

I obtained it from here so maybe some clues can be found there. its about calculating averages

How to rank products based on user input

This was the solution. In case anyone needs it

    static final int[] DEFAULT_PRIOR = {2, 2, 2, 2, 2};

static float  score(int[] votes) {
    return score(votes, DEFAULT_PRIOR);
}

private static float score(int[] votes, int[] prior) {

    int[] posterior = new int[votes.length];
    for (int i = 0; i < votes.length; i++) {
        posterior[i] = votes[i] + prior[i];
    }
    int sum = 0;
    for (int i = 0; i < posterior.length; i++) {
        sum = sum + posterior[i];
    }

    float sumPlusOne = 0;
    for (int i = 0; i < posterior.length; i++) {
        sumPlusOne = sumPlusOne + (posterior[i] * (i + 1));
    }
    return sumPlusOne / sum;
}
  • 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-17T19:39:43+00:00Added an answer on May 17, 2026 at 7:39 pm

    The code does the following things:

    It set a constant named DEFAULT_PRIOR (java equivalent would be a static final variable) to an array containing the number 2 five times. In java:

    static final int[] DEFAULT_PRIOR = {2,2,2,2,2};
    

    It defines a two-argument method named score where the second argument defaults to DEFAULT_PRIOR. In java this can be achieved with overloading:

    float score(int[] votes) {
        return score(votes, DEFAULT_PRIOR);
    }
    

    Inside the score method, it does the following:

    posterior = votes.zip(prior).map { |a, b| a + b }
    

    This creates an array posterior, where each element is the sum of the corresponding element in votes and the corresponding element in prior (i.e. posterior[i] = votes[i] + prior[i]).

    sum = posterior.inject { |a, b| a + b }
    

    This sets sum to be the sum of all elements in posterior.

    posterior.
    map.with_index { |v, i| (i + 1) * v }.
    inject { |a, b| a + b }.
    to_f / sum
    

    This bit multiplies each element in posterior with its index plus one and then sums that. The result is converted to a float and then divided by sum.

    In java you would iterate over posterior with for-loop (not foreach) and in each iteration add (i + 1) * posterior[i] (where i is the index of the for-loop) to a variable tmp, which you set to 0 before the loop. Then you would cast tmp to float and divide it by sum.

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

Sidebar

Related Questions

I have often heard this term being used, but I have never really understood
I have never used octal numbers in my code nor come across any code
I'm used to doing Java programming, where you never really have to think about
I've just inherited a website built with Ruby on Rails. I have never used
I have never used ClearCase, but have used Subversion and for a short period
I have never used Prototype before. But now when I'm using Rails, it seems
i have a input tag which is non editable, but some times i need
i have never used LINQ in any of my projects , i have always
I have never used virtualization, and am trying to get up to speed. My
I've never used the Global Temporary Tables however I have some questions how they

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.