Presentation is loading. Please wait.

Presentation is loading. Please wait.

CO320 Catchup David Barnes with material from Dermot Shinners-Kennedy.

Similar presentations


Presentation on theme: "CO320 Catchup David Barnes with material from Dermot Shinners-Kennedy."— Presentation transcript:

1 CO320 Catchup David Barnes with material from Dermot Shinners-Kennedy

2 Teaching? http://blog.lib.umn.edu/isss/isss/teaching.png

3 Learning? http://www.murdoch.edu.au/ciee/pages/Spring 2003 Surfing/Learning to surf 2.JPG

4 Learning? http://surfingsecretsrevealed.com/wp-content/uploads/2010/06/LTS2.jpg

5 Class class: A description of the behaviour of a category or type of thing –A class is a blueprint or template from which instances can be created or constructed. –Example: The Clock class (type) is a template for creating many Clock objects.

6 object: An entity that contains data and behaviour. –data: variables inside the object (private). –behaviour: operations inside the object (public). The data is said to be hidden in the object. You manipulate the data using the operations. The operations are called methods. Accessors and Mutators. –Breakfast question. Object Instance Variables (Object State) Operations/ Methods (Behaviour)

7 One Class – Many Objects Every object or instance of a class has the same structure and behaviour

8 Many objects – different states http://remodelista.com/img/sub/retrouvius-stock-market-clocks.jpg

9 Many Objects – Different States The only thing that differs is the STATE (i.e., the values in the variables) For example, the objects shown below are ALL clocks but with different times.

10 Basic methods Find out the state – use an accessor: Clock london = new Clock(); System.out.println(london.getTime()); Change the state – use a mutator: london.setTime(…); E.g., put the clock back one hour: london.setTime( london.getTime() – 1000*60*60 );

11 public class Clock { private long time; public Clock() { time = …; // Obscure the details. } public long getTime() { return time; } public void setTime(long millis) { time = millis; }

12 public class Clock { private long time; public Clock() { time = …; // Obscure the details. } public long getTime() { return time; } public void setTime(long millis) { time = millis; } Constructor Field Accessor Mutator

13 Some rules of thumb The following slides give some rough guidelines for writing fields, constructors, get- and set- methods. They aren’t all true in all cases, but they are true in a large proportion of the cases.

14 Basic field ‘rules’ private Has a type. E.g., int, long, boolean, String, Text, etc. Has a name – your choice. Has a semicolon at the end. There may be more than one field. –Group together inside the first curly bracket.

15 Basic constructor ‘rules’ Comes after the fields. public Always has the same name as the class. Nothing between public and the constructor name. Round brackets after the name. Often takes parameters in the round brackets. Curly brackets mark the constructor body. Inside the body it initialises the fields. May be more than one – different parameters.

16 Accessor (get-method) ‘rules’ Used to return the value of a particular field. public return-type that matches the type of the field. Method name starting with get. No parameters, but has the round brackets. Body with a single statement returning the field.

17 Mutator set-method rules Used to set a replacement value in a field. public void return type. Method name starting with set. Takes one parameter (a variable). The parameter’s type is the same as the field’s type. The parameter’s name related to the field’s. Body with a single statement assigning the parameter variable to the field variable.

18 Example 1 A class called Census with a single integer field called count. The constructor initialises the field to be zero.

19 public class Census { private int count; public Census() { count = 0; } public int getCount() { return count; } public void setCount(int c) { count = c; } Constructor Field Accessor Mutator

20 Example 2 A class called Temperature with a single double field called temp. The constructor takes a single parameter. The constructor initialises the field to take the value of the parameter.

21 public class Temperature { private double temp; public Temperature(double t) { temp = t; } public double getTemp() { return temp; } public void setTemp(double t) { temp = t; } Constructor Field Accessor Mutator

22 Example 3 A class called Pet with a single String field called name. The constructor takes a single parameter. The constructor initialises the field to take the value of the parameter. Write this one yourself...

23 Exercise Make up your own. Things to vary: –The name of the class. –The name of the field. –The type of the field. –Whether the constructor takes a parameter or not.

24 The centrality of fields Objects are essentially all about storing state. State is what is stored in an object’s fields. Get- and set- method allows us to either inspect (get) or change (set) the state. Variation comes from: –More complex state (more fields). –More complex changes to state (more methods).

25 Further exercises Write some methods with more than one int field. Add some methods that make different sorts of changes to an object’s state. For instance, adding or subtracting the parameter from the field’s current value. Using other arithmetic operators and constant values. Add some methods that combine the values of fields in some way; e.g. adding two together and returning the result or overwriting one of the fields with the result.


Download ppt "CO320 Catchup David Barnes with material from Dermot Shinners-Kennedy."

Similar presentations


Ads by Google