Results 1 to 11 of 11
- 04-19-2011, 02:18 AM #1
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
Can't figure out how to get the info from class definition to driver program
I am not getting this - and I know I am close. This is the problem called Puzzlevania where you have to simulate a duel between 3 men who have different probabilities of winning the duel. I wrote the class program (with the help of a tutor). Here it is -
Now what I want to do is "load" the info for the first dueler versus the second dueler. I am not worried yet about repeating this until I see who is the last man standing, etc. I just want the info to load so I can see if the first duel works.Java Code:public class Duelist { private String name; private boolean dead = false; double accuracy = 0; public void setName(String newName) //mutator method (when change something it is a mutator) { name = newName; } public void setAccuracy(double newAccuracy) // mutator method { accuracy = newAccuracy; } public void setDead() // mutator method { dead = true; } public void ShootAtTarget(Duelist target) //method to set to alive or dead { double shotAccuracy = Math.random()*accuracy; if (shotAccuracy > accuracy) { target.setDead(); System.out.println(target + " dead"); } else if (accuracy ==1) { target.setDead(); System.out.println(target + " dead"); } } public Duelist(String newName, double newAccuracy) //constructor { name=newName; accuracy=newAccuracy; } public Duelist()// no argument constructor { name=""; accuracy = 0; } public boolean getDead() //getters { return dead; } public String getName() //getters { return name; } public double getAccuracy() //getters { return accuracy; } }
I know there is something fundamental I am doing wrong. Right not the error is with the line deul.Duelist(aaron); but perhaps I am approaching this totally wrong?Java Code:import java.util.*; public class DuelistTester { public static void main(String[] args) { Duelist aaron = new Duelist("aaron", .33); Duelist bob = new Duelist("bob", .50); Duelist charlie = new Duelist("charlie", 1.0); // while(!(aaron.getDead()&&bob.getDead())||!(aaron.getDead()&&charlie.getDead())||!(bob.getDead()&&charlie.getDead()))//compare the shooters so that program runs while two are still alive { Duelist duel = new Duelist(); duel.Duelist(aaron); duel.ShootAtTarget(charlie); } } }
- 04-19-2011, 02:44 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Duelist is a constructor, it's used to create a new person, so the call to duel.Duelist(aaron) is wrong. You are calling the constructor Duelist on the object duel, with the argument aaron(another dueler), does that make sense to you?
To help you understand this. Start with only 2 duelers and make them fight to the death.
in main, create 2 duelers, you did this. Now create a loop which continues while both are not dead
the method shoot at target will look something like this to get dueler 1 to attack dueler 2Java Code:while(!(dueler1.getDead()) && ! dueler2.getDead()){ have them each attack eachother }
Java Code:dueler1.shootAtTarget(dueler2);
- 04-19-2011, 03:08 AM #3
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
Thanks, I am so brain dead on this problem that I can't even figure out how to print out who won the battle. Sigh....
- 04-19-2011, 03:13 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If you define a toString method in the duelist class you can simply print both objects when the loop ends
Your toString method can return any string it wants, including the current value of the boolean variable dead in your class.Java Code:System.out.println(dueler1); System.out.println(dueler2);
- 04-19-2011, 03:18 AM #5
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
Thanks. I'll give this a rest right now but back on it bright and early. Appreciate your help.
- 04-20-2011, 12:00 AM #6
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
Can't figure out how to test boolean
OK, getting closer. But I want the driver program to test if the dueler is dead. Thanks for any help you can provide.
Here is the class code.
Here is the Driver code - so far. With this code if(aaron.getDead(false)) I was trying to get the program to go back and check if aaron was still alive, but it obviously doesn't work.Java Code:public class Duelist { private String name; private boolean dead = false; double accuracy = 0; public void setName(String newName) //mutator method (when it changes something it is a mutator) { name = newName; } public void setAccuracy(double newAccuracy) // mutator method { accuracy = newAccuracy; } public void setDead() // mutator method { dead = true; } public void ShootAtTarget(Duelist target) //method to set to alive or dead { double shotAccuracy = Math.random(); System.out.println("The random shot accuracy is: "+shotAccuracy); System.out.println(); if (accuracy ==1) { target.setDead(); System.out.println(name + " versus " + target.name); System.out.println(name+" has just killed "+target.name); System.out.println(name + " never misses!"); } else if (shotAccuracy <= accuracy) { System.out.println(" "); System.out.println(name + " versus " + target.name); target.setDead(); System.out.println(name+" has just killed "+target.name); System.out.println("Amazing shot!"); } else { System.out.println(name + " versus " + target.name); System.out.println(name +" missed. " + target.name + " lives to fight again!"); System.out.println(" "); } } public Duelist(String newName, double newAccuracy) //constructor { name=newName; accuracy=newAccuracy; } public Duelist()// no argument constructor { name=""; accuracy = 0; } public boolean getDead() //getters { return dead; } public String getName() //getters { return name; } public double getAccuracy() //getters { return accuracy; } }
Java Code:import java.util.*; public class DuelistTester { public static void main(String[] args) { Duelist duel = new Duelist(); Duelist aaron = new Duelist("Aaron", .33); Duelist bob = new Duelist("Bob", .50); Duelist charlie = new Duelist("Charlie", 1.0); while(!(aaron.getDead()&&bob.getDead())||!(aaron.getDead()&&charlie.getDead())||!(bob.getDead()&&charlie.getDead()))//compare the shooters so that program runs while two are still alive if(aaron.getDead(false)) { aaron.ShootAtTarget(charlie); //bob.ShootAtTarget(charlie); //charlie.ShootAtTarget(aaron); } //else if(bob.getDead=false) { bob.ShootAtTarget(charlie); //bob.ShootAtTarget(charlie); //charlie.ShootAtTarget(aaron); } } }
- 04-20-2011, 12:26 AM #7
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
OK, I figured out that the format is if(aaron.equals(false))
But still can't get it to check if aaron is still alive. Help!!
- 04-20-2011, 12:54 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Did you override the equals method? If not, you shouldn't be overriding it.
The loop already accomplishes this. The entire goal of the loop is to continue while someone is still alive. Testing for if they are dead is like this
Java Code:aaron.getDead() //dead !aaron.getDead()//not dead
- 04-20-2011, 01:23 AM #9
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
Thanks - I just can't seem to figure out how to make this loop until there is only one dueler left, and to tell the user who the winner is. Here is the code so far with your additions -
Java Code:import java.util.*; public class DuelistTester { public static void main(String[] args) { Duelist duel = new Duelist(); Duelist aaron = new Duelist("Aaron", .33); Duelist bob = new Duelist("Bob", .50); Duelist charlie = new Duelist("Charlie", 1.0); // while(!(aaron.getDead()&&bob.getDead())||!(aaron.getDead()&&charlie.getDead())||!(bob.getDead()&&charlie.getDead()))//compare the shooters so that program runs while two are still alive if(!aaron.getDead()) { aaron.ShootAtTarget(charlie); //bob.ShootAtTarget(charlie); //charlie.ShootAtTarget(aaron); } else if(!bob.getDead()) { bob.ShootAtTarget(charlie); //bob.ShootAtTarget(charlie); //charlie.ShootAtTarget(aaron); } else if(!charlie.getDead()) { charlie.ShootAtTarget(bob); } System.out.println("End of looping"); }
-
Java Code:class DuelistTester { private boolean twoAlive = true, oneAlive = false; public static void main(String[] args) { ... while (twoAlive) { if (oneAlive) { endGame(); break; } } } public static void endGame() { ... } }
How would you like to determine when 'oneAlive' should be set to true?
Maybe if you make a calculation of the number of duelers that exist
minus the number of duelers that died; if that calculation equals '1' e.g.
Then each time someone dies you add 1 to a counter, so you can make the calculation, and whenever the game is restarted you'd have to reset the counter to 0.Java Code:public class Duelist { //add class counter private static int numDuelists = 0; Duelist() { //update counter in constructor numDuelists++; } //get method public int getNumDuelists() { return numDuelists; } }
Or maybe you should loop through each player to check if they're still alive,
and incorporate a counter to see the number still alive e.g.
Java Code:public int getNumberAlive() { int countAlive = 0; for (Duelist d : listOfDuelists) if (d.isAlive()) countAlive++; return countAlive; }
Your 'listOfDuelists' would have to be compiled yourself like this:
Java Code:List<Duelist> listOfDuelists = new ArrayList<>(Arrays.asList(new Duelist[] {duel,aaron,bob,charlie}));Last edited by ozzyman; 04-20-2011 at 01:54 AM.
- 04-20-2011, 02:03 AM #11
Member
- Join Date
- Mar 2011
- Location
- San Diego, CA
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
need help to figure out basic java program
By shane123 in forum New To JavaReplies: 21Last Post: 12-02-2011, 04:12 AM -
Error: no class definition found
By toby in forum New To JavaReplies: 6Last Post: 08-28-2011, 10:32 PM -
Dynamic loading of a class (passing class definition over the network)
By eddie-w in forum Advanced JavaReplies: 8Last Post: 04-14-2010, 05:49 AM -
[SOLVED] cant figure this program out..help
By einstein1234 in forum New To JavaReplies: 26Last Post: 04-23-2009, 04:30 AM -
Getting Class info
By Java Tip in forum Java TipReplies: 0Last Post: 12-06-2007, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks