Hi, I'm trying to write a Dragon fight class for CodeHS and I'm having trouble doing so. This is what the exercise says I need to do:
Write a class called Dragon. A Dragon should have a name, a level, and a boolean variable, canBreatheFire, indicating whether or not the dragon can breathe fire. The class should have getter methods for all of these variables - getName, getLevel, and isFireBreather, respectively.
Dragon will also need a constructor, a method to gain experience, and a method to attack.
The constructor should take the name as the first argument and the level as the second argument. The constructor should initialize canBreatheFire based on the dragon’s level. If the dragon is level 70 or higher, the dragon can breathe fire (meaning the third member variable should be set to true).
You should create three getter (accessor) methods called getName(), getLevel(), and isFireBreather().
You should also create a method called attack(). This method does not return anything. If the dragon can breathe fire, it should print
That’s 10 angle brackets, then 14, then 14, then 10 (or copy and paste them!)
If the dragon cannot breathe fire, it should print
~ ~ ~
Finally, you should have a method called gainExperience that takes no parameters. It should add 10 to the dragon’s level and update canBreatheFire if necessary.
In summary, you should have the following methods:
A constructor that takes two parameters - the name and level (in that order)
And this the code that I have so far:
When I check the code in the grader, I get two errors: "Dragon java: Line 62: illegal start of expression, and Dragon java: Line 62: you forgot a semicolon (even though I don't think that I'm supposed to have one there), probably at the end." If anyone could help, I'd apprerciate. Thanks in advance.
Emiel Regis wrote:But if I have to declare getExperienceRepeat, then don't I have to declare attackRepeat as well? Also, I'm confused as to where I'm missing a bracket.
yes, you have to declare both variables getExperienceRepeat as well as attackRepeat.
The bracket issue is quite evident, you have mistakenly defined attack method inside getExperience. This is why you're getting the compilation issue.
Further, even declaring getExperienceRepeat, attackRepeat wont completely solve your issue, your method return type is String. If you declare getExperienceRepeat as a String, then the following code does not make much sense:
Similarly, attackRepeat should have the same issue.
I don't think the problem requires any loop for getExperience
Carey Brown wrote:You can't define a method inside another method. If you clean up your indentation you'd probably see the problem.
Okay, I think I fixed the indentation issue. Now it looks like this:
But now that I've changed the indentation, the grader is telling me that either I haven't declared isFirebreather() or it's out of scope. Also, I honestly have no idea how to fix or even do the getExperience method; I just guessed all of the code when I wrote it.
If you are using an IDE, most of them have auto-formatting capability (e.g. indenting). If you're not using an IDE look at my signature just below and you'll see a link to JavaIndenter. This is Java source code for an indentation program. It's pretty basic and only does Alman style indentation, not K&R, but it suffices for most beginners' needs.
Also, when you post code, always remember to use Code tags so that the indentation is properly shown. Also shows line numbers. You seemed to have used Code tags on your earlier post but not the last one. I'll tweak it for you this time but please always use Code tags.
Emiel Regis wrote:Also, I honestly have no idea how to fix or even do the getExperience method; I just guessed all of the code when I wrote it.
We do not want you to guess code. I have looked at your problem statement in depth and it looks quite interesting. I suggest approaching your problem step by step.
Can you create a simple class called Dragon which has three fields : name, level and canBreatheFire ?
Don't worry about the rest of the code, we can guide you along.