• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Having Trouble With a Dragon Fight Class in CodeHS  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
getName
getLevel
isFireBreather
gainExperience
attack

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.
 
Bartender
Posts: 2166
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all getExperienceRepeat is not declared and you're missing a brace bracket "}".
 
Emiel Regis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:First of all getExperienceRepeat is not declared and you're missing a brace bracket "}".



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.
 
Bartender
Posts: 5637
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't define a method inside another method. If you clean up your indentation you'd probably see the problem.
 
salvin francis
Bartender
Posts: 2166
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Emiel Regis
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Carey Brown
Bartender
Posts: 5637
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Bartender
Posts: 5637
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After adding Code tags to your last post it looks better but doesn't follow normal Java conventions. This is how it should look...
Also note my signature has a link to java formatting style guide.
 
Carey Brown
Bartender
Posts: 5637
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You had in the beginning a method getCanBreatheFire(). Sounds like isFireBreather() would be the same thing. "isFireBreather()" follows typical naming conventions for retrieving boolean values.
 
salvin francis
Bartender
Posts: 2166
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!