You are viewing our Forum Archives. To view or take place in current topics click here.
[Java] Can't get method to work
Posted:

[Java] Can't get method to workPosted:

-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
I'm trying to get a little cat and mouse game working but for some reason my character won't move upwards or left.
I am using the numpad as a basic control system (2,4,6,8) where 8 moves him up by one space, 6 moves right, etc.

I don't see what I am doing wrong to not have him move up or left when both methods are accessed with the mutator methods I've already put in place. The methods are accessed (i've tested this) but the position is not updated.
Hopefully someone spots my mistake.

HeroApp.java

import java.util.Scanner;

public class HeroApp {

   public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      Hero dave = new Hero("Dave", 10);
      Monster monster = new Monster();
      String response = "";
      int move = 0;
      
      dave.printDetails();
      monster.printDetails();
      
      while (!monster.eaten(dave)) {
         response = in.nextLine();
         move = Integer.parseInt(response);
         switch(move){
            case 8:{
               dave.moveUp();
            }
            case 2: {
               dave.moveDown();
            }
            case 4: {
               dave.moveLeft();
            }
            case 6: {
               dave.moveRight();
            }
         }
         
         monster.chase(dave);
         
         dave.printDetails();
         monster.printDetails();
      }
   }
}


Hero.java


public class Hero {

   private String name;
   private int score, lives, x, y;
   
   public Hero(String nameIn, int livesIn) {
      this.name = nameIn;
      this.lives = livesIn;
      this.score = 0;
      this.x = 10;
      this.y = 10;
   }
   
   public void loseLife() { // Decrement the lives.
      if (this.lives > 0) {
         this.lives--;
      } else {
         System.out.println("Lives cannot go below 0.");
      }
   }
   
   public void increaseScore() { // Increase score by 10.
      this.score += 10;
   }
   
   public void printDetails() { // Print name, score and lives remaining for the hero.
      System.out.println("Name: " + this.name +
            "\nScore: " + this.score +
            "\nLives: " + this.lives +
            "\nLocated at: " + this.x + ", " + this.y + "\n");
   }
   
   public boolean isAlive() {
      if (this.lives > 0) {
         return true;
      } else {
         return false;
      }
   }
   
// Accessor Methods
   public int getScore() {
      return this.score;
   }
   
   public int getLives() {
      return this.lives;
   }
   
   public int getX() {
      return this.x;
   }
   
   public int getY() {
      return this.y;
   }
   
// Mutator Methods
   public void moveUp() {
      this.y++;
   }
   
   public void moveDown() {
      this.y--;
   }

   public void moveLeft() {
      this.x--;
   }
   
   public void moveRight() {
      this.x++;
   }
}


Monster.java


public class Monster {
   private int x, y;
   
   public Monster() {
      this.x = 0;
      this.y = 0;
   }
   
   public void chase(Hero hero) {
      if (hero.getX() < this.x) {
         this.x--;
      } else if (hero.getX() > this.x) {
         this.x++;
      } else if (hero.getY() < this.y) {
         this.y--;
      } else if (hero.getY() > this.y) {
         this.y++;
      } else {
         this.eaten(hero);
      }
   }
   
   public boolean eaten(Hero hero) {
      if (this.x == hero.getX() && this.y == hero.getY()){
         System.out.println("OMNOMNOM");
         return true;
      } else {
         return false;
      }
   }
   
   public void printDetails() {
      System.out.println("Name: Monster" +
            "\nPosition: " + this.getX() + ", " + this.getY() + "\n");
   }
   
// Accessor Methods
   public int getX() {
      return this.x;
   }
   
   public int getY() {
      return this.y;
   }
}


Last edited by -Deano ; edited 1 time in total
#2. Posted:
Skittle
  • Video King
Status: Offline
Joined: Aug 20, 201410Year Member
Posts: 6,813
Reputation Power: 413
Status: Offline
Joined: Aug 20, 201410Year Member
Posts: 6,813
Reputation Power: 413
Have you made sure that the mutator methods for up and left at actually being called? You could use a Breakpoint or a message dialog to test this.
#3. Posted:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
doot wrote Have you made sure that the mutator methods for up and left at actually being called? You could use a Breakpoint or a message dialog to test this.


Yes, it definitely goes through the method. I added an output which shows in the console yet the position is not updated.
#4. Posted:
Skittle
  • TTG Commander
