Results 1 to 4 of 4
  1. #1
    sfaiz is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Question how to use objects and its values to use in other function without using parameters

    Hi. I am quite new to Java programming. I have got the following Game class and a Game() constructor where I am strictly advised to initialise all the variables.
    I was also advised to create a toString() method to transfer the values of EnemyShip and PlayerShip objects and use those values in the toString() method. I initially did this using parameter in the toString() method, as per below code. But I was later told that I have to accomplish this without using any parameters. So my question is how do I use the same EnemyShip 'e1', 'e2', and 'e3' objects and PlayerShip 'p1' objects and its values to use in the toString() method without using parameters?
    Java Code:
                                                                                                        
    import java.awt.*;
    import java.util.Scanner;
    import java.lang.*;
    
    public class Game
    {
        private int e1Pos;
        private int e2Pos;
        private int e3Pos;
        private int e1Vel;
        private int e2Vel;
        private int e3Vel;
        private int playerPoints;
            
        public Game()
        {
             PlayerShip p1;
             EnemyShip e1;
             EnemyShip e2;
             EnemyShip e3;
             
             p1 = new PlayerShip();      
             e1 = new EnemyShip();
             e2 = new EnemyShip();
             e3 = new EnemyShip();
            
             Scanner keyboard = new Scanner(System.in);    
            
             System.out.println("Enemy #1");
             System.out.print("- Initial x position:"+" ");
                int e1Pos = keyboard.nextInt();
             System.out.print("- Initial velocity:"+" ");
                int e1Vel = keyboard.nextInt();
             System.out.println("Enemy #2");
             System.out.print("- Initial x position:"+" ");
                int e2Pos = keyboard.nextInt();
             System.out.print("- Initial velocity:"+" ");
                int e2Vel = keyboard.nextInt();
             System.out.println("Enemy #3");
             System.out.print("- Initial x position:"+" ");
                int e3Pos = keyboard.nextInt();
             System.out.print("- Initial velocity:"+" ");
                int e3Vel = keyboard.nextInt();   
                
             p1.setxPosition(0);
             p1.setPlayerPoints(0);
             
             e1.setXposition(e1Pos);
             e1.setVelocity(e1Vel);
               
             e2.setXposition(e2Pos);
             e2.setVelocity(e2Vel);
               
             e3.setXposition(e3Pos);
             e3.setVelocity(e3Vel);
             
         }
        
    
        public String toString(EnemyShip e1, EnemyShip e2, EnemyShip e3, PlayerShip p1 )
    
        {
            String e1String;
            String e2String;
            String e3String;
            String outputString;
     
            boolean e1Destroyed = e1.getDestroyed();
            boolean e2Destroyed = e2.getDestroyed();
            boolean e3Destroyed = e3.getDestroyed();
            
            int playerPoints = p1.getPlayerPoints();
            int playerPos = p1.getxPosition();
    
        int e1Pos =  e1.getxPosition();
        int e2Pos =  e2.getxPosition();
        int e3Pos =  e3.getxPosition();
            
            if ( e1Destroyed = true)
                 e1String = "Enemy(" +e1Pos+ ")*";
            else  
                 e1String = "Enemy(" +e1Pos;
        
            if ( e2Destroyed = true)
                 e2String = "Enemy(" +e2Pos+ ")*";
            else  
                 e2String = "Enemy(" +e2Pos;
            
            if ( e3Destroyed = true)
                 e3String = "Enemy(" +e3Pos+ ")*";
            else  
                 e3String = "Enemy(" +e3Pos;
               
            outputString = e1String + e2String + e3String + " Player[" + playerPos + "," + " " + playerPoints +"pts]";
            return outputString;
        }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Instead of passing them as values, when the constructor initializes, save e1 e2 and e3 as member variables. Then, the toString() method doesn't need them to be passed; it can retrieve them from the class itself. Something like this:
    Java Code:
    public class x {
        int a = 0;
        public x() {
            a = 5;
        }
        public String toString() {
            return a+"";
        }
    }
    ...that kind of idea.

    Good luck!

  3. #3
    sfaiz is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Question

    Hi,

    In the constructor i have declared p1 and e1 as objects of the
    PlayerShip and EnemyShip class(as per my code below). And in the toString() method when I try to get the values of the p1 and e1objects using the getter methods, such as 'boolean e1Destroyed = e1.getDestroyed()', then the compiler shows error on the toString() method stating it cannot identify the getter method 'e1.getDestroyed()'. My code for the constructor and toString() method is shown below.

    Could you please advise as to where am I getting it wrong here??

    Java Code:
    import java.awt.*;
    import java.util.Scanner;
    import java.lang.*;
    
    public class Game
    {
        private int e1Pos;
        private int e1Vel;
            
        public Game()
        {
       	PlayerShip p1;
        	EnemyShip e1;
          
        	p1 = new PlayerShip();      
        	e1 = new EnemyShip();
    
             Scanner keyboard = new Scanner(System.in);    
            
             System.out.println("Enemy #1");
             System.out.print("- Initial x position:"+" ");
                 e1Pos = keyboard.nextInt();
             System.out.print("- Initial velocity:"+" ");
                 e1Vel = keyboard.nextInt();
             
             p1.setxPosition(0);
             p1.setPlayerPoints(0);
             
             e1.setXposition(e1Pos);
             e1.setVelocity(e1Vel);
         }
        
    
        public void toString()
    
        { 
            boolean e1Destroyed = e1.getDestroyed();
            
            int playerPoints = p1.getPlayerPoints();
            int playerPos = p1.getxPosition();
    
    	int e1Pos =  e1.getxPosition();
    
            
        }     
    }

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by sfaiz View Post
    Could you please advise as to where am I getting it wrong here??

    hello, p1 and e1 are declared as local variables of the constructor, so these variables can not be seen outside the constructor block. in order to use these variables in the toString()-method, declare it as instance/member variables of the Game, example

    Java Code:
    public class Game
    {
        private int e1Pos;
        private int e1Vel;
        
        PlayerShip p1;
        EnemyShip e1;
        
        public Game()
        { // other stuff

    now, you can use p1 and e1 also in the toString()-method. other point: in order to overwrite the toString-method your signature must look like

    public String toString()

    and not void as return type.
    Last edited by j2me64; 09-29-2010 at 02:50 PM.

Similar Threads

  1. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  2. Replies: 0
    Last Post: 02-28-2009, 11:16 AM
  3. Tracking Values Using Objects
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-20-2008, 08:18 PM
  4. Replies: 2
    Last Post: 08-03-2007, 04:06 PM
  5. Help with parameters values in java
    By coco in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:14 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
  •