Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

42
Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming

Transcript of Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Page 1: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 20031

1001ICT:: lecture 2

Introduction to Programming

Page 2: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

2

Variables

A variable is an item of data named by an identifier. An object stores its state in variables. You must explicitly provide a name and a type for each

variable you want to use in your program.

– The variable's name must be a legal identifier --an unlimited series of Unicode characters that begins with a letter. You use the variable name to refer to the data that the variable contains. The variable's type determines what values it can hold and what operations can be performed on it.

Page 3: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

3

Primitive & Reference Variables

To give a variable a type and a name, you write a variable declaration, which generally looks like this:

type name Every variable must have a data type. A variable's data

type determines the values that the variable can contain and the operations that can be performed on it.

The Java programming language has two categories of data types: primitive and reference. A variable of primitive type contains a single value of the appropriate size and format for its type: a number, a character, or a boolean value.

Page 4: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

4

Primitive variables

The following table lists, by keyword, all of the primitive data types supported by Java, their sizes and formats, and a brief description of each:

Keyword

Description Size/Format

(integers)

byte Byte-length integer8-bit two's complement

short Short integer16-bit two's complement

int Integer32-bit two's complement

long Long integer64-bit two's complement

(real numbers)

floatSingle-precision floating point

32-bit IEEE 754

doubleDouble-precision floating point

64-bit IEEE 754

(other types)

char A single character16-bit Unicode character

boolean

A boolean value (true or false)

true or false

Page 5: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

5

You can put a literal primitive value directly in your code. For example, if you need to assign the value 4 to an integer variable you can write this: int anInt = 4;

The digit 4 is a literal integer value. Here are some examples of literal values of various primitive types:

LiteralData Type

178 int

8864L long

37.266 double

37.266D double

87.363F float

26.77e3 double

' c ' char

true boolean

false boolean

Page 6: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

6

Generally speaking, a series of digits with no decimal point is typed as an integer. You can specify a long integer by putting an 'L' or 'l' after the number. 'L' is preferred as it cannot be confused with the digit '1'. A series of digits with a decimal point is of type double. You can specify a float by putting an 'f' or 'F' after the number. A literal character value is any single Unicode character between single quote marks. The two boolean literals are simply true and false.

Page 7: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

7

Reference variables

Arrays*, classes, and interfaces* are reference types. The value of a reference type variable, in contrast to that of a primitive type, is a reference to (an address of) the value or set of values represented by the variable.

A reference is called a pointer, or a memory address in other languages. The Java programming language does not support the explicit use of addresses like other languages do. You use the variable's name instead.

*discussed later

Page 8: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

8

Variable Names

A program refers to a variable's value by the variable's name.

In the Java programming language, the following must hold true for a variable name:

– It must be a legal identifier. An identifier is an unlimited series of Unicode characters that begins with a letter.

– It must not be a keyword, a boolean literal (true or false), or the reserved word null.

– It must be unique within its scope. A variable may have the same name as a variable whose declaration appears in a different scope. In some situations, a variable may share the same name as another variable if it is declared within a nested block of code. (variable scope will be discussed next)

Page 9: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

9

By Convention :  Variable names begin with a lowercase letter, and class names begin with an uppercase letter. If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter, like this: isVisible. The underscore character (_) is acceptable anywhere in a name, but by convention is used only to separate words in constants (because constants are all caps by convention and thus cannot be case-delimited).

Page 10: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

10

examples of variable declarations:– int years;– int days = 7;– long seconds;– double interestRate;– float sharePrice = 3.2456F;– boolean gameOver;– boolean accountOverdrawn = false;

Page 11: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

11

Short quiz

What is a variable? What are the 2 categories of variable types? What are the main primitive types? Declare the following variables, giving them appropriate

data types:

variable descriptionstudents stores number of students

speedOfLight stores speed of light (surprise!)

isAlive stores whether something is dead or alive

orderTotal stores currency value of order total

Page 12: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

12

Scope

A variable's scope is the region of a program within which the variable can be referred to by its simple name. Secondarily, scope also determines when the system creates and destroys memory for the variable.

The location of the variable declaration within your program establishes its scope and places it into one of these four categories:

– member variable – local variable – method parameter – exception-handler parameter (discussed in 1104CIT

programming II)

Page 13: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

13

Page 14: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

14

A member variable is a member of a class or an object. It is declared within a class but outside of any method or constructor.

A member variable's scope is the entire declaration of the class. However, the declaration of a member needs to appear before it is used when the use is in a member initialisation expression.

