Page 2 of 5 FirstFirst 1234 ... LastLast
Results 21 to 40 of 81
  1. #21
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    That part I get.(by the way, what do you call that in english?)
    But the return part. If number2 is larger than number1, it will return number2, other wise number1(which is the case here). But were does it return number1? I mean there is no varible that is going to accept number1, but it seems that biggest is taking the value that the method max is returning. I am confused.

  2. #22
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If a method has a return value you can substitute it for that type nix it returns a double it can be assigned to a double variable. When you set biggest to max it makes biggest equal the result of max.

  3. #23
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    When you set biggest to max it makes biggest equal the result of max.
    So if I understand right:
    becuse number1 is bigger, it get copied to max, and then return it to biggest?

  4. #24
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Sort of. The method max takes two arguments and returns one of them(the larger of the two) so max(5,2) is the same as 5. You passed in the arguments and it returns the larger of the two. What you do with the return is up to you. You can set it to some variable(like biggest) or perform calculations on it and so on. Try not to think if the method so strictly as a method and vaguely as a type equal to the return type. I hope I am making sense to you.

  5. #25
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    an other question about return.

    Java Code:
    Code:
    ---------
    class Pojahns
    
    {
        public static void main (String[] args)// throws Exception
    
        {
            java.util.Scanner    in = new java.util.Scanner (System.in);
    
            line (5);
    
        }
    
        public static void line (int n)
        {
            if (n < 1)
                return;
    
            System.out.print ("-");
            line (n - 1);
        }
    
    
    }
    ---------

    This program will print "-" 5 times. That I get. But the if and the return part, how does it work? What does return do if the condition is true? and false?(what does it return?)
    This is a post by you that was sent to my email address coming from this thread but it hasn't shown here.

    This is a recursive method. With a recursive method it keeps calling itself until some condition is met(sounds familiar to a loop, right?)

    In this case the condition to stop execution of the program is when n < 1.

    Each recursive call subtracts 1 from n, so with each call the method gets closer and closer to the termination clause(the clause that ends it). Each step is continually added to the stack, when it reaches the line with the recursive call it pushes a new method on the stack, it continues to do this until the termination clause is met and it pops everything off.

    This is, of course, a very basic recursive program and you can do much more complex things with recursion. Try looking up sierpinski triangle for an example of a fun recursion problem(which I am sure can be done with loops as well). If you like the idea of recursion and would like to learn more on it, a great book is htdp, available for free at htdp.org. Finally this method could also be made to return a string of lines like this:

    Java Code:
    public static String line(int n){
      if(n < 1){
        return;
      }
      else{
        return "-" + line(n - 1);
      }
    }
    For more recursive exercises, check out the following link:

    CodingBat Java Recursion-1

  6. #26
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Thanks, you made it more clear for me.
    Another thing about methods.

    Lets say I have an array that look like this:

    Java Code:
    int[] a = new int[4];
    v[0] = 4
    v[1] = 2
    v[2] = 6
    v[3] = 1
    Now I want to create a method that change this to numerical order(smallest number comes first, and then high and higher,so it would look like 1 2 4 6).
    Then it return it to a new array(it is not possible to replace a, new array must be created).

  7. #27
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can do this easy way:
    Java Code:
    Arrays.sort(array);
    or

    the hard way:

    Create your own sort algorithm.

    Java Code:
    public static int[] sortArray(int[] intArray){
      int[] x = new int[intArray.length];
      //do sorting
      return x;
    }
    Sorting will take some thinking, and possibly some research, try googling the different types of sorting(selection, insertion, merge, and quicksort)

  8. #28
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Arrays.sort works fine :) I need to check the library more.
    I thought creating number ordering was going to be easy. I read on a site that it was recommended to use Arrays.sort instead of creating their own.
    A question is, how can I check Arrays.sort? I can find it in the library, but I dont know how the check the code.

  9. #29
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you mean, how can you print the array? How would you print a normal array(think loops)?

    You can also use Collections.sort, when using things other than arrays(list, set, maps, etc)

  10. #30
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    I want to view the actual code of method Arrays.sort.
    I want to see how the author made it, so I can learn from it.

  11. #31
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, I really hope you aren't cheating and trying to get the actual code. If you go into your java file you should see a file named src, inside there you can find all the code for every java class. Be careful not to edit/delete any of that however.

    for me it's located at
    Java Code:
    c:/program files/java/jdk1.6.0_23/src

  12. #32
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    I am stuck, and I dont think I will figure it out by myself this time :D

    I want to make so it checks if this double varible have .5 in decimals.

    Java Code:
    double a = 6.5
    if (a == a.5)
    This dont work. Any idea?

    An other thing, how come when I change double 4.8 to a int varbile, it becomes 4 and not 5? Is there way to change that?

  13. #33
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try seeing if you can use modulus to figure this one out

  14. #34
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It can also be done with subtraction and a cast.

  15. #35
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Pojahn_M View Post
    I am stuck, and I dont think I will figure it out by myself this time :D

    I want to make so it checks if this double varible have .5 in decimals.

    Java Code:
    double a = 6.5
    if (a == a.5)
    This dont work. Any idea?

    An other thing, how come when I change double 4.8 to a int varbile, it becomes 4 and not 5? Is there way to change that?
    Java Code:
    double a = 6.5;
    boolean increment;
    String doubleString = a.toString();
    int index = doubleString.indexOf(".");
    
    if (doubleString.substring(index+1).equals("5"))
    increment = true;

  16. #36
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    I cant figure it out. Help me with a start.
    Solarsonic I havenet made iit that far yet :) But I check it out.
    I am looking for a manual way.

  17. #37
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Pojahn_M View Post
    I am stuck, and I dont think I will figure it out by myself this time :D

    I want to make so it checks if this double varible have .5 in decimals.

    Java Code:
    double a = 6.5
    if (a == a.5)
    This dont work. Any idea?

    An other thing, how come when I change double 4.8 to a int varbile, it becomes 4 and not 5? Is there way to change that?
    Quote Originally Posted by Solarsonic View Post
    Java Code:
    double a = 6.5;
    boolean increment;
    String doubleString = a.toString();
    int index = doubleString.indexOf(".");
    
    if (doubleString.substring(index+1).equals("5"))
    increment = true;
    Quote Originally Posted by Pojahn_M View Post
    I cant figure it out. Help me with a start.
    Solarsonic I havenet made iit that far yet :) But I check it out.
    I am looking for a manual way.
    What I did is:

    I first converted the double to a String. I then got the index of the decimal point. And finally I got what comes after the decimal point by using substring and adding 1 to the index to start after the decimal point. In the same if statement, I checked if the String after the decimal point is equal to 5 (so that the number has to have .5).

  18. #38
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    An easier way is to simply subtract the low end of the number from the total(for 6.5, the low end is 6.) with this you will be able determine what the right side of the decimal is and do comparisons.

    The reason 4.8 as an int produces 4 and not 5 is that integers don't do rounding, they simply have all numbers to the right side truncated.

  19. #39
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by sunde887 View Post
    An easier way is to simply subtract the low end of the number from the total(for 6.5, the low end is 6.) with this you will be able determine what the right side of the decimal is and do comparisons.

    The reason 4.8 as an int produces 4 and not 5 is that integers don't do rounding, they simply have all numbers to the right side truncated.
    Tbh I think using String methods is easier and makes more sense.

  20. #40
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Either way is viable, however the number method may be slightly less typing.
    @pojahn, try to understand both ways and decide which you like better.

    Also, post if you are still unclear.

Page 2 of 5 FirstFirst 1234 ... LastLast

Similar Threads

  1. Replies: 5
    Last Post: 04-27-2011, 03:13 AM
  2. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 06:37 PM
  3. Replies: 4
    Last Post: 06-07-2009, 01:59 AM
  4. Replies: 3
    Last Post: 06-07-2009, 01:14 AM
  5. Replies: 12
    Last Post: 05-26-2009, 02:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •