Question

   public static boolean life = true; // all the creatures start alive    public boolean...

   public static boolean life = true; // all the creatures start alive
   public boolean isAlive() {
       if (this.getDamagePoints()> 0) {
           life = true;
       }else {
           life = false;
       }
       return life;
   }

public boolean springTrap(Inhabitant ihab) {
       // create variable to see if the creature is alive
       boolean OK = ihab.isAlive();
      
      
       // find location of the trap
       Cave trapLocation = this.getLocation();
       // find location of the ihab
       Cave creatureLocation = ihab.getLocation();
       // if they are the same location and the creature isnt immune return trigger message and return true
       if ((trapLocation == creatureLocation) && (ihab.hasImmunity(this)== false) && (OK == true)) {
           System.out.println(triggerMessage(ihab.getName()));
           System.out.println(this.SuccessMessage());
           for (int i = 0; i < ihab.getDamagePoints(); i++ ) {
               ihab.decreaseDamagePoints();
           }
          
           ihab.putToDeath();
           return true;
          
       }
       // if the creature has immunity then return false as trap failed
       if ((ihab.hasImmunity(this))== true ||(OK == false)){
           System.out.println(ihab.getName()+ this.FailMessage());
          
       }return false;
      
      
   }

i cant get this to assert false?? any ideas thanks its java

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Firstly, I assume that you would have multiple objects of class Inhabitant. Then if any one of the object dies, that is life becomes false, then it would mean all object will die.

Second assumption is that the function decreaseDamagePoints() decreases one damage point at a time.

The mistake in your code is in the statement for (int i=0; i<ihab.getDamagePoints();i++) . The terminal condition of the for loop is not static and will change with each iteration. The result would be that damage points never gets to be 0. And life will always remain true.

For easier understanding dry run the following example:

c=4;

For(int i =0; i<c;i++){

c--;

}

Value of c after the execution will be 2 and not 0.

What you can do is store the value of damage points in another variable say, tempDamagePoints and use that I'm the terminal condition.

Also, if you want different value of the variable life for different objects then Don't declare it as static.

Add a comment
Know the answer?
Add Answer to:
   public static boolean life = true; // all the creatures start alive    public boolean...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly;...

    import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly; private int numCastMembers; private String[] castMembers;    // default constructor public Movie() { movieName = "Flick"; numMinutes = 0; isKidFriendly = false; numCastMembers = 0; castMembers = new String[10]; }    // overloaded parameterized constructor public Movie(String movieName, int numMinutes, boolean isKidFriendly, String[] castMembers) { this.movieName = movieName; this.numMinutes = numMinutes; this.isKidFriendly = isKidFriendly; numCastMembers = castMembers.length; this.castMembers = new String[numCastMembers];    for(int i=0;i<castMembers.length;i++)...

  • public static boolean primeCheck (int num) int temp; boolean isPrime-true: for(int i-2;i<=num/ 2 ; i++) temp-numsi...

    public static boolean primeCheck (int num) int temp; boolean isPrime-true: for(int i-2;i<=num/ 2 ; i++) temp-numsi if (temp=-0) isPrime-false; break; return isPrime Given the above code segment, 1. (30 points) Draw a Control Flow Graph (CFG) for the code. 2. (10 points) Enumerate the Test Requirements for node coverage. Design a set of 3. 4. 5. test cases (input value num) that will satisfy node coverage. (10 points) Enumerate the Test Requirements for edge coverage. Design a set of test...

  • Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void...

    Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void main(String[] args) { int currentVertex, userChoice; Scanner input = new Scanner(System.in); // create graph using your WeightedGraph based on author's Graph WeightedGraph myGraph = new WeightedGraph(4); // add labels myGraph.setLabel(0,"Spot zero"); myGraph.setLabel(1,"Spot one"); myGraph.setLabel(2,"Spot two"); myGraph.setLabel(3,"Spot three"); // Add each edge (this directed Graph has 5 edges, // so we add 5 edges) myGraph.addEdge(0,2,9); myGraph.addEdge(1,0,7); myGraph.addEdge(2,3,12); myGraph.addEdge(3,0,15); myGraph.addEdge(3,1,6); // let's pretend we are on...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT