: :
This message was edited by kentylad at 2004-5-2 6:20:33
: : : :
This message was edited by kentylad at 2004-5-2 3:29:59
: : : : : : 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:
: : : : : :
: : : : : :
: : : : : : import java.lang.*;
: : : : : : import java.math.*;
: : : : : :
: : : : : : public class Gcd {
: :
: : : : : : gcd = 0;
: :
: : : : : : public static int gcdCalc ( int int1, int int2 ) {
: : : : : :
: : : : : : 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 );
: : : : : :
: : : : : : System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
: : : : : : }//main
: : : : : :
: : : : : : }//Gcd class
: : : : : :
: : : : : :
: : : : : : The program as it stands will not compile and the errors I cannot seem to fix
: : : : : :
: : : : : :
Errors:
: : : : : :
: : : : : : 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
: : : :
: : : : cannot resolve symbol
: : : : symbol : variable gcd
: : : : location: class Gcd
: : : : return( gcd );
: : : : ^
: : : : 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
:
: 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 );
:
: 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
:
: System.out.println( "The GCD for Integers " + arg1 + " and " + arg2 + " is " + endGcd );
:
: }//main
:
: }//Gcd class
:
:
: 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