Page 15: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

15

You declare local variables within a block of code. In general, the scope of a local variable extends from

its declaration to the end of the code block in which it was declared.

Parameters are formal arguments to methods or constructors and are used to pass values into methods and constructors. The scope of a parameter is the entire method or constructor for which it is a parameter.

Parameters will be discussed further when we discuss methods in more detail.

Page 16: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

16

Expressions & assignment

an expression is a statement which returns a value consider the following code fragment:

int x = 3;int y = 4;int z;z = x + y;

the last line is an expression, since it returns a value the value produced is often called the return value You can provide an initial value for a variable within its

declaration by using the assignment operator (=) just as is done in lines 1 & 2 above

Page 17: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

17

assignment refers to assigning a value to a variable consider again the following code fragment:

int x = 3;

int y = 4;

int z;

z = x + y;

z = z + x;

what does z equal?

Page 18: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

18

the right side of an assignment expression is always calculated before the assignment takes place:

int x = 5;

x = x + 5;

What is the value of x? Consider the following statements:

int x, y, z;

x = y = z = 7; x, y & z will all equal the value 7

Page 19: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

19

Short quiz

Consider the following assignment expressions:int x, y, z;

y = 4;

z = 3;

z = z + y + 1;

y = y - 2 + z;

x = z - y;

x = x + 2;

What is the final value of the variable x?

Page 20: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

20

Which of the following are valid variable names? 1. int

2. anInt

3. i

4. i1

5. 1

6. thing1

7. 1thing ONE-HUNDRED

1. ONE_HUNDRED

2. something2do

Page 21: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

21

What is another word for a class?a) Object

b) Template

c) Instance

What does an instance method of a class represent?a) the attributes of that class

b) the behaviour of that class

c) the behaviour of an object created from that class

Page 22: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

22

Answers

0 2, 3, 4, 6, 9, 10 A class is an abstract template used to create objects

that are similar to each other instance methods refer to a specific object’s behaviour.

Class methods refer to the behaviour of all objects belonging to that class.

Page 23: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

23

Example Program: Currency Converter

Problem statement:– create a program which converts Australian Dollars (AUD) to

US Dollars (USD)

Page 24: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

24

Glossary

API – Application Programming Interface. The specification of how a

programmer writing an application accesses the behavior and state of classes and objects.

applet – A component that typically executes in a web browser, but can

execute in a variety of other applications or devices that support the applet programming model.

ASCII – American Standard Code for Information Interchange. A

standard assignment of 7-bit numeric codes to characters. See also Unicode.

Page 25: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

25

bit – The smallest unit of information in a computer, with a value of

either 0 or 1.

boolean – Refers to an expression or variable that can have only a true or

false value. The Java programming language provides the boolean type and the literal values true and false.

byte – A sequence of eight bits. The Java programming language

provides a corresponding byte type.

Page 26: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

26

char – A Java programming language keyword used to declare a

variable of type character. class

– a template for an object that contains variables to describe how the object behaves. Classes can inherit variables and methods from other classes.

class method – a method that operates on a class itself rather than on specific

instances of a class. Class methods are invoked without reference to a particular object. Class methods affect the class as a whole, not a particular instance of the class. Also called a static method. See also instance method.

Page 27: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

27

class variable– a variable that describes an attribute of class instead of

specific instances of the class. A data item associated with a particular class as a whole--not with particular instances of the class. Class variables are defined in class definitions. Also called a static field. See also instance variable.

comment – In a program, explanatory text that is ignored by the compiler.

In programs written in the Java programming language, comments are delimited using // or /*...*/.

Page 28: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

28

compilation unit – The smallest unit of source code that can be compiled. In the

current implementation of the Java platform, the compilation unit is a class.

compiler – A program to translate source code into code to be executed

by a computer. The Java compiler translates source code written in the Java programming language into bytecode for the Java virtual machine1. See also interpreter.

deprecation – Refers to a class, interface, constructor, method or field that is

no longer recommended, and may cease to exist in a future version.

Page 29: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

29

double – A Java programming language keyword used to define a

variable of type double.

encapsulation – The localization of knowledge within a module. Because

objects encapsulate data and implementation, the user of an object can view the object as a black box that provides services. Instance variables and methods can be added, deleted, or changed, but as long as the services provided by the object remain the same, code that uses the object can continue to use it without being rewritten. See also instance variable, instance method.

Page 30: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

