Monday, September 5, 2016

JAVA 16 - Classes ... within classes.

So far we've defined a class and even extended a class.

Once defined, you can use a class pretty much as you'd use any variable data types - meaning you can have classes which use classes within them.  You can also have arrays (or better, list arrays) of your class.

In this final part on Java theory, we're going to do just that!

I hate them dices to pieces


So far we've made a great class which simulates the action of a single dice.  But as mentioned, sometimes we need more than just another dice.

I want to keep the dice class as it is, simulating the random-ness of dice.  But I want to have a layer outside of that which will allow me to create groupings of dice.  And within this class I'm going to put a bit more of the rules of the game, and managing my expectations around multiple dice.

This class I'm calling diceGroupClass.

Attributes


In terms of attributes, one of the most important things is to declare a List for my dice.  I know it's going to be important to keep track of how many dice I have, and I did wonder if I should have a integer numDice attribute, but I can use the .size() method that's built into List to keep track of my dice quite easily.

Constructors


I'm expecting a number to be passed on creation of an object for this class - this number will defined the number of dice to be rolled in the group.  If no number is supplied, I'm going to assume it to be 2 dice, and call the constructor with it.

The constructor calls new ArrayList<diceClass>() to set up an ArrayList for the private attribute (though it's declared as the interface type of List in the variables section - see here for why).

It also passes the number of required dice to a function, setNumDice.

setNumDice



I had to have a big think about this - whether I wanted it to be private or public.  I decided to make it public, because I wanted to be able to dynamically change the number of dice I'm using (you'll see why later).

This method is passed the number of dice required, and compares with diceList.size(), then it either,
  • Uses a while loop to use the .add() method to add elements to the ArrayList until it's large enough
  • Uses a different while loop to use the .remove() method to remove elements from the ArrayList until it's small enough.

Yup - I know I'd typically pick on the ISTQB if they had a definition for a method that says .add() adds and .remove() removes.  But folks, it really is what-you-see-is-what-you-get there.

rollAllDice


This uses the really neat loop we've talked about previously for ArrayLists,

for(diceClass thisDice:diceList)

Will loop through each dice in our ArrayList, assigning the element to the handle thisDice.  We then use the .rollDice() method of that class to roll the dice.

sumAllDice


This rolls all the dice using the above method.  Then similar to rollAllDice, loops through each dice element, and adds the dice value to a running total, to get the sum of all the numbers on the dice.

getDiceOverThreshold


This is very similar to sumAllDice, but instead of the total, it uses the .diceOnOrOver method to compare each dice to a rollable number, and return 1 if the dice equals or exceeds.

I've not made a group method like this for .diceOnOrUnder - if you like, have a go.






@Test Roll two dice


Here I just simply want to roll 2 dice, and sum the result


@Test Warhammer Space Marine shooting simulator


This is a more complex test based on the rules of a game called Warhammer 40,000.

Ten Space Marines are shooting at Orks,
  • Each Space Marine gets 1 shot, which allows a dice roll
  • Each shot hits on a dice roll of 3+
  • For each hit you get, you re-roll and cause a wound on a roll of 4+


Here you can see I use the constructor to pass that I originally want 10 dice to be used, then set numHit to the results of the method .getDiceOverThreshold(3).

I then use .setNumDice to resize my group of dice, and re-roll them, using .getDiceOverThreshold(4) to find how many have causes a wound on a 4+ roll.


As always, my code can be found on Github here.









Congratulations if you're reading this, because you've come to the end of our theory.  I've managed to cover what I consider to be the core basics of Java over the last few weeks.  Remember this series is not everything you need to know, but enough to give you a taste of Java and to get exploring.  But most importantly to get you trying out these exercises, if you haven't been trying out some of these programs and modifying yourself, you're missing the main education to be had!

There's a few more things to cover off before we're done - I'm going to go through a couple more examples, then conclude the series with a set of things for you to try out yourself.

1 comment:

  1. The online gambling model where you can easily use the service.


    จีคลับ Online casino investment services are the place where investors can come to play online gambling games where investors can come into play for the thrilling gambling games. Just a gambler to use online betting services with us here, you will not be disappointed.

    In addition, investors will be playing the gambling game at the end. Upon entering our website, investors will get many bonuses as well. In addition, our website is also available online betting players, players, players who are interested in using the online gambling games with us, you can come into play at all. Absolutely disappointed Also, we have a website that checks the government to investors to check with. Gamblers who have not played before, never used to invest online casino. We have a team to recommend you every day. Do not forget to subscribe here. Today, people who use the service on our website if investors are advised to use the service, investors will get more bonus and then come to use a lot here. บาคาร่า

    ReplyDelete