# Variables - Fundamental Programming Techniques

Λογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 9 μήνες)

638 εμφανίσεις

02 Variables

1

March 13

02 Variables

CE00858
-
1:

Fundamental Programming Techniques

02 Variables

2

March 13

Objectives

In this session, we will:

look at integers and doubles

declare and initialise variables in Java applications

perform arithmetic operations on numbers

output values

02 Variables

3

March 13

Handling data

most programs need to handle data

to solve a problem, we need to think about:

what data is used

what results are required

how we convert the data into the results

program must store data in memory and access it later

memory is like boxes that can contain values:

20

a

3.141

fred

02 Variables

4

March 13

Numbers

most programs use numbers

may be whole numbers or decimal point numbers

whole numbers:
integer

days in a week

legs on a dog

pages in a book

decimal point numbers:
double

distance between Stafford and Bristol in kilometres

average exam mark for a group of students

person’s reaction time in seconds

02 Variables

5

March 13

Declaring variables

values stored in variables may change as program runs

variables have:

a name

a type

a value

must declare variable before use

int count;

double average;

count

average

02 Variables

6

March 13

Variable names

all names are case sensitive

rules for variable names:

may be any length

must start with a letter _ or \$

must not contain spaces, tabs or special characters

must not be keywords (main, void, public, etc)

conventions for variable names:

first letter should be lowercase

each new word starts with a capital

should be meaningful

02 Variables

7

March 13

Initialisation

giving a variable an initial value

good practice to initialise variables

error raised if variable doesn't have a value before it is
used

int count = 10;

double average = 2.5;

10

count

2.5

average

02 Variables

8

March 13

Assignment

storing data in an existing variable

evaluates right hand side of equals sign and
overwrites

contents of variable on left hand side

count = 46;

average = 9.75;

46

count

9.75

average

02 Variables

9

March 13

Arithmetic operators

Java supports the standard arithmetic operators:

all expressions must be given in full

Operator

Meaning

*

multiply

/

divide

%

remainder

+

-

subtract

energy = mass * speedOfLight * speedOfLight; //e = mc
2

02 Variables

10

March 13

Operator precedence

operators have precedence which determines the order in
which they are evaluated:

operators with same order of precedence are evaluated
from left to right

brackets can override the order of operation

Operator

Precedence

* / %

carried out before + and
-

+
-

carried out after *, / and %

average = (a + b) / 2;

02 Variables

11

March 13

Displaying variables

contents of variables can be output using calls to
System.out.print and System.out.println

can output more than one thing using concatenator, +:

System.out.println("Count is: " + count + " items");

System.out.print("Answer is: " + ans);

02 Variables

12

March 13

Analysis of calculations

need to consider what data:

type

value

where it comes from

input by user

hard
-
coded in program

calculated by program

what operations

calculations required

output needed

02 Variables

13

March 13

Calculation example

Rectangle

problem:

calculate and output area of rectangle given width of 10 and
height of 20

analysis
:

what data?

width: integer set to 10

height: integer set to 20

area: integer calculated in program

what operations are performed?

area = width
* height

output area

02 Variables

14

March 13

//calculate and output area of rectangle

public class Rectangle

{

public static void main (String[] args)

{

int width = 10;

int height = 20;

//calculate area

int area = width * height;

//output area

System.out.println ("The area is " + area);

}

}

Rectangle.java

02 Variables

15

March 13

Integers and doubles

double variable can store integer values:

integer variable cannot store double values:

double average;

int count = 7;

average = count;

double average;

int count = 7;

count = average;

integer is promoted to a double

potential loss of accuracy

fails to compile

02 Variables

16

March 13

Division

if both operands are
integers

and result is
integer
, integer
division takes place:

if both results are
integer

and result is
double
, integer division
takes place and result is promoted to double:

if either result is
double
, result must be
double

and real
division takes place:

double ave = (1 + 2 + 3 + 4) / 4.0;

//ave = 2.5

double ave = (1 + 2 + 3 + 4) / 4;

//ave = 2.0

int ave = (1 + 2 + 3 + 4) / 4;

//ave = 2

02 Variables

17

March 13

Java Development Kit & NetBeans

we will be using Java Development Kit (JDK)

Java SE

versions are available for Windows and Linux

NetBeans is an integrated development for developing
Java application

JDK and NetBeans are available from Sun

visit the Sun Java website:
http://java.sun.com

download instructions are available from the FPT web site

02 Variables

18

March 13

Summary

In this session we have:

looked at when we use integers and doubles

declared, initialised and used variables in Java applications

performed calculations using numbers

output results of calculations