Question
Exception Handling - Java Implement the following program with exception handling. -----------------Character.java------------------------------------------------------ public class Character { private int strength; //Instance variable for strength private int
Exception Handling - Java
Implement the following program with exception handling.
-----------------Character.java------------------------------------------------------
public class Character { private int strength; //Instance variable for strength private int dexterity; //Instance variable for dexterity private int constitution; //Instance variable for constitution private int intelligence; //Instance variable for intellgence private int wisdom; //Instance variable for wisdom private int charisma; //Instance variable for charisma public Character(int strength, int dexterity, int constitution, int intelligence, int wisdom, int charisma) { //Constructor this.strength = strength; //Intializing this.dexterity = dexterity; //Initializng this.constitution = constitution; //Initializing this.intelligence = intelligence; // Initialzing this.wisdom = wisdom; //Initializing this.charisma = charisma; //Initializing } public void setStrength(int strength) { this.strength = strength; } public int getStrength() { return strength; } public void setDexterity(int dexterity) { this.dexterity = dexterity; } public int getDexterity() { return dexterity; } public void setConstitution(int constitution) { this.constitution = constitution; } public int getConstitution() { return constitution; } public void setIntelligence(int intelligence) { this.intelligence = intelligence; } public int getIntelligence() { return intelligence; } public void setCharisma(int charisma) { this.charisma = charisma; } public int getCharisma() { return charisma; } public void setWisdom(int wisdom) { this.wisdom = wisdom; } public int getWisdom() { return wisdom; } int getStatsTotal() //Function that adds all the 6 variables together to produce the total { int sum; //Sum is the total of the stats combined sum = this.strength + this.dexterity + this.constitution + this.intelligence + this.wisdom + this.charisma; if(sum < 0) //If statement in case the stat total is negative it returns zero return 0; else return sum; }
}
-----------------------------CharacterTest.java-----------------------------------------------
public class CharacterTest {
/** * @param args the command line arguments */ public static void main(String[] args) { //main function Character a = new Character(25, 32, 44, 21, 18, 9); //Numbers to be plugged into variables System.out.println("Welcome to the game! Below is your character's stats and total"); System.out.println("Strength: " + a.getStrength()); //Printing of variables System.out.println("Dexterity: " + a.getDexterity()); System.out.println("Constitution: " + a.getConstitution()); System.out.println("Intelligence: " + a.getIntelligence()); System.out.println("Wisdom: " + a.getWisdom()); System.out.println("Charisma: " + a.getCharisma()); System.out.println(); System.out.println("Total:" + a.getStatsTotal()); //Printing of total } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started