Help me finish this program :(
This is due this Saturday and the compiler just keeps sending me error. Probably because I am not that knowledgeable enough with JAVA yet so I hope you guys can help me with what is wrong with my code and help me understand more about JAVA Language.
Code :
import java.util.Random;
public class Case{
int[] cases = {1 , 2 , 5 , 10 , 25 , 50 , 75,
100 , 200 , 300 , 400 ,500,750, 1000 ,
5000 , 10000 , 25000 , 50000 , 75000 , 100000 ,
200000 , 300000 , 400000 , 500000 , 750000 , 1000000 };
void shuffle(){
Random rndNumbers = new Random();
for (int i=0; i<cases.length; i++) {
int randomPosition = rndNumbers.nextInt(cases.length);
int temp = cases[i];
cases[i] = cases[randomPosition];
cases[randomPosition] = temp;
}
for (int j=0; j!=26; j++ ){
System.out.println( cases[j] );
}
}
}
This is my first class which is the Case.java
It holds that value of the 26 Briefcases.
When I run it in the compiler it works fine.
But when I remove void shuffle() wherein there will be no method inside the code, it will not compile.
It says Illegal start of type and identifiers expected.
My question is it really neccesary to make it a method?
Code :
public class Player{
int myBriefcase;
int myCaseIndex;
void chooseCase(){
System.out.println("Choose:");
myCaseIndex = SavitchIn.readLineInt();
myBriefcase = cases[myCaseIndex];
System.out.println("You chose briefcase number" + myCaseIndex);
}
int i=26;
int remove;
void removeCase(){
while ( i != 1){
System.out.println("Remove what");
remove = SavitchIn.readLineInt();
if ( remove > 25 ){
System.out.println("Out of Bounds");
removeCase();
}
else if ( cases[remove] == 0 || cases[remove] == cases[myCaseIndex]){
System.out.println("It has been chosed already.");
removeCase();
}
else{
cases[remove] = 0;
i--;
}
}
}
void openCase(){
System.out.println("Inside briefcase is" + cases[myCaseIndex]);
}
}
Here is the next class, which is the Player.
When I make this my main code, which is by adding public static void main (String[] args) on the top of the code AND putting the int[] case in the code (which is now in the Case.java), it works pretty fine. Though this is not my main code, so I remove that.
The problem is when I compile this it gives the error that it cannot find the symbol in cases.
How can I access the int[] cases which is in the Case.java? How do I access a variable which is in another class?
I read somewhere that I should use abstarct class or interface though I am not quite familiar with it that much.
//I have two or three more class but for now this is my queries. Hope you can help me guys. :)
Re: Help me finish this program :(
Ok, in answer to your first question, you do not have to put the code into a method, it can be placed into a class constructor. This will be called when the object of that class is created:
Case()
{
//your code here
}
secondly, you can access the variables of other classes by adding some getter and setter methods in the class case
E.G
public int[][] getCases()
{
return cases;
}
As a footnote i just want to add that it is usually good practice to make class variables private and to add getter and setter methods to access them.