Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help me finish this program :(

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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?

    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.


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Location
    UK
    Posts
    3
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Can anyone help me finish creating a GUI for my program
    By danbendlin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 16th, 2012, 07:34 PM
  2. Get out of loop to finish program?
    By Doofy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 17th, 2011, 08:34 AM
  3. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM
  4. tax return program..help finish please!!
    By dscrudato21xo in forum Loops & Control Statements
    Replies: 2
    Last Post: November 5th, 2009, 03:23 PM