Status: Offline
Joined: Aug 20, 201410Year Member
Posts: 6,813
Reputation Power: 413
Status: Offline
Joined: Aug 20, 201410Year Member
Posts: 6,813
Reputation Power: 413
I am at a loss, everything looks as I would expect :/ I'm not an expert in Java so it may be a simple Syntax error, but I'm assuming your IDE gives you no errors..
#5. Posted:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
doot wrote I am at a loss, everything looks as I would expect :/ I'm not an expert in Java so it may be a simple Syntax error, but I'm assuming your IDE gives you no errors..


No errors.
I can't make any sense of this problem.

can't be that it won't decrement the values because I can move right.
can't be that it won't increment the values because I can move down.
the method is called because I put in a print to trigger when called.

:javapls:
#6. Posted:
xMrGnome
  • Ladder Climber
Status: Offline
Joined: Jul 10, 201014Year Member
Posts: 391
Reputation Power: 15
Status: Offline
Joined: Jul 10, 201014Year Member
Posts: 391
Reputation Power: 15
Have you tried making the program in Scratch?
#7. Posted:
ObscureCoder
  • Resident Elite
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Status: Offline
Joined: Jun 29, 201311Year Member
Posts: 211
Reputation Power: 13
Your error is rather trivial.
You've messed up the switch statement.
That's the incorrect syntax for a switch statement, you don't use curly braces.

Fixed code:


      while (!monster.eaten(dave)) {
         int move = in.nextInt();
         
         switch (move) {
         case 8:
            dave.moveUp();
            break;
         case 2:
            dave.moveDown();
            break;
         case 4:
            dave.moveLeft();
            break;
         case 6:
            dave.moveRight();
            break;
         default:
            //
         }

         monster.chase(dave);

         dave.printDetails();
         monster.printDetails();
      }



As a side note:
For a boolean method such as isAlive, don't do an if statement for an evaluative expression; just return the expression.

   public boolean isAlive() {
      return (this.lives > 0);
   }

Also, use the methods you create:

   public void loseLife() { // Decrement the lives.
      if(this.isAlive()) {
         this.lives--;
      }
   }


Also, for the printDetails():
1) Don't create a method called printDetails() - that's the kinda unprofessional stuff you get taught in introductory Java lessons. Override every class object's toString() method and print the object.
2) Use System.out.println() more than once if you're wanting newlines. Java is multi platform but that doesn't mean that the newline \n works on all platforms. System.out.println() will end every console line with System.getProperty("line.separator");

There's also no real need to make things messy by referring to "this" all the time.
You can also assign the values of class members outside methods, so.. no need to set them inside the constructor.
#8. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
ObscureCoder wrote Your error is rather trivial.
You've messed up the switch statement.
That's the incorrect syntax for a switch statement, you don't use curly braces.

Fixed code:


      while (!monster.eaten(dave)) {
         int move = in.nextInt();
         
         switch (move) {
         case 8:
            dave.moveUp();
            break;
         case 2:
            dave.moveDown();
            break;
         case 4:
            dave.moveLeft();
            break;
         case 6:
            dave.moveRight();
            break;
         default:
            //
         }

         monster.chase(dave);

         dave.printDetails();
         monster.printDetails();
      }



As a side note:
For a boolean method such as isAlive, don't do an if statement for an evaluative expression; just return the expression.

   public boolean isAlive() {
      return (this.lives > 0);
   }

Also, use the methods you create:

   public void loseLife() { // Decrement the lives.
      if(this.isAlive()) {
         this.lives--;
      }
   }


Also, for the printDetails():
1) Don't create a method called printDetails() - that's the kinda unprofessional stuff you get taught in introductory Java lessons. Override every class object's toString() method and print the object.
2) Use System.out.println() more than once if you're wanting newlines. Java is multi platform but that doesn't mean that the newline \n works on all platforms. System.out.println() will end every console line with System.getProperty("line.separator");

There's also no real need to make things messy by referring to "this" all the time.
You can also assign the values of class members outside methods, so.. no need to set them inside the constructor.


Thanks. It's obvious I'm on an introductory course.
I had thought that it was necessary to include the "this." for everything as part of the class creation.
Thanks for the tips
#9. Posted:
xMrGnome
  • Ladder Climber
Status: Offline
Joined: Jul 10, 201014Year Member
Posts: 391
Reputation Power: 15
Status: Offline
Joined: Jul 10, 201014Year Member
Posts: 391
Reputation Power: 15
Deano3607 wrote
Thanks. It's obvious I'm on an introductory course.
I had thought that it was necessary to include the "this." for everything as part of the class creation.
Thanks for the tips


Don't mention it mate, always happy to help! I'm glad you decided to use my scratch idea!
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.