1

I have to write a code for class. These are the requirements:

One class should be the Controller with the main method.

The other class should:

  • do some action
  • pass in variables
  • use at least one if statement
  • have private methods
  • have some getters and setters.

I have done most of this. I ran into a problem where I want the user to type 'Go Forward' and then for the stamina to be decreased by five and have a message appear in the sonsole saying, "You lose 5 stamina." And also I'd like for the user to be able to type 'Rest' and for the stamina to increase by 5 (capping at 100, and for the console to say, "Stamina increased to #."

I don't know how to increase/decrease the stamina though. Whenever I try something, I get an error.

If there'es something I have that I don't need, then tell me what it is and why I should remove it. If there is something that's public and doesn't need to be, tell me why it shouldn't be. My teacher says that later on, there will be a 5 point deduction for every time something is public that doesn't need to be.

Here's the code:

Controller Class:

import java.io.*;
import java.util.Scanner;

public class Controller {
  public static void main(String[] args){

    System.out.println("Type your name here:");
    Scanner scan = new Scanner(System.in);
    String input = scan.nextLine();         
    Player playerOne = new Player(input);
          playerOne.setStrength(78);
          playerOne.setHealth(99);
          playerOne.setStamina(67);
          playerOne.printplayer();

          System.out.println("Type 'Go Forward' to move forward");
          Scanner mscan = new Scanner(System.in);
          //mscan = movement scan   
          String minput = scan.nextLine();
                if(minput.equals("Go Forward"))
                    //minput = movement input
                    System.out.println("You lose 5 stamina.");
                    //Getting an error here : 'stamina cannot be resolved to a variable'
                    System.out.println("Stamina: " + stamina);
                if(minput.equals("Rest"))   
                //Getting an error here : 'stamina cannot be resolved to a variable'
                    System.out.prinln("Stamina has been increased to " + stamina);
                     scan.close(); 
                     }
}

And the Player Class:

import java.util.Scanner;

public class Player{

        public String name; 
        private String input;
        private double health;


        public double strength;

        public double stamina;

        //Player Name
         public Player (String input){
          name = input;
       }

       //Player Health
         public void setHealth(double playHp){
             health = playHp;
       }
       //Player Strength
       public void setStrength(double playStrn){
          strength = playStrn;
       }
       //Player Stamina
       public void setStamina(double playStam){
          stamina = playStam;
       }

       public void printplayer(){
          System.out.println("name  : " + name );
          System.out.println("Health :" + health);
          System.out.println("Strength :" + strength);
          System.out.println("Stamina :" + stamina);

            }

       private void reduceStamina() {
           int stamina;

         }
    public int getStamina() {

        return 0;
    }
}

          System.out.println("Type 'Go Forward' to move forward");
          Scanner mscan = new Scanner(System.in);
          //mscan = movement scan   
          String minput = scan.nextLine();
                if(minput.equals("Go Forward"))
                    //minput = movement input
                    System.out.println("You lose 5 stamina.");
                    //Getting an error here : 'stamina cannot be resolved to a variable'
                    System.out.println("Stamina: " + stamina);
                if(minput.equals("Rest"))   
                //Getting an error here : 'stamina cannot be resolved to a variable'
                    System.out.prinln("Stamina has been increased to " + stamina);
                     scan.close(); 
                     }
}
8
  • "I don't know how to increase/decrease the stamina though. Whenever I try something, I get an error." -- Please show what you've tried and the full error messages that this causes. Else we won't know what you might be doing wrong, or what incorrect assumptions you might be making. Commented Aug 25, 2015 at 1:13
  • stamina is a field of Player, you should provide a getStamina method in your Player class and use playerOne.getStamina() when you want to display it. Commented Aug 25, 2015 at 1:15
  • @MadProgrammer you're right, but he set stamina to public so that doesn't look like the overall problem. It's wrong, but it won't generate an error like he is getting. Commented Aug 25, 2015 at 1:17
  • @lacraig2 "//Getting an error here : 'stamina cannot be resolved to a variable'", the OP is not referencing the Player instance at all, they've just used a undefined variable in the main method called stamina ... and don't get me started on public fields Commented Aug 25, 2015 at 1:18
  • @MadProgrammer I don't like it either, but his problem is that he's not calling playerOne.stamina (or getStamina() if he set up the method correctly, not that he isn't properly protecting his data. Commented Aug 25, 2015 at 1:20

2 Answers 2

1

To use a variable in Java, you have to declare it first. Example you would like to use an integer, you have to declare it

int mynumber = 1; //declare variable named mynumber and give it value is 1

You missed on how to declare a variable, that make 2 mistake I see in your code 1. Reusable: you already declared a Scanner before, so you can use it again, dont have to declare one more Scanner named mscan 2. Declare a variable: as I said before, if you want to use a variable, you have to declare it first. In your whole code, you did not declare stamina variable so you can not use it. That's the reason why you got error

stamina cannot be resolved to a variable.

If you want to decrease stamina of that player, you should get current stamina, decrease it and set it back For example:

double currentStamina = player.getStamina();
currentStamina = currentStamina - 5; // (or currentStamina-=5) decrease current stamina by 5
player.setStamina(currentStamina); // set current stamina for player

Next I will continue about your code. The property of an object should not be declared as public (your fields 'name', 'strength', 'stamina'). They should be private and you can get/set value of them through the get/set methods. You use stamina as variable but it is not referenced (I said you did not declare it before). Did you mean the stamina field of class Player? So if you want to use a field of another object, you have to use like this: instead of

System.out.println("Current stamina is " + stamina);

change it to

System.out.println("Current stamina is " + player.stamina);

But dont call like that, you should call:

System.out.println("Current stamina is " + player.getStamina()); // don't directly access field of an object, please call get/set instead

Next: IF and ELSE IF You for get open and close brankets in your code for the if statements. If you dont have open and close brankets after your statement, it will only execute only 1 next line as the action for the previous condition. And you should learn to use else keyword.

After the whole things, your code should be:

String minput = scan.nextLine();
double currentStamina = player.getStamina();
if(minput.equals("Go Forward"))
{
     currentStamina -= 5;
     System.out.println("You lose 5 stamina.");
     System.out.println("Stamina: " + currentStamina);
}
else if(minput.equals("Rest"))   
{
     currentStamina += 5;
     System.out.prinln("Stamina has been increased to " + currentStamina );
}
player.setStamina(currentStamina); // update stamina value to player
scan.close(); 
Sign up to request clarification or add additional context in comments.

Comments

0

Declare a public methods called getStamina,increaseStamina and decreaseStamina:

public int getStamina(){
   return stamina;
}

public void increaseStamina(int st){
   stamina+=st;
}

public void decreaseStamina(int st){
   stamina-=st;
}

Then in your program do something like:

Player player=new Player();
if(minput.equals("Go Forward"))
    System.out.println("You lose 5 stamina.")
    player.decreaseStamina(5);
    System.out.println("Stamina: " + player.getStamina());

2 Comments

He is a new to coding, your answer let him be a copy&paste robot.
My teacher teached as robot, I have some robot friend. So sad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.