Results 1 to 4 of 4
- 09-29-2010, 05:22 AM #1
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
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; }
- 09-29-2010, 08:33 AM #2
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+""; } }
Good luck!
- 09-29-2010, 03:10 PM #3
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
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(); } }
- 09-29-2010, 03:38 PM #4
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 03:50 PM.
Similar Threads
-
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 04:08 PM -
how to insert data in table (stored proc) without taking all the values as parameters
By Faheem_Ahmed in forum New To JavaReplies: 0Last Post: 02-28-2009, 12:16 PM -
Tracking Values Using Objects
By Npcomplete in forum New To JavaReplies: 2Last Post: 10-20-2008, 09:18 PM -
how to have function variables remember their values between calls
By asterik123 in forum New To JavaReplies: 2Last Post: 08-03-2007, 05:06 PM -
Help with parameters values in java
By coco in forum New To JavaReplies: 1Last Post: 07-31-2007, 09:14 PM
Bookmarks