Results 1 to 5 of 5
  1. #1
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Passing a variable between classes

    Hi, I have two files one called MyProgram.java and one called GuessingGame.java(which I got off a java site). My aim is to create 5 rounds of different mini games. Ok, so what I want to do is keep track of the result of each game, then at the end do some calculations. What I want to do is pass the 'numberOfTries' variable from the GuessingGame class into the game2 method in the MyProgram class.

    Here is my code, first one is my own, second one is copied.

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class MyProgram {
    
         public static void main(String[] args) {
    	 
    	     boolean decide; 
    		 String input;
    		 int tracker = 0; 
    	 
             String[] questions = new String[11];
    		  
    		 questions[1] = "What is the largest continent?";
    		 questions[2] = "Which animal has black and white stripes?";
    		 questions[3] = "What is a skeleton made of?";
    		 questions[4] = "Where do bats live?";
    		 questions[5] = "Where does a king and queen live?";
    		 questions[6] = "What colour does a rainbow start with?";
    		 questions[7] = "What sport does Wayne Rooney play?";
    		 questions[8] = "What animal goes 'oink oink'?";
    		 questions[9] = "Which planet is the nearest to Earth?";
    		 questions[10] = "What colour is a banana?";
    		 
    		 String[] PosAns = new String[49];
    	 
    		 PosAns[1] = "asia";
    		 PosAns[2] = "Asia";
    		 PosAns[3] = "ASIA";
    		 PosAns[4] = "zebra";
    		 PosAns[5] = "Zebra";
    		 PosAns[6] = "zebras";
    		 PosAns[7] = "Zebras";
    		 PosAns[8] = "ZEBRAS";
    		 PosAns[9] = "ZEBRA";
    		 PosAns[10] = "BONE";
    		 PosAns[11] = "BONES";
    		 PosAns[12] = "bone";
    		 PosAns[13] = "bones";
    		 PosAns[14] = "Bone";
    		 PosAns[15] = "Bones";
    		 PosAns[16] = "cave";
    		 PosAns[17] = "caves";
    		 PosAns[18] = "Cave";
    		 PosAns[19] = "Caves";
    		 PosAns[20] = "CAVES";
    		 PosAns[21] = "CAVE";
    		 PosAns[22] = "castle";
    		 PosAns[23] = "castles";
    		 PosAns[24] = "Castle";
    		 PosAns[25] = "Castles";
    		 PosAns[26] = "CASTLE";
    		 PosAns[27] = "CASTLES";
    		 PosAns[28] = "red";
    		 PosAns[29] = "Red";
    		 PosAns[30] = "RED";
    		 PosAns[31] = "FOOTBALL";
    		 PosAns[32] = "Football";
    		 PosAns[33] = "football";
    		 PosAns[34] = "SOCCER";
    		 PosAns[35] = "Soccer";
    		 PosAns[36] = "soccer";
    		 PosAns[37] = "pig";
    		 PosAns[38] = "PIG";
    		 PosAns[39] = "Pig";
    		 PosAns[40] = "PIGS";
    		 PosAns[41] = "Pigs";
    		 PosAns[42] = "pigs";
    		 PosAns[43] = "moon";
    		 PosAns[44] = "Moon";
    		 PosAns[45] = "MOON";
    		 PosAns[46] = "YELLOW";
    		 PosAns[47] = "yellow";
    		 PosAns[48] = "Yellow";
    		 
    		 System.out.println("Welcome to my custom made, fun and testing game.");
    		 System.out.println("Are you brainy enough to tackle the questions?!?");
    		 System.out.println("");
    		 System.out.println("The first game is a simple question and answer excercise.");
    		 System.out.println("You will be asked various questions which you will be asked to answer.");
    		 System.out.println("--------------------------------------------------------------------------------");
    		 System.out.println("Round 1 Commencing...");
    		 System.out.println("");
    		 
    		 for (int dx=1;dx<=questions.length-1;dx++) { 
    		 
    		     decide = false;
    
                 System.out.println(questions[dx]); 
    
                 Scanner scan = new Scanner(System.in); 
    		     input = scan.nextLine(); 
    			 
    			 for (int ab=1;ab<PosAns.length-1;ab++) { 
    			 
    			     if(PosAns[ab].equals(input)) { 
                         decide = true;
    				 }
                 }
    			 
    			 if (decide == true) {
    			 
    			     System.out.println("Correct!"); 
    			     System.out.println(""); 
    				 tracker++; 
    			 }
    			 else {
    			     System.out.println("Wrong!"); 
    			     System.out.println(""); 
    			 }
    		 } 
    		 System.out.println("");
             System.out.println("You scored " + tracker + "/10"); 
    		 System.out.println("");
    		 
    		 game2(tracker);
    	 }
    	 
    	 public static void game2(int tracker) {
    	 
    	     System.out.println("In the last round you scored " + tracker + "/10.");
    	     System.out.println("This will count towards your overall score.");
    	     System.out.println("");
    	     System.out.println("The next round is a numeric based guessing game.");
    	     System.out.println("The aim of the game is to complete it with the lowest number of guesses.");
    	  
    	     GuessingGame.method();
    		 
    	 }
    }
    
    //END OF PROGRAM++
    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class GuessingGame {
    
         public static void main(String[] args) {
    	 
    	     method();
    		 
    	 }
    	 public static void method() {
    	 
    	     Random rand = new Random();
    		 int numberToGuess = rand.nextInt(1000);
    		 int numberOfTries = 0;
    		 Scanner input = new Scanner(System.in);
    		 int guess;
    		 boolean win = false;
    		 
    		 while (win == false) {
    		 
    		 
    		     System.out.println("Guess a number between 1 and 1000: ");
    	         guess = input.nextInt();
    		     numberOfTries++;
    		 
    		     if (guess == numberToGuess) {
    		         
    				 win = true;
    		     
    		     }
    		     else if (guess < numberToGuess) {
    		 
    		         System.out.println("Your guess is too low!");
    		 
    		     }
    		     else if (guess > numberToGuess) {
    		 
    		         System.out.println("Your guess is too high!");
    		 
    		     }
    		 }
    		 
    		 System.out.println("You win!");
    		 System.out.println("The number was " + numberToGuess + ".");
    		 System.out.println("It took you " + numberOfTries + " tries.");
    		 
    	 }
    
    }
    Thank you in advance for any information.
    Last edited by Nanomech; 08-20-2011 at 11:08 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You can't really do this as the variables are local to static methods. What you really want to do is to re-write both classes to have non-static fields and methods as well as constructors, in other words create true object oriented classes. You also should be careful when "borrowing" code off the internet. Better that you borrow concepts from the code and use the concepts in your programs rather than borrow and use code at the risk of not understanding what it's doing.

  3. #3
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    Thank you for the guidance, i'm going to have a look into changing these now.

    I do understand the borrowed code in terms of what it actually does, but when it comes to the method keywords, i'm still a little unsure of how to use them. I understand how to pass variables between methods in the same class, but just couldn't figure out how to pass between 2 different classes. I've only picked up Java 2/3 weeks ago and i'm currently learning off this site: Java Programming Made Easy - Learn to Program Today!.

    As a side note, is there any good sites you could recommend for a total beginner? I'm familiar with JavaScript and web based design so I can see little similarities in the code etc but i need a site for an absolute beginner.

    Google has not been my friend this time :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you haven't done so, please have a look at the official Java tutorials her: The Java Tutorials. You'll find a wealth of information there.

    Best of luck and welcome to the forum and to Java!

  5. #5
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    Ah thanks but I already have it bookmarked but the terminology is what I can't really understand. I need something which explains it in 'layman terms' if you like. I'm gonna have another go at understanding this site.

    Thanks again for the help.

Similar Threads

  1. Passing a variable problem.
    By kaizen9001 in forum New To Java
    Replies: 12
    Last Post: 04-30-2011, 04:02 PM
  2. Passing Variable In Frame
    By amitabh in forum JDBC
    Replies: 2
    Last Post: 06-07-2010, 04:36 PM
  3. passing variable in trees??
    By player123 in forum Advanced Java
    Replies: 2
    Last Post: 02-03-2009, 12:42 PM
  4. Need help passing data between classes
    By bri1547 in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 04:19 AM
  5. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 08:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •