Computer Science Canada

Subroutines

Author:  Neo [ Sat Jan 22, 2005 7:02 pm ]
Post subject:  Subroutines

Why do I get an error at cardDraw in the subroutine? It says cannot resolve symbol.
code:

public class BlackJack{
       
        public static void main (String[]args){
                boolean[][] cardDrawn=new boolean[13][4];

        }//end of MAIN

        static int SELECT_CARD(int tempValue, int tempSuit){
                if (cardDraw[tempValue][tempSuit]=true){
                        return 0;
                }
        }

}//end of BlackJack class

Author:  Hikaru79 [ Sat Jan 22, 2005 7:07 pm ]
Post subject: 

Simply because the variable you declared is cardDrawn, but you CALL cardDraw with no 'n'.

But you'll still get an error. You need to tell the function what to return assuming the 'if' condition is false.

EDIT: Wait, I don't think it'll work anyway because cardDraw is declared inside main, so it's not global. There's quite a few things wrong with that code =P

Author:  Neo [ Sat Jan 22, 2005 7:26 pm ]
Post subject: 

Ok so I declare it outside main to make it global? Now it says missing return statment and poiints to line 11...
BTW this is just a snipet of my code, I just need this part to compile correctly.

code:

public class BlackJack{
        boolean[][] cardDrawn=new boolean[13][4];
       
        public static void main (String[]args){
        }//end of MAIN

        public int SELECT_CARD(int tempValue, int tempSuit){
                if (cardDrawn[tempValue][tempSuit]=true){
                        return 0;
                }
        }
}//end of BlackJack class

Author:  wtd [ Sat Jan 22, 2005 7:27 pm ]
Post subject: 

There are other problems. The array is declared locally within main. It cannot then be referenced in another method unless passed in as an argument.

Author:  Hikaru79 [ Sat Jan 22, 2005 7:39 pm ]
Post subject: 

I've already told you why Smile
Hikaru79 wrote:

But you'll still get an error. You need to tell the function what to return assuming the 'if' condition is false.


What will the method return if cardDrawn[tempValue][tempSuit]=false ? You need to give it a backup plan. Also, by convention, method names shouldn't be in all caps.

Author:  Neo [ Sat Jan 22, 2005 7:44 pm ]
Post subject: 

Hikaru79 wrote:

But you'll still get an error. You need to tell the function what to return assuming the 'if' condition is false.


Thanks Hikaru, my eyes kinda jumped that line Laughing .

Author:  wtd [ Sat Jan 22, 2005 7:49 pm ]
Post subject: 

Hikaru79 wrote:
I've already told you why Smile
Hikaru79 wrote:

But you'll still get an error. You need to tell the function what to return assuming the 'if' condition is false.


What will the method return if cardDrawn[tempValue][tempSuit]=false ? You need to give it a backup plan. Also, by convention, method names shouldn't be in all caps.


Nor should they include underscores.

Also

code:
cardDrawn[tempValue][tempSuit]=false


Will always return false. Perhaps you meant:

code:
cardDrawn[tempValue][tempSuit] == false

Author:  Neo [ Sat Jan 22, 2005 10:06 pm ]
Post subject: 

wtd wrote:

code:
cardDrawn[tempValue][tempSuit]=false


Will always return false. Perhaps you meant:

code:
cardDrawn[tempValue][tempSuit] == false


Yea these little things catch me every time, I'm too used to turing.

Author:  Neo [ Sun Jan 23, 2005 1:33 pm ]
Post subject: 

If I declare a variable outside the main routine why won't it let me use it from inside the main routine? And why cant I call the funciton from within main? It says non-static method cannot be referenced from a static context.

EDIT: If I declare the variables and function as static I get no error... anyone care to explain why?

Author:  Hikaru79 [ Sun Jan 23, 2005 3:44 pm ]
Post subject: 

Do you understand the difference between static and non-static? Read here: http://leepoint.net/notes-java/20language/40methods/50static-methods.html


: