gcd method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PvtBillPilgrim
    New Member
    • May 2007
    • 19

    gcd method

    I'm very new to Java and I was looking for some help on this particular problem.

    I need to create a method that takes a String as a parameter and returns "true" if the parameter is the letter Y or the letter N (in either upper or lower case), or false otherwise.

    Most of the methods we've been working on are numbers (integers mostly) not strings, so this one confuses me quite a bit. How do you make the computer look for a certain character like y, Y, n, N. I don't want to declare them as variables, I just want Java to see them as letters.

    I have:
    public static boolean isYorN(String str)
    {
    boolean character;
    switch (character)
    {
    case 'y':
    character = true;
    return true;
    break;

    case 'Y':
    character = true;
    return true;
    break;

    case 'n':
    character = true;
    return true;
    break;

    case 'N':
    character = true;
    return true;
    break;

    default:
    character = false;
    return false;

    }
    }
    }

    I think a switch case is the best method, but I'm missing something about naming because all my compiling errors are telling me that I have incompatible types (i.e. found: boolean, required: int).

    Thank you in advance for any help.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    I believe the errors are coming from the fact that you are using 'Y', 'y', 'N', and 'n' as the cases, when you tell the switch statement (swtich (character)) that it should expect a boolean - either a true or a false.

    I would recommend using the .equals() function in the String class. That returns a boolean, so you can resolve it as being either true or false (use the fact that it will return as either true or false).

    (And in future posts, please use [code ] and [/code ] around your code - only without the spaces at the end, it really helps with readability. Thanks!)
    Last edited by sicarie; May 23 '07, 09:29 PM. Reason: Clarifying explanation.

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      Originally posted by sicarie
      I believe the errors are coming from the fact that you are using 'Y', 'y', 'N', and 'n' as the cases, when you tell the switch statement (swtich (character)) that it should expect a boolean - either a true or a false.

      I would recommend using the .equals() function in the String class. That returns a boolean, so you can resolve it as being either true or false (use the fact that it will return as either true or false).

      (And in future posts, please use [code ] and [/code ] around your code - only without the spaces at the end, it really helps with readability. Thanks!)

      Can PvtBillPilgrim use scanner and do this
      if(kbd.equalsIg noreCase("Y"));

      then use the switch or if statement
      ...

      Comment

      • PvtBillPilgrim
        New Member
        • May 2007
        • 19

        #4
        gcd method

        I am a beginner programmer in Java. Just started learning the past few weeks.

        I'm taking a class and need to create a method that finds the greatest common divisor of two integers. I can assume that both are positive, but I cannot use the Euclidean Algorithm.

        I'm sort of lost, but I think I'm on the right track, although this may look confusing:
        Code:
        public static int gcd(int a, int c)
        {
        int gcd;
        int attempt;
        if (a > c)
        {
         if (a % c == 0)
        {
        gcd = c;
        return gcd;
        }
        else
        {
        for (attempt = c; attempt = 1; attempt--)
        do
        {
        if (c % attempt == 0 && a % attempt == 0)
        {
        gcd = attempt;
        return attempt;
        }
        }
        while (c % attempt != 0 || a % attempt == 0);
        }
        }
        And then I basically copied the code with an else statement if c > a.

        I'm sure there are a lot of mistakes. Could someone just point them out and offer a simpler way of doing it (possibly without using the Euclidean Algorithm)?

        Thanks.
        Michael

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Hint: for any two (positive) numbers a and c the gcd is the largest number in the
          range [1 ... min(a, c) ]. Only a single for loop could do the job. There's no need
          to duplicate code for a < c or c < a and there's also no need for two nested loops.

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by PvtBillPilgrim

            ....I think a switch case is the best method...
            I'm afraid you're wrong there and that's where you missed it. Just an if else will do it.

            big hint:
            [CODE=java] if(str.equalsIg noreCase("y")) ....[/CODE]

            Comment

            • rsrinivasan
              New Member
              • Mar 2007
              • 221

              #7
              Originally posted by PvtBillPilgrim
              I am a beginner programmer in Java. Just started learning the past few weeks.

              I'm taking a class and need to create a method that finds the greatest common divisor of two integers. I can assume that both are positive, but I cannot use the Euclidean Algorithm.

              I'm sort of lost, but I think I'm on the right track, although this may look confusing:
              Code:
              public static int gcd(int a, int c)
              {
              int gcd;
              int attempt;
              if (a > c)
              {
               if (a % c == 0)
              {
              gcd = c;
              return gcd;
              }
              else
              {
              for (attempt = c; attempt = 1; attempt--)
              do
              {
              if (c % attempt == 0 && a % attempt == 0)
              {
              gcd = attempt;
              return attempt;
              }
              }
              while (c % attempt != 0 || a % attempt == 0);
              }
              }
              And then I basically copied the code with an else statement if c > a.

              I'm sure there are a lot of mistakes. Could someone just point them out and offer a simpler way of doing it (possibly without using the Euclidean Algorithm)?

              Thanks.
              Michael
              Hi,
              I modified ur function to find GCD of two numbers.
              Try this function repl ur feedback

              [edit] Don't supply full spoonfed code; it's not allowed in this forum. Thanks;

              Thanks,

              Srinivas r.

              Comment

              • PvtBillPilgrim
                New Member
                • May 2007
                • 19

                #8
                OK. I think this is a lot better. It compiles and makes sense to me at least. However, it doesn't work properly for more difficult integers like 20, 30. Can anyone spot the problem (as I said nothing wrong with compiling, just doesn't work logically). As I said before, I can't use the Euclidean Algorithm, so I basically had to write the simplest program to find gcd.

                Code:
                public static int gcd(int a, int b)
                {
                int gcd;
                if (a > b && a % b == 0)
                {
                gcd = b;
                return gcd;
                }
                else if (b > a && b % a == 0)
                {
                gcd = a;
                return gcd;
                }
                else if (a > b && a % b != 0)
                {
                int count = b;
                do
                {
                count = count - 1;
                }
                while (b % count != 0 && a % count != 0);
                gcd = count;
                return gcd;
                }
                else if (b > a && b % a != 0)
                {
                int count = a;
                do
                {
                count = count - 1;
                }
                while (b % count != 0 && a % count != 0);
                gcd = count;
                return gcd;
                }
                else
                {
                gcd = a;
                return gcd;
                }
                }

                Comment

                • blazedaces
                  Contributor
                  • May 2007
                  • 284

                  #9
                  I'm not 100% sure about the forum guidelines, but I'm pretty sure you should post this as a new thread/topic because it is indeed a ... new... topic. Anyways, may I ask why you're using subtraction and not mod? What do you mean you can't use the euclidean algorithm?

                  Ok, you're subtracting by one each time... why? Why not subtract by intervals of the smaller number (original euclidean algorithm) or use the mod method?

                  You realize there's a Math.max(int a, int b) method?

                  I don't know what is logically wrong in your code, but you don't have a good base of an idea, try rewriting and also tell us what exactly outputs when you have higher integers.

                  -blazed

                  Comment

                  • astolpho
                    New Member
                    • May 2007
                    • 3

                    #10
                    Originally posted by r035198x
                    I'm afraid you're wrong there and that's where you missed it. Just an if else will do it.

                    big hint:
                    [CODE=java] if(str.equalsIg noreCase("y")) ....[/CODE]
                    perhaps simply

                    return str.equalsIgnor eCase("y") || str.equalsIgnor eCase("n");

                    Comment

                    • prometheuzz
                      Recognized Expert New Member
                      • Apr 2007
                      • 197

                      #11
                      Originally posted by PvtBillPilgrim
                      OK. I think this is a lot better. It compiles and makes sense to me at least. However, it doesn't work properly for more difficult integers like 20, 30. Can anyone spot the problem (as I said nothing wrong with compiling, just doesn't work logically). As I said before, I can't use the Euclidean Algorithm, so I basically had to write the simplest program to find gcd.
                      Oh man, why so complicated? Here's some pseudo code to do it (non-Euclidean-style, why not Euclidean, btw?):

                      Code:
                      public static int gcd(int a, int b) {
                      
                        counter <- the minimum value between 'a' and 'b'
                      
                        WHILE counter is more than 0
                      	IF 'counter' divides 'a' AND 'counter' divides 'b'
                      	  break out of this loop
                      	END IF
                      	decrease 'counter'
                        END WHILE
                      
                        return 'counter'
                      }
                      Good luck.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by prometheuzz
                        Oh man, why so complicated? Here's some pseudo code to do it (non-Euclidean-style, why not Euclidean, btw?):
                        Yup, exactly what I suggest in the thread where the sequel of this
                        thread belongs ;-)

                        kind regards,

                        Jos

                        ps. I just merged the two threads.
                        Last edited by JosAH; May 24 '07, 07:27 PM. Reason: merged two threads

                        Comment

                        Working...