Abstracting

 

Abstracting means to ignore or hide everything except essential data that we require for the given problem or situation.

The London tube map is a great example of an abstraction. We don't need info about shops, house addresses or precise directions. We just need station names linked together.

Non-essential information is ignored as we develop an abstraction.

Objectives

Students will be able to:

  • define a class file storing properties
  • define a constructor method
  • create an instance of an object(s) in a computer program

The Farmer Class

public class Farmer{

//first declare the class properties

String name;

int score;

boolean bankrupt;

//then design the constructor method-usually this is used to assign initial data to the object when it is constructed

public Farmer(String name, int score, boolean bankrupt){

//assign parameters to class properties.

this.name = name;

this.score = score;

this.bankrupt = bankrupt;

}

//behaviour (other methods)

public void speak(){

System.out.println("My name is "+name);

}

}

Strategy 1

String name1 = "Jack";

String name2 = "Jill";

String name3 = "Joe";

int score1 = 0;

int score2 = 1;

int score3 = 4;

boolean bs1 = false;

boolean bs2 = false;

boolean bs3 = true;

Strategy 2

String[] names = {"Jack","Jill","Joe"};

int[] scores = {0,1,4};

boolean[] bs = {false,false,true};

Strategy 3

Farmer jack = new Farmer("Jack",0,false);

Farmer jill = new Farmer("Jill",1,false);

Farmer joe = new Farmer("Joe",4,true);

Object Oriented Programming

Barmy Farmers!

Intro to OOP

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.

Task 1

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.

Video Review

The video showed us:

  1. How to create a class file(s)
  2. The basic structure of a class file, including properties and a constructor method, and other methods describing object behaviour eg speak()
  3. How to create a new instance(s) of an object - this is know as instantiation
  4. How to access an object's properties eg System.out.println(joe.score);
  5. How to change/mutate an object's properties eg joe.score = joe.score+1;
  6. How to call an object's methods eg joe.speak();

Reference variables - Review

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}

Class vs Object

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().

Exercises

Click the exercises button and see if you can complete the challenges.

Glossary

object

property

method

create an object/instance of a class

instantiate an object

encapsulate

access

mutate

abstraction

Tags

idesyntax file class instantiate constructor type propertymethod