30

exception – An event during program execution that prevents the program

from continuing normally; generally, an error. The Java programming language supports exceptions with the try, catch, and throw keywords. See also exception handler.

exception handler – A block of code that reacts to a specific type of exception. If the

exception is for an error that the program can recover from, the program can resume executing after the exception handler has executed.

Page 31: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

31

field – A data member of a class. Unless specified otherwise, a field is

not static.

float – A Java programming language keyword used to define a

floating point number variable.

garbage collection – The automatic detection and freeing of memory that is no

longer in use. The Java runtime system performs garbage collection so that programmers never explicitly free objects.

Page 32: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

32

GUI – Graphical User Interface. Refers to the techniques involved in

using graphics, along with a keyboard and a mouse, to provide an easy-to-use interface to some program.

import – A Java programming language keyword used at the beginning

of a source file that can specify classes or entire packages to be referred to later without including their package names in the reference.

Page 33: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

33

instance– the same thing as an object. Each object is an instance of

some class. In programs written in the Java programming language, an instance of a class is created using the new operator followed by the class name.

instance method – a method of an object that operates on that object by

manipulating the values of its instance variables. Because instance methods are much more common than class methods, they are often just called methods.

Page 34: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

34

instance variable – a variable that describes an attribute of an instance of a class

instead of the class itself. Any item of data that is associated with a particular object. Each instance of a class has its own copy of the instance variables defined in the class. Also called a field. See also class variable.

int – A Java programming language keyword used to define a

variable of type integer.

Page 35: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

35

interface – A Java programming language keyword used to define a

collection of method definitions and constant values. It can later be implemented by classes that define this interface with the "implements" keyword.

interpreter– A module that alternately decodes and executes every

statement in some body of code. The Java interpreter decodes and executes bytecode for the JavaTM virtual machine1. See also compiler, runtime system.

Page 36: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

36

JVM (Java Virtual Machine)– A software "execution engine" that safely and compatibly

executes the byte codes in Java class files on a microprocessor (whether in a computer or in another electronic device).

local variable – A data item known within a block, but inaccessible to code

outside the block. For example, any variable defined within a method is a local variable and can't be used outside the method.

long – A Java programming language keyword used to define a

variable of type long.

Page 37: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

37

member – A field or method of a class. Unless specified otherwise, a

member is not static.

method – a group of statements in a class that defines how the class’s

objects will behave. Methods are analogous to functions in other languages but must always be located inside a class

new – A Java programming language keyword used to create an

instance of a class.

Page 38: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

38

object– an instance of a class. Multiple objects that are instances of

the same class have access to the same methods but often have different values for their instance variables. The principal building blocks of object-oriented programs. Each object is a programming unit consisting of data (instance variables) and functionality (instance methods). See also class.

object-oriented design – A software design method that models the characteristics of

abstract or real objects using classes and objects.

Page 39: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

39

scope – A characteristic of an identifier that determines where the

identifier can be used. Most identifiers in the Java programming environment have either class or local scope. Instance and class variables and methods have class scope; they can be used outside the class and its subclasses only by prefixing them with an instance of the class or (for class variables and methods) with the class name. All other variables are declared within methods and have local scope; they can be used only within the enclosing block.

Page 40: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

40

static – A Java programming language keyword used to define a

variable as a class variable. Classes maintain one copy of class variables regardless of how many instances exist of that class. "static" can also be used to define a method as a class method. Class methods are invoked by the class instead of a specific instance, and can only operate on class variables.

short– A Java programming language keyword used to define a

variable of type short. static field

– Another name for class variable.

Page 41: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

41

static method – Another name for class method.

Unicode – A 16-bit character set defined by ISO 10646. See also ASCII.

All source code in the Java programming environment is written in Unicode.

variable – An item of data named by an identifier. Each variable has a

type, such as int or Object, and a scope. See also class variable, instance variable, local variable.

Page 42: Friday, 24 October 2003 1 1001ICT:: lecture 2 Introduction to Programming.

Friday, 24 October 2003

42

virtual machine– An abstract specification for a computing device that can be

implemented in different ways, in software or hardware. You compile to the instruction set of a virtual machine much like you'd compile to the instruction set of a microprocessor. The Java virtual machine consists of a bytecode instruction set, a set of registers, a stack, a garbage-collected heap, and an area for storing methods.

void – A Java programming language keyword used in method

declarations to specify that the method does not return any value. "void" can also be used as a nonfunctional statement.