-2
public class Geniegotchi {
   private String name = "Bob";
   private int endurance = 4;
   private int happiness = 3; 
   public void setName(String newName){
      name = newName;
   } 
   public void setEndurance(int newEndurance){
   endurance = newEndurance;
   }
   public void setHappiness (int newHappiness){
   happiness = newHappiness;
   }
   public String getName(){
   return name;
   }
   public int getEndurance (){
   return endurance;
   }
   public int getHappiness (){
   return happiness;
   }
   public void genieInfo(){
   System.out.println( "current name: "+this.getName());
   System.out.println( "current happiness level: "+this.getHappiness());
   System.out.println( "current endurance level: "+this.getEndurance());
   }
   public void feed(){
   if (this.getEndurance() <= 10){
   }
   else
     System.out.println("No, thanks...");
   }
   public void play(){
   if (this.getHappiness() < 10){
   }
   else
   System.out.println("No, thanks");
}

void feed() method to increases current endurance by 1 if endurance is less than 10, otherwise it prints a “No, thanks...” message to the screen;

void play() method is to increases current happiness by 1 and decreases current endurance by 2, if happiness is less than 10, then this otherwise it prints a “No, thanks...” message to the screen;

For the void feed and void play parts, I don't know how to increases current endurance by 1, and increases current happiness by 1 and decreases current endurance by 2. Thank you

ozil
  • 6,930
  • 9
  • 33
  • 56
  • 1
    you could simply just use `endurance++;` as a statement to increase your value. As an alternative you could also call `setEndurance(1+getEndurance());` – SomeJavaGuy Nov 16 '15 at 06:53
  • I offer you to use AtomicInteger. https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html – newuserua_ext Nov 16 '15 at 07:19

1 Answers1

0

There are a few ways to increase or decrease variables, all my examples will use your "endurance" variable, but you can do this for any variable. The first two are shorthand methods to increase/decrease the variable by a certain value and the last method shows that you can also use a variable in an equation and store the result back in the same variable.

endurance++;                // increase by 1
endurance--;                // decrease by 1

endurance += 7;             // increase by any number (7 in this example)
endurance -= 7;             // decrease by any number

endurance = endurance + 9;  // increase by any number (9 in this example)
endurance = endurance - 9;  // decrease by any number
Tekkerue
  • 1,497
  • 1
  • 11
  • 17