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

    Default cant set ints from constructor! Helpp!

    Okay so I am having this problem, and its a silly one but I am not sure what it is..

    Java Code:
    package ZooZ;
    
    
    import java.util.Random;
    public class Animal {
        
       int playerOne;    <------- Remains 0
        int playerTwo;  <------- Remains 0
        
      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 Animal(int pO, int pT){                            <----------- This constructor assigns playerOne and playerTwo to the values. But outside of the constructor they do not set. and they remain 0.
          playerOne = pO;
          playerTwo = pT;
          System.out.println(playerTwo);        <-------- Returns 2 like it should, but outside the constructor it stays one.
      }
    
      
      public void Bash(){
          System.out.println(playerTwo);           <--------- This returns 0. But should be 2
       System.out.println("\n| You bash the other animal |");
       if(playerTwo == 2){
       int Damage = random.nextInt(25+1);
       Lion lion1 = new Lion();
       System.out.printf("Damage hit on Player: %d", Damage);
       lion1.loseHealth(Damage);
       }
       
       
      }
      public void Bite(){
       System.out.println("You bash the other animal.");
       
      }
       public void Defend(){
       System.out.println("You bash the other animal.");
       
      }
      
    }
    If you look at the code, I set "playerOne" and "playerTwo", to set them as what was passed in from another class..
    In the Animal constructor.

    in that constructor the value is 2. But when I use it in the "Bash()" method it is 0 in the console.

    Why doesnt the playerOne and playerTwo stay the value it is assigned in the constructor.??

    Thanks for reading.
    Last edited by DayKema; 08-10-2014 at 03:58 AM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,804
    Rep Power
    5

    Default Re: cant set ints from constructor! Helpp!

    Quote Originally Posted by DayKema View Post
    If you look at the code, I set "playerOne" and "playerTwo", to set them as what was passed in from another class..
    In the Animal constructor.
    I am looking at the code and I don't see where you call the constructor and set anything. Your error is probably
    elsewhere (perhaps in some other class).

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

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

    Default Re: cant set ints from constructor! Helpp!

    Quote Originally Posted by jim829 View Post
    I am looking at the code and I don't see where you call the constructor and set anything. Your error is probably
    elsewhere (perhaps in some other class).

    Regards,
    Jim
    public Animal(int pO, int pT){ <----------- This constructor assigns playerOne and playerTwo to the values. But outside of the constructor they do not set. and they remain 0.
    playerOne = pO;
    playerTwo = pT;
    System.out.println(playerTwo); <-------- Returns 2 like it should, but outside the constructor it stays one.
    }

    the arguments were passed in and set to playerOne, and playerTwo.

    But outside of the constructor they are equal to 0, but should be two.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: cant set ints from constructor! Helpp!

    But you do not show us the code where you apparently call the constructor. Until we see valid code that properly creates one or more Animal objects we cannot help you.

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

    Default Re: cant set ints from constructor! Helpp!

    Quote Originally Posted by Junky View Post
    But you do not show us the code where you apparently call the constructor. Until we see valid code that properly creates one or more Animal objects we cannot help you.
    Sure you can. The variable is passed in and it works. But outside of the constructor in Animal in other methods its still set to 0.

    But heres the code.

    Java Code:
    package ZooZ;
    
    /**
     *
     * @author Alexander
     */
    
    import java.util.Scanner;
    public class Fight {
        
        
        Scanner scanner = new Scanner(System.in);
        //TestDrive PlayerOne = new TestDrive();
        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 void Begin(int pO, int pT){ 
            
      
            
            Animal animal = new Animal(pO, pT); <--- Made a object passing in variables pO, and pT.
            
    
            
            System.out.println("\n\nThe Fight has begun, the first player may choose his move");
            System.out.printf("playerOne : %s\n", pO);
            System.out.printf("playerTwo : %s\n", pT);
            
            String attack1 = scanner.nextLine();
            
           
            
            if(pO == 1){
                if(attack1.equals("Bash")){
                elephant.Bash();
                }
                if(attack1.equals("Bite")){
                elephant.Bite();
                }
                if(attack1.equals("Defend")){
                elephant.Defend();
                }   
            } else if(pO == 2){
                if(attack1.equals("Bash")){
                lion.Bash();
                }
                if(attack1.equals("Bite")){
                lion.Bite();
                }
                if(attack1.equals("Defend")){
                lion.Defend();
            } else if(pO == 3){
                if(attack1.equals("Bash")){
                bear.Bash();
                }
                if(attack1.equals("Bite")){
                bear.Bite();
                }
                if(attack1.equals("Defend")){
                bear.Defend();
                }
            }
            }
            
    
           // System.out.println(playerOne);
        }
        
    }

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,804
    Rep Power
    5

    Default Re: cant set ints from constructor! Helpp!

    Quote Originally Posted by DayKema View Post
    Sure you can. The variable is passed in and it works.
    So you come here and ask this forum to help you solve a problem. Then when we request more information you
    tell us we don't need it. And you also say the program works. Well apparently it doesn't. Since you did not
    include the other classes (Lion, Bear, Elephant) I can only make an assumption. That is that you subclass Animal
    class in each of those. But then create an instance of Animal which is totally different. I also think you
    may be confusing which methods are called and getting your instance fields mixed up. You only set your player
    numbers in your instance of Animal but not in your other classes (Lion, etc). Yet your Animal instance never
    calls Bash(), only the other classes.

    Others may be able to tell you what is wrong with the information provided but I cannot. I can only guess.

    Regards,
    Jim
    Last edited by jim829; 08-11-2014 at 01:31 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 10:50 AM
  2. Identifying Ints and Non Ints in a string
    By Mnelson in forum New To Java
    Replies: 6
    Last Post: 07-07-2012, 01:08 AM
  3. helpp
    By amzers in forum New To Java
    Replies: 1
    Last Post: 02-05-2011, 09:43 AM
  4. helpp lol
    By ryzii in forum New To Java
    Replies: 3
    Last Post: 03-26-2009, 03:56 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
  •