Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In java For this question, you will modify the above classes and add one new class, in order to add magical spells for the player

In java

For this question, you will modify the above classes and add one new class, in order to add magical spells for the player to use

(a) Spell Write a class Spell. A Spell has the following private attributes:

A String name

A double minimum damage

A double maximum damage

A double chance of success for the spell (from 0 to 1)

The Spell class also contains the following public methods:

1.A constructor that takes as input the name, minimum and maximum damage, and chance of

success for the spell

An IllegalArgumentException must be thrown if the minimum damage is less than 0 or greater than the maximum damage, or the chance of success is less than zero or greater than one.

2.getName which returns the name of the spell

3.A getDamage method that returns the damage for a spell casting. First, a random number from 0 to 1 is generated. If the random number is above the chance of success, the spell fails, and 0 damage is returned. Otherwise, a random number from the minimum damage to the maximum damage is returned. Use the Random class to generate the random numbers, and do not use a seed.

4.A toString method. The String which is returned must contain the name, minimum and maximum damage, and the success chance as a percentage from 0 to 100 (so a chance of 0.5 is reported as 50.0%)

(b) Character Modify the Character class.

1) Add a private static attribute spells. This variable will be an ArrayList which contains Spells that the character can cast. Note that this list of spells will be available to all Characters.

2) Add a setter method setSpells for the spells attribute. Recall that a setter method allows other classes to set values for private attributes. This method will take one parameter, and will copy the Spells contained in the input parameter into a new ArrayList stored in the spells attribute. This method does not return a value.

3) Add a new method castSpell, which takes the name of a spell to cast as a parameter spellName. This method will return the damage done by the spell as a double.

This method will iterate through the list of spells, searching for the spell with the name matching spellName. Note that you should ignore the capitalization of the spell name when matching.

If the spell cannot be found, print a message which says that the character tried to cast an unknown spell. Also return zero damage.

If the spell is found, the damage done by the spell is given by calling the getDamage method on the spell. If the damage returned is zero, then the spell failed to cast. Print a message that the character failed to cast the spell. If the damage was above zero, print a message that the spell was cast. Return the damage from the castSpell method.

In all messages printed in this castSpell method, the message should have the characters name, as well as the name of the spell. See the example output at the end of this document.

(c) FileIO Add a new static method, the readSpells method. This method takes a filename of spells to read,

and returns an ArrayList of Spells.

An example of a file containing spells is found in the provided files as spells.txt.

Read the file indicated by the method parameter filename, by using a BufferedReader. Catch the IOException and FileNotFoundException exceptions in two separate catch blocks, and throw an IllegalArgumentException with an appropriate error message if these occur. Note that the catch block for the FileNotFoundException must be above the catch block for the IOException.

As before, if you prefer you can also use the throws statement to throw the FileNotFoundException and IOException up to the calling playGame method. Then these Exceptions would be caught in a try/catch block there.

Each spell is on one line, consisting of the spell name, the min damage, the max damage, and the chance for success, separated by spaces. Use the split() method to split each line of the file by the space delimiter.

Use the four values on each line of the file to create new Spell instances, and then add these Spell instances to the ArrayList which will be returned from this method. Do not use the Scanner class to parse the file.

(d) playGame Change the playGame method in the BattleGame class to allow the player to cast spells.

1) At the beginning of the playGame method, call the readSpells method in the FileIO class with the filename of the file containing spells. This method returns an ArrayList of spells.

Print out each spell (using the toString method of the Spell class), so that the player knows which spells they may cast.

Then call the setSpells method in the Character class with this list of spells.

2) In addition to the attack and quit commands, we will allow the user to cast spells. If the input is not attack or quit, then the input must be passed to the castSpell method on the players character, as we assume it is the name of a spell to cast.

The damage returned from the castSpell method should then be applied to the monster, using the takeDamage method.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago