Question

To be written in JAVA

We want you to write a Java class named Character WithStatus that can be used to keep track of random game effects status on

- AtoString method to display all of the characters information and status. You are free to format this information as you s

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

Complete java program:

import java.util.Random;

public class CharacterWithStatus {
//fields
private String name;
private int health;
private int mana;
private String status;
private int dice;
  
//constructor
public CharacterWithStatus(String name,int health,int mana){
this.name = name;
this.health = health;
this.mana = mana;
}
  
//accessor and mutators for all data fields except dice and status
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getHealth() {
return health;
}

public void setHealth(int health) {
this.health = health;
}

public int getMana() {
return mana;
}

public void setMana(int mana) {
this.mana = mana;
}
  
public void rollDice(){
//instance of Random class to generate random numbers
Random random = new Random();
  
//generates random number from 1-6 and updates the dice field
this.dice = random.nextInt((6 - 1) + 1) + 1;
}
  
public void updateStatus(){
if(this.dice == 1){
this.status = "dead";
this.health = 0;
this.mana = 0;
}
else if(this.dice == 2){
this.status = "stunned";
}
else if(this.dice == 3){
this.status = "poisoned";
this.health = 0;
this.mana = 0;
}
else if(this.dice == 4){
this.status = "sleeping";
}
else if(this.dice == 5){
this.status = "half dead";
this.health = this.health / 2; //half of the current value
this.mana = this.mana / 2;
}
else if(this.dice == 6){
this.status = "revive";
this.health = 100;
this.mana = 100;
}
}

public String describeStatus(){
String statusResult = null;
if("".contentEquals(this.status)){
statusResult= "Test your fate and roll the dice";
}else if("dead".equalsIgnoreCase(this.status)){
statusResult= "It appears fate wanted you dead";
}else if("poisoned".contentEquals(this.status)){
statusResult= "fate has it that you suffer slowly";
}else if("sleeping".equalsIgnoreCase(this.status)){
statusResult= "fate says take a nice rest";
}if("half dead".contentEquals(this.status)){
statusResult= "fate has spare your life";
}else if("revive".equalsIgnoreCase(this.status)){
statusResult= "It appears fate wants you to live another day";
}
return statusResult;
}
@Override
public String toString() {
return "name:" + name + ", health:" + health + ", mana:" + mana + ", status:" + describeStatus() + ", dice:" + dice
+ "]";
}


//main method-starting point of running the program
public static void main(String[] args) {
CharacterWithStatus obj= new CharacterWithStatus("John",1000,1000);
obj.rollDice();
obj.updateStatus();
System.out.println(obj.toString());
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
To be written in JAVA We want you to write a Java class named Character WithStatus...
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
  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • Write a Java class that examines a single character input from the keyboard. Follow this dialog...

    Write a Java class that examines a single character input from the keyboard. Follow this dialog to guide your coding: Type a single character and press Enter: m Digit: No Letter: Yes Lowercase: Yes Toggle case: M Note: There is no "next" method of the Scanner class to obtain input from the keyboard of data type char. It simply doesn't exist so you need a workaround. Store the end-user input into a String variable; then extract the first character using...

  • please write in Java and include the two classes Design a class named Fan (Fan.java) to...

    please write in Java and include the two classes Design a class named Fan (Fan.java) to represent a fan. The class contains: An int field named speed that specifies the speed of the fan. A boolean field named fanStatus that specifies whether the fan is on (default false). A double field named radius that specifies the radius of the fan (default 5). A string field named color that specifies the color of the fan (default blue). A no-arg (default) constructor...

  • WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length,...

    WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length, and depth. It will return the total surface area (6 sides) of the rectangular box it represents. 2. Implement a method named rightTriangle that accepts 2 double arameters named sideA and hypotenuseB. The method will return the length of the third side. NOTE To test, you should put all of these methods into a ‘MyMethods’ class and then write an application that will instantiate...

  • PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class...

    PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class has 4 attributes: (1) student ID: protected, an integer of 10 digits (2) student name: protected (3) student group code: protected, an integer (1 for undergraduates, 2 for graduate students) (4) student major: protected (e.g.: Business, Computer Sciences, Engineering) Class Student declaration provides a default constructor, get-methods and set-methods for the attributes, a public abstract method (displayStudentData()). PART II: Create a Java class named...

  • Write a class named Month. The class should have an int field named monthNumber that holds...

    Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example. January would be 1. February would be 2. and so forth. In addition, provide the following methods A no-arg constructor that sets the monthNumber field to 1 A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than...

  • Problem 2 (36 pts): You are going to design a class named Computer. The class contains:...

    Problem 2 (36 pts): You are going to design a class named Computer. The class contains: A string field named manufacturer. A string field named serialNumber. A double field named speed (measured in Gigahertz). A constructor that has a parameter for each data field and sets the data fields with these values. A no-arg constructor that sets string fieldsto and numeric fields to 0. Mutator and Accessor methods for each data field. A method toString) that returns each field value...

  • Design a Java class named StringQueue for storing strings of first names into a queue. The...

    Design a Java class named StringQueue for storing strings of first names into a queue. The StringQueue.java class contains: * A String[] data field named names that stores the String values in the queue. * A private data field named size that stores the number of names in the queue. * A public static final data field named DEFAULT_CAPACITY = 10 for the array. * A private static data field named firstInitCtr that counts the number of names with the...

  • THE FOLLOWING PROGRAM IS NEEDED IN JAVA LANGUAGE Design a class torepresent account, include the following...

    THE FOLLOWING PROGRAM IS NEEDED IN JAVA LANGUAGE Design a class torepresent account, include the following members: -Data Members a)Name of depositor-Stringb)Account Number –intc)Type of account –Boolean d)Balance amount -doublee)AnnualInterestrate -double Methods: -(a)To assign initial values (use constructor)(b)To deposit an amount.(c)TO withdraw amount with the restriction the minimum balance is 50 rs. Ifyouwithdraw amount reduced the balance below 50 then print the error message.(d)Display the name and balance of the account.(e)Get_Monthly_intrestRate() -Return the monthly interestrate whichis nothing but Annualintrestrate/12. Annualinterestrate...

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