Results 1 to 5 of 5
  1. #1
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    23
    Rep Power
    0

    Default Passing in a variable from one class to another!?

    It just wont work... Everytime I try to pass a variable into a class it sets it to 0. Ughh I have no clue what to do.

    The variable should be '2', but it prints out as 0!?


    Java Code:
    package Zoo;
    
    
    import java.util.Random;
    public class Animal {
        
    
        
      Random random = new Random();
      
      
      public void Eat(){
       System.out.println("The animal starts to eat."); 
      }
      
      public void Sleep(){
       System.out.println("The animal goes to sleep"); 
      }
      
      public Animal(){
      }
      
          public int playerOne = 0;
        public int playerTwo = 0;
      
      public Animal(int pO, int pT){   <-----------  ANIMAL CONSTRUCTOR PASSES IN 2 FOR pT AND SETS IT AS playerTwo.
          this.playerOne = pO;
          this.playerTwo = pT;
          System.out.println(playerTwo); <--------- PRINTS OUT 2 LIKE IT SHOULD.
          playerOne(pO);
         // playerTwo();
      }
      
    
      public int playerOne(int pO){
        if(pO == 1){
          return 1;
        }
            if(pO == 2){
          return 2;
        }
                if(pO == 3){
          return 3;
        }
      return 0;
      }
        public int playerTwo(){
      return 0;
      }
        
            public int playerOne(){
      return 0;
      }
    
      
    
      
      public void Bash(){
    
       System.out.println(playerTwo); <---- Prints out 0.
       System.out.println("\n| You bash the other animal |");
       if(playerTwo == 2){
       int Damage = random.nextInt(25+1);
       Lion lion1 = new Lion();
       lion1.loseHealth(Damage);
       //System.out.printf("Damage hit on Player: %d\n", Damage);
       }
       if(playerOne == 1){
       int Damage = random.nextInt(25+1);
       Lion lion1 = new Lion();
       lion1.loseHealth(Damage);
       //System.out.printf("Damage hit on Player: %d\n", Damage);
       }
       
       
      }
      public void Bite(){
       System.out.println("You bash the other animal.");
       
      }
       public void Defend(){
       System.out.println("You bash the other animal.");
       
      }
      
    }





    Java Code:
    package Zoo;
    
    /**
     *
     * @author Alexander
     */
    
    import java.util.Scanner;
    public class Fight {
        
        
        Scanner scanner = new Scanner(System.in);
        Elephant elephant = new Elephant("Elephant", "Big", "Full", 100);
        Lion lion = new Lion("Lion", "Strong", "Full", 100);
        Bear bear = new Bear("Bear", "Powerful", "Full", 100);
        
    
        
        public int playerOne = 0;
        public int playerTwo = 0;
        
         public Fight(int pO, int pT){
         this.playerOne = pO;
         this.playerTwo = pT;
        }
         
         public Fight(){
    
        }
        
        
             public int getPlayerTwo(){
         return this.playerTwo; 
        }
             
            
             
         
        
        public void Begin(){
          
          
          System.out.println(playerOne);
            System.out.println(playerTwo);
           
            Animal animal = new Animal(playerOne, playerTwo);
            
    
            
            System.out.println("\n\nThe Fight has begun, the first player may choose his move");
            System.out.printf("playerOne : %s\n", playerOne);
            System.out.printf("playerTwo : %s\n", playerTwo);
            
            String attack1 = scanner.nextLine();
            
           
            
            if(playerOne == 1){
                if(attack1.equals("Bash")){
                elephant.Bash();
                }
                if(attack1.equals("Bite")){
                elephant.Bite();
                }
                if(attack1.equals("Defend")){
                elephant.Defend();
                }   
            } else if(playerOne == 2){
                if(attack1.equals("Bash")){
                lion.Bash();
                }
                if(attack1.equals("Bite")){
                lion.Bite();
                }
                if(attack1.equals("Defend")){
                lion.Defend();
            } else if(playerOne == 3){
                if(attack1.equals("Bash")){
                bear.Bash();
                }
                if(attack1.equals("Bite")){
                bear.Bite();
                }
                if(attack1.equals("Defend")){
                bear.Defend();
                }
            }
            }
            
            System.out.printf("\n|| Player Two May Choose His Attack ||\n");
    
            String attack2 = scanner.nextLine();
            
                   if(playerTwo == 1){
                if(attack1.equals("Bash")){
                elephant.Bash();
                }
                if(attack1.equals("Bite")){
                elephant.Bite();
                }
                if(attack1.equals("Defend")){
                elephant.Defend();
                }   
            } else if(playerTwo == 2){
                if(attack1.equals("Bash")){
                lion.Bash();
                }
                if(attack1.equals("Bite")){
                lion.Bite();
                }
                if(attack1.equals("Defend")){
                lion.Defend();
            } else if(playerTwo == 3){
                if(attack1.equals("Bash")){
                bear.Bash();
                }
                if(attack1.equals("Bite")){
                bear.Bite();
                }
                if(attack1.equals("Defend")){
                bear.Defend();
                }
            }
            }
         
            
    
           // System.out.println(playerOne);
        }
        
             public int getPlayerOne(){
         return this.playerOne;
        }
        
    
         
    
        
    }
    The method bash, needs the variable 'playerTwo' to be 2, to carry on. But it prints out 0.

    Int the animal constructor, I passed in the variable from another class, and it prints out 2 in the constructor like it should. But outside of the constructor it is equal to 0.

    Any ideas?
    Last edited by DayKema; 08-16-2014 at 07:41 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Passing in a variable from one class to another!?

    Not for me, that particular line in Bash prints out 2 like it should, at least when I strip away all the fluff and narrow the code down to the bare basics.

    Java Code:
    import java.util.Random;
    public class Test {
         
      Random random = new Random();
       
      public Test(){
      }
       
          public int playerOne = 0;
        public int playerTwo = 0;
       
      public Test(int pO, int pT){  
          this.playerOne = pO;
          this.playerTwo = pT;
          System.out.println(playerTwo); // prints 2
          playerOne(pO);
         // playerTwo();
      }
       
     
      public int playerOne(int pO){
        if(pO == 1){
          return 1;
        }
            if(pO == 2){
          return 2;
        }
                if(pO == 3){
          return 3;
        }
      return 0;
      }
        public int playerTwo(){
      return 0;
      }
         
            public int playerOne(){
      return 0;
      }
     
      public void Bash(){
     
       System.out.println(playerTwo); // prints 2
      }
    
      public static void main(String[] args){
    	Test test = new Test(1,2);
    	test.Bash();
      }
    }
    What I do notice is that the Bash.playerTwo() method, which has the same name as the variable you've been using, does return 0 always. Perhaps there is a confusion there somewhere.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Passing in a variable from one class to another!?

    Sounds similar to cant set ints from constructor! Helpp!

    Apparently, the OP didn't like my last reply to that thread.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Passing in a variable from one class to another!?

    Well we can't help people who don't want to listen, learn or think. I'm out.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    DayKema is offline Member
    Join Date
    Jul 2014
    Posts
    23
    Rep Power
    0

    Default Re: Passing in a variable from one class to another!?

    Quote Originally Posted by gimbal2 View Post
    Well we can't help people who don't want to listen, learn or think. I'm out.
    I can, I am just stumbled. I would give you all the code if I could. Thanks for your input anyways.

Similar Threads

  1. Replies: 10
    Last Post: 06-26-2014, 10:04 AM
  2. Replies: 2
    Last Post: 01-08-2014, 06:09 PM
  3. Passing a variable between classes
    By Nanomech in forum New To Java
    Replies: 4
    Last Post: 08-22-2011, 01:29 PM
  4. Passing Variable In Frame
    By amitabh in forum JDBC
    Replies: 2
    Last Post: 06-07-2010, 05:36 PM
  5. passing variable in trees??
    By player123 in forum Advanced Java
    Replies: 2
    Last Post: 02-03-2009, 01:42 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
  •