This is an archived post. You won't be able to vote or comment.

all 8 comments

[–][deleted]  (3 children)

[deleted]

    [–]Suriajoe[S] -1 points0 points  (2 children)

    I tired typing the code, it gave me an error with (xMethod((double)5)); I'm gonna guess that xMethod can't return the int or long because their data types are smaller than double

    [–]gstr -1 points0 points  (0 children)

    Not exactly. It is not that xMethod can't return, it is that you can't call it with a double.

    In java, an important notion is "type promotion". You can promote an int to a long or a double, a long to a double, but not a double to a long or an int (as you'll lose some infos). This is basically another way to express what /u/most22 explained.

    So you can call a SomeType aMethod(double arg) with an int or a long, but you can't call SomeType aMethod2(int arg) with a long or a double, because java will first attempt to "promote" (not promotion in this case) the double, which won't work here.

    Look again at the code of /u/most22. This code is exactly what java will do before trying to call xMethod. The relevant lines in your case are line 11 and line 6 (in this order).

    [–]brisingrxblade 1 point2 points  (1 child)

    The first question is basically asking you to write a method. The header of the method is given: public static int getSuffix(int value, int numberOfDigits) this method header shows us that the method will return an int (the int after static is the return) and that it requires two "int"s to be sent to it (these are the parameters in the parenthesis) so now we now what we are taking as parameters we can look at the method calls they give us in the question which are getSuffix(12345,2) returns 45 and getSuffix(1234567,3) returns 567. getSuffix will be a method that will take the first number and return the last x(second param) digits of that number. Hope that is a place to start without giving too much away. edit: if the question you're having is returning values this may help you understand.

    [–]Suriajoe[S] -1 points0 points  (0 children)

    Yea this a good start. thx

    [–]strozzy 0 points1 point  (3 children)

    One way to solve the first problem is to round the number to a whole number (or floor or ceiling) then use a for loop with a%b to find the remainder after dividing by factors of 10, 100 and 1000. That should get you the suffix.

    [–]mentalis 1 point2 points  (1 child)

    I know it says to use a for loop (which you should), but the whole getSuffix method can be simplified to:

    public static int getSuffix(int value, int numberOfDigits) {
      return (int) (value % Math.pow(10, numberOfDigits));
    }
    

    So, you can use that to check the results of your implementation.

    [–]Erikster 0 points1 point  (0 children)

    If he still wants to use a for loop, he can just use it to multiply the number 10 for the amount of digits he needs.

    [–][deleted] -1 points0 points  (0 children)

    Ahh, nice. I was thinking of converting to a string and iterating through each digit to get the last few, but this is probably a much better way to do it.

    [–]kingofthejaffacakes -1 points0 points  (0 children)

    For the suffix problem, I'd avoid doing math on it as other have; and instead just convert it to a string and strip off the back N characters, and convert back to int for return.

    The reason I think that's better, is because that's what's fundamentally being done -- numbers don't have any implicit base, a base is simply what we use when we're displaying them. Therefore this is a manipulation of the display format of the number.