• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to add some numbers  RSS feed

 
Greenhorn
Posts: 6
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I'm new here! But that's besides the point. I'm trying to make a program that calculates the various damage stats (Burst Damage, Damage per Second, etc.) just by asking the weapon and level. I have succeeded in being able to calculate the stats of 1 weapon using a bunch of if statements, but I have no idea how to add the stats of two or more weapons. For example, I want to know what the burst damage of two weapons is, so for both I use if word1.equals blah int BD_1 = blah and if word2.equals blah int BD_2 = blah. So how would I go about in adding BD_1 and BD_2 together? The only thing I can think of is int BD = (BD_1 + BD_2); but it can't find BD_1 and BD_2. I can supply more info if neccisary.

Please help me if you can.
 
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code with comments describing what it is trying to do and how it is going to do it?
 
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Justyn Durnford,

I think that this is a job for collections and/or lists.



Now that you have a collection you can do a lot more things if you wanted to.

You could also implement an interface for the damage done by the weapons and then loop through all of the objects that support that interface.

However like @Norm Radder mention some of your code would be helpful.
 
Justyn Durnford
Greenhorn
Posts: 6
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I have arrays of the damage for each weapon. Each weapon has 12 levels so there are 12 numbers in each array. Some are 0 because the weapon doesn't exist at that level. I basically have it ask what the weapon is and what level it is, and so on for the next weapon.

int[] blah = 12 numbers

System.out.println("Hello!");
System.out.println("What is your first weapon? You can choose from: blah blah");
String word1 = keyboard.next();

Then later is goes
if(word1.equals("blah")){
int BD_1 = blah[ number1-1 ]*something
System.out.println("BurstDamage: " +BD_1);
 
Pete Letkeman
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that if you put you weapons into a collection or array or arraylist then you may be able to loop through all of the active weapons.

If you change the if statement to a switch statement things may be easier to follow and you can still add the weapons to the collection:



Please use the code format feature when supplying code to make it easy for everyone else to read.
Of course I'm only using ArrayList and there are other collection objects.
You can also do things with collections and streams including predicates and lamdas.

I'm going ask for some of you actual code if possible. Are you able to share your Weapon class definition?
 
Justyn Durnford
Greenhorn
Posts: 6
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I did that then where would I put the arrays for the weapon levels?
 
Pete Letkeman
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a limited number of levels...say 12 like in the code you posted earlier then you could make that an enumerated set using enums. Or for weapon levels you could use interfaces. With Java 8 you can have default methods so you would only need to overwrite the levels that are supported by each weapon as listed below:



Doing something like this with enums is possible as well and you can have an array of enum values. From what I gather enums can act almost like classes as they can have custom methods. Perhaps enums would be the cleaning/easier way to go. I'll see if I can create something like this with enums to give you a starting point.

I should probably point out that the use of default methods inside of an interface as I have done if frowned upon by some developers. The default methods were added mainly for backwards compatibility.

To answer your question directly, you can put the array of weapon levels in the weapon class.
 
Justyn Durnford
Greenhorn
Posts: 6
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this is a portion of my code.



These are the arrays for the weapons. Each number represents how much damage a single bullet/missile/burst deals at the corresponding level.
      
      

This is where the program gets the weapon name and level number.



This is the punisher, for an example.
 
Pete Letkeman
Ranch Foreman
Posts: 920
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is what I've got thus far and I think it covers the bases:



I think that I'd make the following methods/properties of the weapons class so that each weapon could have their on secret formula:
  • BurstDamage
  • DamagePerSecond
  • DamagePerSecond2
  • DamagePerMinute


  • There are still a few checks that can be added to the set methods, but that shouldn't be too hard to do.
     
    Greenhorn
    Posts: 27
    Android Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you use Java 8, take a look at the Stream interface. It provides a lot of functions to iterate through the list and reduce results
     
    Justyn Durnford
    Greenhorn
    Posts: 6
    Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm using Blue J for now.
     
    Stefano Carniel
    Greenhorn
    Posts: 27
    Android Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If I'm not mistaken, BlueJ is an IDE, what's the problem to use it with version 8 of Java?
     
    Marshal
    Posts: 56610
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have suffered from BlueJ in the past, and am pleased to have got over it. That has left me with a lifelong dislike for BlueJ .
    I don't like all those arrays, which look error‑prone. Does passing an array mean that a Weapon can have different damage values from all other Weapons, even of the same design?
     
    Pete Letkeman
    Ranch Foreman
    Posts: 920
    26
    Android Chrome IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I not a large fan of all of the array either. I think that a better solution would be to read in the values from a database source (which could be a flat text file). I can do some database operations in Java, but not enough to be helpful to anyone.

    Here is some pseudo of what could work:

    1) Have the user select a weapon
    2) Have the user select a weapon level
    3) Do a read from the database for that one record, which could be something like:

    4) Fight with weapon

    If you keep the weapon details in a database then you can easily update any of the weapon information and/or add/delete weapons as your project matures.
    I know that some people are new to databases, however there are many people out there that willing and able to help plus there are videos on YouTube and many tutorials on the web.

    I don't have any experience with Blue J, however I do think that IntelliJ is a solid editor and there is a free community version that you can download directly from their web site.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!