Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Java Program - Greatest Common Divisor

kentyladkentylad Posts: 27Member
I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:

[code]
import java.lang.*;
import java.math.*;

public class Gcd {

public static int gcdCalc ( int int1, int int2 ) {

if( int1 == int2) {
gcd = int1;
}//if

else {
while ( int1 != int2) {

if( int1 > int2 ) {
int1 = int1 - int2;
}//nested-if

else {
int2 = int2 - int1;
}//nested-else
}//while
}//else

return( gcd );

}//gcdCalc


public static void main( String args[] ) {

int arg1 = Integer.parseInt( args[0] );
int arg2 = Integer.parseInt( args[1] );

int endGcd = gcdCalc( arg1, arg2 );

System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
}//main

}//Gcd class
[/code]

The program as it stands will not compile and the errors I cannot seem to fix

[b][blue]Errors:[/b]

cannot resolve symbol
symbol : variable gcd
location: class Gcd
gcd = int1;
^
cannot resolve symbol
symbol : variable gcd
location: class Gcd
return( gcd );
^
[/blue]

Any suggestions?
«1

Comments

  • ManningManning Posts: 1,621Member
    : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
    :
    : [code]
    : import java.lang.*;
    : import java.math.*;
    :
    : public class Gcd {
    :
    : public static int gcdCalc ( int int1, int int2 ) {
    :
    : if( int1 == int2) {
    : gcd = int1;
    : }//if
    :
    : else {
    : while ( int1 != int2) {
    :
    : if( int1 > int2 ) {
    : int1 = int1 - int2;
    : }//nested-if
    :
    : else {
    : int2 = int2 - int1;
    : }//nested-else
    : }//while
    : }//else
    :
    : return( gcd );
    :
    : }//gcdCalc
    :
    :
    : public static void main( String args[] ) {
    :
    : int arg1 = Integer.parseInt( args[0] );
    : int arg2 = Integer.parseInt( args[1] );
    :
    : int endGcd = gcdCalc( arg1, arg2 );
    :
    : System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
    : }//main
    :
    : }//Gcd class
    : [/code]
    :
    : The program as it stands will not compile and the errors I cannot seem to fix
    :
    : [b][blue]Errors:[/b]
    :
    : cannot resolve symbol
    : symbol : variable gcd
    : location: class Gcd
    : gcd = int1;
    : ^
    : cannot resolve symbol
    : symbol : variable gcd
    : location: class Gcd
    : return( gcd );
    : ^
    : [/blue]
    :
    : Any suggestions?

    The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.
  • kentyladkentylad Posts: 27Member
    [b][red]This message was edited by kentylad at 2004-5-2 3:29:59[/red][/b][hr]
    : : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
    : :
    : : [code]
    : : import java.lang.*;
    : : import java.math.*;
    : :
    : : public class Gcd {
    : :
    : : public static int gcdCalc ( int int1, int int2 ) {
    : :
    : : if( int1 == int2) {
    : : [red]int[/red] gcd = int1;
    : : }//if
    : :
    : : else {
    : : while ( int1 != int2) {
    : :
    : : if( int1 > int2 ) {
    : : int1 = int1 - int2;
    : : }//nested-if
    : :
    : : else {
    : : int2 = int2 - int1;
    : : }//nested-else
    : : }//while
    : : }//else
    : :
    : : return( gcd );
    : :
    : : }//gcdCalc
    : :
    : :
    : : public static void main( String args[] ) {
    : :
    : : int arg1 = Integer.parseInt( args[0] );
    : : int arg2 = Integer.parseInt( args[1] );
    : :
    : : int endGcd = gcdCalc( arg1, arg2 );
    : :
    : : System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
    : : }//main
    : :
    : : }//Gcd class
    : : [/code]
    : :
    : : The program as it stands will not compile and the errors I cannot seem to fix
    : :
    : : [b][blue]Errors:[/b]
    : :
    : : cannot resolve symbol
    : : symbol : variable gcd
    : : location: class Gcd
    : : gcd = int1;
    : : ^
    : : cannot resolve symbol
    : : symbol : variable gcd
    : : location: class Gcd
    : : return( gcd );
    : : ^
    : : [/blue]
    : :
    : : Any suggestions?
    :
    : The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.

    OK so I have declared the variable gcd's type (int), however the
    [blue]
    cannot resolve symbol
    symbol : variable gcd
    location: class Gcd
    return( gcd );
    ^[/blue]
    error still persists




  • ManningManning Posts: 1,621Member
    : [b][red]This message was edited by kentylad at 2004-5-2 3:29:59[/red][/b][hr]
    : : : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
    : : :
    : : : [code]
    : : : import java.lang.*;
    : : : import java.math.*;
    : : :
    : : : public class Gcd {
    : : :
    : : : public static int gcdCalc ( int int1, int int2 ) {
    : : :
    : : : if( int1 == int2) {
    : : : [red]int[/red] gcd = int1;
    : : : }//if
    : : :
    : : : else {
    : : : while ( int1 != int2) {
    : : :
    : : : if( int1 > int2 ) {
    : : : int1 = int1 - int2;
    : : : }//nested-if
    : : :
    : : : else {
    : : : int2 = int2 - int1;
    : : : }//nested-else
    : : : }//while
    : : : }//else
    : : :
    : : : return( gcd );
    : : :
    : : : }//gcdCalc
    : : :
    : : :
    : : : public static void main( String args[] ) {
    : : :
    : : : int arg1 = Integer.parseInt( args[0] );
    : : : int arg2 = Integer.parseInt( args[1] );
    : : :
    : : : int endGcd = gcdCalc( arg1, arg2 );
    : : :
    : : : System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
    : : : }//main
    : : :
    : : : }//Gcd class
    : : : [/code]
    : : :
    : : : The program as it stands will not compile and the errors I cannot seem to fix
    : : :
    : : : [b][blue]Errors:[/b]
    : : :
    : : : cannot resolve symbol
    : : : symbol : variable gcd
    : : : location: class Gcd
    : : : gcd = int1;
    : : : ^
    : : : cannot resolve symbol
    : : : symbol : variable gcd
    : : : location: class Gcd
    : : : return( gcd );
    : : : ^
    : : : [/blue]
    : : :
    : : : Any suggestions?
    : :
    : : The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.
    :
    : OK so I have declared the variable gcd's type (int), however the
    : [blue]
    : cannot resolve symbol
    : symbol : variable gcd
    : location: class Gcd
    : return( gcd );
    : ^[/blue]
    : error still persists

    Has to do with the "scope" of the variable. Since you declared it inside a set of { } brackets, it is only visible from within that block of code. You need to declare it at the very top of your method, and then it'll be available in all parts of it.
  • kentyladkentylad Posts: 27Member
    [b][red]This message was edited by kentylad at 2004-5-2 6:20:33[/red][/b][hr]
    : : [b][red]This message was edited by kentylad at 2004-5-2 3:29:59[/red][/b][hr]
    : : : : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
    : : : :
    : : : : [code]
    : : : : import java.lang.*;
    : : : : import java.math.*;
    : : : :
    : : : : public class Gcd {

    : : : : [red]gcd = 0;[/red]

    : : : : public static int gcdCalc ( int int1, int int2 ) {
    : : : :
    : : : : if( int1 == int2) {
    : : : : gcd = int1;
    : : : : }//if
    : : : :
    : : : : else {
    : : : : while ( int1 != int2) {
    : : : :
    : : : : if( int1 > int2 ) {
    : : : : int1 = int1 - int2;
    [red]gcd = int1;[/red]
    : : : : }//nested-if
    : : : :
    : : : : else {
    : : : : int2 = int2 - int1;
    [red]gcd = int2;[/red]
    : : : : }//nested-else
    : : : : }//while
    : : : : }//else
    : : : :
    : : : : return( gcd );
    : : : :
    : : : : }//gcdCalc
    : : : :
    : : : :
    : : : : public static void main( String args[] ) {
    : : : :
    : : : : int arg1 = Integer.parseInt( args[0] );
    : : : : int arg2 = Integer.parseInt( args[1] );
    : : : :
    : : : : int endGcd = gcdCalc( arg1, arg2 );
    : : : :
    : : : : System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
    : : : : }//main
    : : : :
    : : : : }//Gcd class
    : : : : [/code]
    : : : :
    : : : : The program as it stands will not compile and the errors I cannot seem to fix
    : : : :
    : : : : [b][blue]Errors:[/b]
    : : : :
    : : : : cannot resolve symbol
    : : : : symbol : variable gcd
    : : : : location: class Gcd
    : : : : gcd = int1;
    : : : : ^
    : : : : cannot resolve symbol
    : : : : symbol : variable gcd
    : : : : location: class Gcd
    : : : : return( gcd );
    : : : : ^
    : : : : [/blue]
    : : : :
    : : : : Any suggestions?
    : : :
    : : : The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.
    : :
    : : OK so I have declared the variable gcd's type (int), however the
    : : [blue]
    : : cannot resolve symbol
    : : symbol : variable gcd
    : : location: class Gcd
    : : return( gcd );
    : : ^[/blue]
    : : error still persists
    :
    : Has to do with the "scope" of the variable. Since you declared it inside a set of { } brackets, it is only visible from within that block of code. You need to declare it at the very top of your method, and then it'll be available in all parts of it.
    :

    OK changes have been made and the program will now print out the common divisor. Error checks to go



  • kentyladkentylad Posts: 27Member
    : [b][red]This message was edited by kentylad at 2004-5-2 6:20:33[/red][/b][hr]
    : : : [b][red]This message was edited by kentylad at 2004-5-2 3:29:59[/red][/b][hr]
    : : : : : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
    : : : : :
    : : : : : [code]
    : : : : : import java.lang.*;
    : : : : : import java.math.*;
    : : : : :
    : : : : : public class Gcd {
    :
    : : : : : [red]gcd = 0;[/red]
    :
    : : : : : public static int gcdCalc ( int int1, int int2 ) {
    : : : : :
    : : : : : if( int1 == int2) {
    : : : : : gcd = int1;
    : : : : : }//if
    : : : : :
    : : : : : else {
    : : : : : while ( int1 != int2) {
    : : : : :
    : : : : : if( int1 > int2 ) {
    : : : : : int1 = int1 - int2;
    : [red]gcd = int1;[/red]
    : : : : : }//nested-if
    : : : : :
    : : : : : else {
    : : : : : int2 = int2 - int1;
    : [red]gcd = int2;[/red]
    : : : : : }//nested-else
    : : : : : }//while
    : : : : : }//else
    : : : : :
    : : : : : return( gcd );
    : : : : :
    : : : : : }//gcdCalc
    : : : : :
    : : : : :
    : : : : : public static void main( String args[] ) {
    : : : : :
    : : : : : int arg1 = Integer.parseInt( args[0] );
    : : : : : int arg2 = Integer.parseInt( args[1] );
    : : : : :
    : : : : : int endGcd = gcdCalc( arg1, arg2 );
    : : : : :
    : : : : : System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
    : : : : : }//main
    : : : : :
    : : : : : }//Gcd class
    : : : : : [/code]
    : : : : :
    : : : : : The program as it stands will not compile and the errors I cannot seem to fix
    : : : : :
    : : : : : [b][blue]Errors:[/b]
    : : : : :
    : : : : : cannot resolve symbol
    : : : : : symbol : variable gcd
    : : : : : location: class Gcd
    : : : : : gcd = int1;
    : : : : : ^
    : : : : : cannot resolve symbol
    : : : : : symbol : variable gcd
    : : : : : location: class Gcd
    : : : : : return( gcd );
    : : : : : ^
    : : : : : [/blue]
    : : : : :
    : : : : : Any suggestions?
    : : : :
    : : : : The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.
    : : :
    : : : OK so I have declared the variable gcd's type (int), however the
    : : : [blue]
    : : : cannot resolve symbol
    : : : symbol : variable gcd
    : : : location: class Gcd
    : : : return( gcd );
    : : : ^[/blue]
    : : : error still persists
    : :
    : : Has to do with the "scope" of the variable. Since you declared it inside a set of { } brackets, it is only visible from within that block of code. You need to declare it at the very top of your method, and then it'll be available in all parts of it.
    : :
    :
    : OK changes have been made and the program will now print out the common divisor. Error checks to go
    [code]
    import java.lang.*;
    import java.math.*;

    public class Gcd {

    public static int gcdCalc ( int int1, int int2 ) {

    int gcd = 0;

    if( int1 == int2) {
    gcd = int1;
    }//if

    else {
    while ( int1 != int2) {

    if( int1 > int2 ) {
    int1 = int1 - int2;
    gcd = int1;
    }//nested-if

    else {
    int2 = int2 - int1;
    gcd = int2;
    }//nested-else
    }//while
    }//else

    return( gcd );

    }//gcdCalc


    public static void main( String args[] ) {

    int arg1 = Integer.parseInt( args[0] );
    int arg2 = Integer.parseInt( args[1] );
    int endGcd = gcdCalc( arg1, arg2 );

    [blue]if( args.length != 2) {
    System.out.println( "Error: Incorrect Number of Arguments" );
    System.exit( 1 );
    }//if

    if ( arg1 < 0 || arg2 < 0 ) {
    System.out.println( "Error: Negative Argument Entered" );
    System.exit( 1 );
    }//if[/blue]

    System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );

    }//main

    }//Gcd class
    [/code]

    As you can see I have two error check; one to test the correct number of arguments has been entered (this works fine), and on to test for a negative integer. The negative integer checl does not work for some reason although it does compile. It seems to loop infinitly. I cant exlicitly see anything wrong with the code however
  • kentyladkentylad Posts: 27Member
    : : [b][red]This message was edited by kentylad at 2004-5-2 6:20:33[/red][/b][hr]
    : : : : [b][red]This message was edited by kentylad at 2004-5-2 3:29:59[/red][/b][hr]
    : : : : : : I have been asked to create a java program which will calculate the Greatest Common Divisor of two integer arguments. I have so far got this:
    : : : : : :
    : : : : : : [code]
    : : : : : : import java.lang.*;
    : : : : : : import java.math.*;
    : : : : : :
    : : : : : : public class Gcd {
    : :
    : : : : : : [red]gcd = 0;[/red]
    : :
    : : : : : : public static int gcdCalc ( int int1, int int2 ) {
    : : : : : :
    : : : : : : if( int1 == int2) {
    : : : : : : gcd = int1;
    : : : : : : }//if
    : : : : : :
    : : : : : : else {
    : : : : : : while ( int1 != int2) {
    : : : : : :
    : : : : : : if( int1 > int2 ) {
    : : : : : : int1 = int1 - int2;
    : : [red]gcd = int1;[/red]
    : : : : : : }//nested-if
    : : : : : :
    : : : : : : else {
    : : : : : : int2 = int2 - int1;
    : : [red]gcd = int2;[/red]
    : : : : : : }//nested-else
    : : : : : : }//while
    : : : : : : }//else
    : : : : : :
    : : : : : : return( gcd );
    : : : : : :
    : : : : : : }//gcdCalc
    : : : : : :
    : : : : : :
    : : : : : : public static void main( String args[] ) {
    : : : : : :
    : : : : : : int arg1 = Integer.parseInt( args[0] );
    : : : : : : int arg2 = Integer.parseInt( args[1] );
    : : : : : :
    : : : : : : int endGcd = gcdCalc( arg1, arg2 );
    : : : : : :
    : : : : : : System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
    : : : : : : }//main
    : : : : : :
    : : : : : : }//Gcd class
    : : : : : : [/code]
    : : : : : :
    : : : : : : The program as it stands will not compile and the errors I cannot seem to fix
    : : : : : :
    : : : : : : [b][blue]Errors:[/b]
    : : : : : :
    : : : : : : cannot resolve symbol
    : : : : : : symbol : variable gcd
    : : : : : : location: class Gcd
    : : : : : : gcd = int1;
    : : : : : : ^
    : : : : : : cannot resolve symbol
    : : : : : : symbol : variable gcd
    : : : : : : location: class Gcd
    : : : : : : return( gcd );
    : : : : : : ^
    : : : : : : [/blue]
    : : : : : :
    : : : : : : Any suggestions?
    : : : : :
    : : : : : The error should be pretty clear. It says the error is cannot resolve symbol, and it says the symbol is variable gcd, so it's saying cannot resolve variable gcd. IOW, you need to declare the variable gcd before you try to use it.
    : : : :
    : : : : OK so I have declared the variable gcd's type (int), however the
    : : : : [blue]
    : : : : cannot resolve symbol
    : : : : symbol : variable gcd
    : : : : location: class Gcd
    : : : : return( gcd );
    : : : : ^[/blue]
    : : : : error still persists
    : : :
    : : : Has to do with the "scope" of the variable. Since you declared it inside a set of { } brackets, it is only visible from within that block of code. You need to declare it at the very top of your method, and then it'll be available in all parts of it.
    : : :
    : :
    : : OK changes have been made and the program will now print out the common divisor. Error checks to go
    : [code]
    : import java.lang.*;
    : import java.math.*;
    :
    : public class Gcd {
    :
    : public static int gcdCalc ( int int1, int int2 ) {
    :
    : int gcd = 0;
    :
    : if( int1 == int2) {
    : gcd = int1;
    : }//if
    :
    : else {
    : while ( int1 != int2) {
    :
    : if( int1 > int2 ) {
    : int1 = int1 - int2;
    : gcd = int1;
    : }//nested-if
    :
    : else {
    : int2 = int2 - int1;
    : gcd = int2;
    : }//nested-else
    : }//while
    : }//else
    :
    : return( gcd );
    :
    : }//gcdCalc
    :
    :
    : public static void main( String args[] ) {
    :
    : int arg1 = Integer.parseInt( args[0] );
    : int arg2 = Integer.parseInt( args[1] );
    : int endGcd = gcdCalc( arg1, arg2 );
    :
    : [blue]if( args.length != 2) {
    : System.out.println( "Error: Incorrect Number of Arguments" );
    : System.exit( 1 );
    : }//if
    :
    : if ( arg1 < 0 || arg2 < 0 ) {
    : System.out.println( "Error: Negative Argument Entered" );
    : System.exit( 1 );
    : }//if[/blue]
    :
    : System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
    :
    : }//main
    :
    : }//Gcd class
    : [/code]
    :
    : As you can see I have two error check; one to test the correct number of arguments has been entered (this works fine), and on to test for a negative integer. The negative integer checl does not work for some reason although it does compile. It seems to loop infinitly. I cant exlicitly see anything wrong with the code however
    :
    The single error for checking for negative integers is still not working, and proving to be the trailing factor in my program. Is the condition correct i.e. if( arg1 < 0 || ar2 < 0 )?

    Regards

  • ManningManning Posts: 1,621Member
    : The single error for checking for negative integers is still not working, and proving to be the trailing factor in my program. Is the condition correct i.e. if( arg1 < 0 || ar2 < 0 )?


    That looks fine to me. IIRC "boolean or" has a lower precedence than the "less than" and "greater than" operators, but just in case you could add brackets to make sure the order is what you want. So:

    [code]
    if ((arg1 < 0) || (arg2 < 0)) {
    // stuff here
    }
    [/code]
  • kentyladkentylad Posts: 27Member
    Unfortunatly the brakects dont seem to have worked. I'm almost 100% certain I am entering the correct arguments. For example, I pass in 10 & 100 as two arguments, and expect the GCD to be 10; which I get. However if I pass in 100 and -10; the program seems to go one infinitly.

    As I said before; all other error checks work - it's just this blighter!

  • ManningManning Posts: 1,621Member
    [b][red]This message was edited by Manning at 2004-5-3 16:23:51[/red][/b][hr]
    : Unfortunatly the brakects dont seem to have worked. I'm almost 100% certain I am entering the correct arguments. For example, I pass in 10 & 100 as two arguments, and expect the GCD to be 10; which I get. However if I pass in 100 and -10; the program seems to go one infinitly.
    :
    : As I said before; all other error checks work - it's just this blighter!


    Ahh I just took a closer look at the code. You have your error checking AFTER you call your gcdCalc() function. It needs to go before of course, otherwise it's kind of useless.
  • kentyladkentylad Posts: 27Member
    Good man Manning problem sorted
«1
Sign In or Register to comment.