Results 1 to 11 of 11
  1. #1
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default 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 -

    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;
    }
    
    }
    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:
    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);
     		}
    
    	}
    
    }
    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?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    while(!(dueler1.getDead()) && ! dueler2.getDead()){
      have them each attack eachother
    }
    the method shoot at target will look something like this to get dueler 1 to attack dueler 2
    Java Code:
    dueler1.shootAtTarget(dueler2);

  3. #3
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    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....

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you define a toString method in the duelist class you can simply print both objects when the loop ends
    Java Code:
    System.out.println(dueler1);
    System.out.println(dueler2);
    Your toString method can return any string it wants, including the current value of the boolean variable dead in your class.

  5. #5
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    Thanks. I'll give this a rest right now but back on it bright and early. Appreciate your help.

  6. #6
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default 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.
    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;
    }
    
    
    
    }
    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:
    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);
    		}
    
    	}
    
    }

  7. #7
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    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!!

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  9. #9
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    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");
    	}

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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.
    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; }
    }
    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.

    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 02:54 AM.

  11. #11
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

Similar Threads

  1. need help to figure out basic java program
    By shane123 in forum New To Java
    Replies: 21
    Last Post: 12-02-2011, 05:12 AM
  2. Error: no class definition found
    By toby in forum New To Java
    Replies: 6
    Last Post: 08-28-2011, 11:32 PM
  3. Replies: 8
    Last Post: 04-14-2010, 06:49 AM
  4. [SOLVED] cant figure this program out..help
    By einstein1234 in forum New To Java
    Replies: 26
    Last Post: 04-23-2009, 05:30 AM
  5. Getting Class info
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 03:46 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
  •