This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Joe Ess
  • salvin francis
  • fred rosenberger

Method problem

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm new to Java, learning it for my Computing course at university.

For a project over Easter I must make a simple game on the concept of rock, paper, scissors.
Note the game is nothing graphical yet, just command line stuff.

The first activity is to create a class to make the base units. (This game is using land, sea and air instead of rock, paper and scissors.)
I have done this, however I need methods to make the units attack each other.

I need two methods, one to attack, and another to calculate the damage. The attack method is called and uses the damage method to deal damage to the other unit.

How can I instruct the attack method to use the damage method?

Here is my code:

and the main method to test this:

The attack and damage methods are currently incomplete. If I comment them out and run the program, it returns this result:

Base Unit (100/100)
Gary (75/100) false

So how can I code this problem? I remember one of my classmates working on it, and having something like

Is this on the right lines?

I hope you can help me.

Thanks in advance.
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, I don't think you will find anyone to do your homework for you on this or any other site.

With that in mind, all you have done, providing that everything compiles okay, is to output the data
represented by your first reference to the Unit object, and the second reference. Also, our not passing anything into the dead
method so it is really not doing anything useful.

status should be a class member like boolean STATUS.

What you might think about doing is to give paper, rock, and scissors a value to compare against one another.
Then you would have something to compare against.

Posts: 67435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

What is the d parameter in the damage() method? You appear not to be using it; why not?

I think you need to get a pencil and paper and write down what each method is supposed to do, and how the class should be constituted. In the attack method, you need to invoke the damage method on your opponent's object reference.
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!