In Java Create a class called Character that a role-playing game might use to re
ID: 3881973 • Letter: I
Question
In Java
Create a class called Character that a role-playing game might use to represent a character within the game. A character should include six stats as instance variables - strength, dexterity, constitution, intelligence, wisdom and charisma of the character (all types are int). Your class should have a constructor that initializes these six instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getStatsTotal that calculates the total of these six stats, then returns the amount as an int value. If the total is not positive, it should be set to 0. Write a test application named CharacterTest that demonstrates class Character’s capabilities.
Explanation / Answer
The code is
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
class Character
{
private int strength, dexterity, constitution, intelligence, wisdom, charisma;
public Character(int strength, int dexterity, int constitution, int intelligence, int wisdom, int charisma) {
this.strength = strength;
this.dexterity = dexterity;
this.constitution = constitution;
this.intelligence = intelligence;
this.wisdom = wisdom;
this.charisma = charisma;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getDexterity() {
return dexterity;
}
public void setDexterity(int dexterity) {
this.dexterity = dexterity;
}
public int getConstitution() {
return constitution;
}
public void setConstitution(int constitution) {
this.constitution = constitution;
}
public int getIntelligence() {
return intelligence;
}
public void setIntelligence(int intelligence) {
this.intelligence = intelligence;
}
public int getWisdom() {
return wisdom;
}
public void setWisdom(int wisdom) {
this.wisdom = wisdom;
}
public int getCharisma() {
return charisma;
}
public void setCharisma(int charisma) {
this.charisma = charisma;
}
int getStatsTotal()
{
int sum;
sum=this.charisma+this.constitution+this.dexterity+this.intelligence+this.strength+this.wisdom;
if(sum<0)
return 0;
return sum;
}
}
public class Main
{
public static void main( String args[] )
{
Character c=new Character(10,20,30,40,50,60);
System.out.println(c.getStatsTotal());
}
}
The output is
210
Do give a thumbs up as it matters a lot and in case there are doubts leave a comment.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.