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();
}
}
"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.staminais a field ofPlayer, you should provide agetStaminamethod in yourPlayerclass and useplayerOne.getStamina()when you want to display it.Playerinstance at all, they've just used a undefined variable in themainmethod calledstamina... and don't get me started onpublicfields