Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic, Class Variables and Class Methods Week 11

Similar presentations


Presentation on theme: "Arithmetic, Class Variables and Class Methods Week 11"— Presentation transcript:

1 Arithmetic, Class Variables and Class Methods Week 11

2 CONCEPTS COVERED THIS WEEK
Arithmetic Class Variables Class Constants Class Methods

3 Variables A variable is a location in the computer’s memory that is used to store data A variable has a name, a type and a value associated with it int mark = 10; mark – its name int – its type – what we can store in it, in this case integers value what is currently stored in it The statement can be broken down into two separate statements int mark; // declaration mark = 10; // assignment

4 Variable Declarations
Variables must be declared before they are used. To declare a variable we give it a name and a type i.e. what type of data can be stored in it e.g. int mark; double price; String name;

5 Assignment To put a value in a variable we use the assignment operator = mark = 10; price = 3.64; name = “Fred”;

6 Arithmetic Operators Addition + Subtraction - Multiplication *
Division / For example: int result = * 4; What value is assigned to result?

7 Order of Operations int result = 2 + 3 * 4;
result is assigned a value of 14 multiplication and division take precedence over addition and subtraction brackets can be used e.g. int result = (2 + 3) * 4; result is now assigned a value of 20

8 Programming with Classes
CLASS VARIABLES How can we give each Triangle a unique object number?

9 Programming with Classes
CLASS VARIABLES How can we give each Triangle a unique object number? First Idea: Create an int field called number and add 1 to it every time the object constructor is called. public class Triangle { // instance variables private int number; ... // Triangle constructor public Triangle() { number++; System.out.println("Triangle object " + number + " created"); }

10 Programming with Classes
CLASS VARIABLES How can we give each Triangle a unique object number? First Idea: Create an int field called number and add 1 to it every time the object constructor is called. This does not work as number is an instance variable and therefore re-initialised to zero each time a Triangle is instantiated/created

11 Programming with Classes
CLASS VARIABLES How can we give each Triangle a unique object number? Second Idea: We need to create a non-instance count that can be incremented after each new Triangle, and assign its current value to the instance variable number. This is precisely the purpose behind the creation of so-called static, or Class, variables Class variables hold state that is shared in common among all objects of the class.

12 Programming with Classes
CLASS VARIABLES public class Triangle { // class variable private static int count; // instance variables private int number; ... // Triangle constructor public Triangle() { count++; number = count; System.out.println("Triangle object " + number + " created"); }

13 Programming with Classes
CLASS VARIABLES How can we give each Triangle a unique object number? Second Idea: Create a static variable called count and add 1 to it each time the constructor is called. Assign the value of count to an instance variable called number in the constructor. This does work as count is not re-initialised to zero each time a new Triangle object is created

14 Programming with Classes
CLASS VARIABLES Triangle count 3 is instance of… is instance of… is instance of… triangle1: Triangle number 1 height 30 width 40 color “green” triangle2: Triangle number 2 height 30 width 40 color “green” triangle3: Triangle number 3 height 30 width 40 color “green”

15 Programming with Classes
CLASS CONSTANTS Class constants are class variables that are given a fixed value using the keyword final. They should be written in UPPERCASE with underscores between the words that make up compound variable names. For example: In a class that represents a bouncing ball it might be useful to declare a constant as follows: private static final double GRAVITATIONAL_ACCELERATION = 9.8; There is a Flash demo of bouncing balls at:

16 Programming with Classes
CLASS METHODS How can we access the value of a class variable? We could use an object method but this would require that an object had already been created and we had its reference. Also, why should we have to create an object to reference a class variable? It seems rather odd to create a BouncingBall object in order to find the value of gravity that will effect ALL bouncing balls.

17 Programming with Classes
CLASS METHODS How can we access the value of a class variable? A much better solution would be to have a method that related to the BouncingBall class, rather than to a specific BouncingBall object, as this method has no interest in any specific object’s state. Such methods are called Class Methods (they are also often referred to as Static Methods), and they are defined as part of a class definition in the same way as object methods.

18 Programming with Classes
CLASS METHODS public class Triangle { private static int count; // class variable private int number; // instance variable // Triangle constructor public Triangle() { count++; number = count; } // get number of Triangle objects created public static int getCount() { return count;

19 A look at class methods/variables in the Triangle class
BlueJ Demonstration A look at class methods/variables in the Triangle class

20 CLASS VARIABLES AND METHODS
Programming with Classes CLASS VARIABLES AND METHODS Class variables hold state that is shared in common among all objects of class Class methods act on attributes of the whole class through class variables Class methods can’t access instance variables


Download ppt "Arithmetic, Class Variables and Class Methods Week 11"

Similar presentations


Ads by Google