Difference between revisions of "2020 - Household"
(→Random) |
(→Interesting) |
||
Line 31: | Line 31: | ||
==Interesting== | ==Interesting== | ||
+ | The 'rnd' is a static Random, this means you don't need to create an object of 'Household' to use it. In this case it means you can access the 'rnd' and generate a random number without creating an object. | ||
+ | |||
The 'nextID' is a static int, this means you don't need to create an object of 'Household' to use it. In this case the 'nextID' is static so that each 'Household' created will have different 'ID' values. It essentially increments after each 'Household' is created. | The 'nextID' is a static int, this means you don't need to create an object of 'Household' to use it. In this case the 'nextID' is static so that each 'Household' created will have different 'ID' values. It essentially increments after each 'Household' is created. | ||
Latest revision as of 11:49, 27 January 2020
Contents
Variables
Random
private static Random rnd
Double
protected double chanceEatOutPerDay
Int
protected int xCoord, yCoord, ID protected static int nextID = 1
Methods
Constructor
Accepts the X & Y parameters, and stores them as 'xCoord' and 'yCoord'. It generates a new random double and sets this to the 'chanceEatOutPerDay'. It sets the 'ID' to the current value of 'nextID'. It then increments 'nextID'.
GetDetails
Creates a string to concatenate the details of this household. The created string is then returned.
GetChanceEatOut
Just returns the 'chanceEatOutPerDay'.
GetX
Just returns the 'xCoord'.
GetY
Just returns the 'yCoord'.
Interesting
The 'rnd' is a static Random, this means you don't need to create an object of 'Household' to use it. In this case it means you can access the 'rnd' and generate a random number without creating an object.
The 'nextID' is a static int, this means you don't need to create an object of 'Household' to use it. In this case the 'nextID' is static so that each 'Household' created will have different 'ID' values. It essentially increments after each 'Household' is created.
Having the 'rnd' private means that it is only available to this class and not any subclasses created from it.
Having the protected variables means these are private to just this class, but will also be available in any subclasses created from 'Household'.