A computer game, Barmy Farmers, is being designed.
Players act as Farmers and compete to see who can raise the healthiest chickens and sell the most eggs in one month.
Each Farmer in the game will have 3 properties:
Also, every Farmer has a behaviour: (s)he can speak his/her name.
There are several ways that we can design and process the data required for this game:
Use 9 variables!
We can simply assign each item of data relating to each farmer in a variable.
This is reasonable for 3 farmers because only 9 variables are required.
But what if we have 10 farmers? Or 100?
Use 3 arrays
We can create an array to store the names, an array to store the scores and an array to store the bankrupt statuses.
This is a reasonable strategy because the indexes in the arrays create the relationship between the data sets.
However, if we have more information that requires to be stored eg addresses, id numbers, ages etc things can start to get a little messy.
Classify the farmers!
Create a single file which describes the general nature of Barmy Farmers.
Then, use this file to create as many farmer objects as necessary!
Sound wierd? Watch the following video and let's get started with Object Oriented Programming in Java.
The following video intends to act as an introduction to Object Oriented Programming.
Watch the video and complete the Task document.
When the speaker opens his IDE to start coding, open yours and follow along.
The video showed us:
Can you find the 3 reference variables to Farmer objects in the above code?
Reference variables allow our computer program to communicate with the digital objects!
Now, our program can interact with each object eg:
jack.speak();
System.out.println(jill.score);
if(jack.score > jill.score){//do something}
A class defines an object in general. It is like a recipe. It describes an object's properties (sometimes referred to as its state) and methods (sometimes referred to as its behaviour).
When we create an instance of a class (ie construct an object), we can give its properties specific values and call its methods to make it do things eg speak().
Click the exercises button and see if you can complete the challenges.
object
property
method
create an object/instance of a class
instantiate an object
encapsulate
access
mutate
abstraction
idesyntax file class instantiate constructor type propertymethod