Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your task in this question is to write a class called SkillTree which extends BasicMAryTree 2 8 0 ( an m - ary tree of

Your task in this question is to write a class called SkillTree which extends BasicMAryTree280
(an m-ary tree of Skill objects; a complete Skill.java is provided). A template for the SkillTree
class is provided. It contains a constructor and a couple of useful methods. You will add additional
methods to this class in the following steps, which you should complete in order:
(a) Write a main() method in the SkillTree class in which you construct your own skill tree for your
own hypothetical video game. Your tree must contain at least 10 skills. However, for the sanity
of everyone involved, try to keep it under 15 skills. Be creative! There is no reason why any
two students should hand in exactly the same (or even very similar) skill trees, nor should you
just duplicate the skill tree shown in the sample output. Print your tree to the console using the
toStringByLevel() method inherited from BasicMAryTree280.
(b) Write a method in the SkillTree class called skillDependencies which takes a skill name as
input and returns an instance of LinkedList280 which contains all the of the skills which
are prerequisites for obtaining the input skill (including the input skill itself!). A RuntimeException
exception should be thrown if the tree does not contain the given skill. A good implementation
approach for this method is to use a recursive traversal of the tree to find the named skill, and
then add skills to the output list as the recursion unwinds. Tutorial 3 includes some discussion of
recursive traversal of m-ary trees. Add to your main() program a few tests of this method, and
print out the lists that is returned (you can use the lists toString() method for this). Be sure to
test the case where the named skill does not exist in the tree.
(c) Write a method in the SkillTree class called skillTotalCost which takes a skill name as in-
put and returns the total number of skill points that a player must invest to obtain the given
skill. If the named skill is not in the skill tree, then the skillTotalCost method should throw
a RuntimeException exception. Hint: this method is quite easy to implement if you make use of the
previously implemented skillDependencies method.
For example, in the above skill tree, if a character wants the Shield Ally skill they would need to
spend 1 skill point to get Shield Proficiency, and then spend 3 skill points to get Shield Ally for an
1In the video game world, the term skill tree sometimes refers to things that actually arent trees; a noteworthy example is the
skill tree in the ARPG Path Of Exile, which, if you click the link, can see is clearly not a tree, even though they call it that. Here in
question 2, we used the term skill trees to mean skill trees that are, in fact, actual trees.
Page 4
overall investment of 1+3=4 points, so for the above tree, skillTotalCost("Shield Ally")
should return 4. Note that the Skill object contains the cost of the skill.
Add to your main() program a few tests of skillTotalCost, and print out the total costs returned.
Be sure to test the case where the named skill does not exist in the tree.
(d) Run your main() program. Cut and paste the console output to a text file and submit it with your
assignment. See the sample output below.
Sample Output
Here is an example of what the output of your program might look like. Remember, you are ex-
pected to be creative in designing your skill tree, and your submission should not attempt to duplicate
what you see here aside from the general formatting (the formatting can be the same, but the data
should be different!). Note that the formatting of output of the skill tree contents is done by the
toStringByLevel() method of BasicMAryTree280
import lib280.list.LinkedList280;
import lib280.tree.BasicMAryTree280;
import lib280.tree.LinkedSimpleTree280;
import lib280.tree.MAryNode280;
public class SkillTree extends BasicMAryTree280{
/**
* Create lib280.tree with the specified root node and
* specified maximum arity of nodes.
* @timing O(1)
* @param x item to set as the root node
* @param m number of children allowed for future nodes
*/
public SkillTree(Skill x, int m)
{
super(x,m);
}
/**
* A convenience method that avoids typecasts.
* Obtains a subtree of the root.
*
* @param i Index of the desired subtree of the root.
* @return the i-th subtree of the root.
*/
public SkillTree rootSubTree(int i){
return (SkillTree)super.rootSubtree(i);
}
************************
/* This class stores the data for a single skill in a
* skill lib280.tree for a hypothetical video game.
*/
public class Skill{
String skillName;
String skillDescription;
int skillCost;
Skill(String name, String desc, int cost){
this.skillName = name;
this.skillDescription = desc;
this.skillCost = cost;
}
public String toString(){
return skillName +", Cost: "+ skillCost;
}
/**
* @return the skillName
*/
public String getSkillName(){
return skillName
image text in transcribed

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

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions

Question

show how to do in excel

Answered: 1 week ago

Question

What is the formula used for Return on Assets Ratio

Answered: 1 week ago