120 likes | 236 Views
Variables. A variable is just a space in the computer’s memory to store data temporarily. They are called variables because their values may change throughout the program. Generally variables use sensible names and follow camel case notation
E N D
Variables • A variable is just a space in the computer’s memory to store data temporarily. • They are called variables because their values may change throughout the program. • Generally variables use sensible names and follow camel case notation • Every variable has a scope which defines the life of the variable.
Declaring a variable • int x = 1; (A variable of integer type called x. A value of 1 is assigned) • double y = 1.0; (A variable of double type called y. It has been assigned the value of 1.0)
Arithmetic operators + for addition • for subtraction / for division % known as mod. Used for dividing and returning the remainder.
‘=‘ Assignment operator • The assignment operator works by evaluating an expression and assigning it to the variable. For example: int total = 10; total = total + 1; (Here we are adding 1 to the existing value of total which was 10, so after adding the total becomes 11)
Data types • A data type is a classification of identifying one of the various types of data, such as real numbers, integer or Boolean. A data type determines the possible values for that type. • For example: • Int – Integer • Double – real numbers • Char – characters
Instance • As you already know that a class provides a blue print for objects and acts like a template for the objects. When an object is created we say it is an instance of a particular class. • E.g. Karel myrobot1 = new Karel; • In the above example we have created an object called myrobot1 from the Karel class. The ‘new’ operator creates an instantiation of a class.
Classes as Hierarchy • Remember you wrote a class which extends Karel and gives you the basic functionality of Karel • And then we extended SuperKarel which gave us even more functions like turnRight Karel SuperKarel Subclass
Super class Karel Super Class Super Karel Sub Class
Classes, objects and instances Animals Digest food Blastula stage Warm Blooded Have memory glands Five fingers Monkeys Highly developed brains Alex & Sam are objects of the human class Object is an instance of a class Alex Sam
The ACM (Association for Computing Machinery) was founded in 1947. It is the largest and oldest international scientific and industrial computer society. Association of Computer Machinery