02 Variables1November Variables CE : Fundamental Programming Techniques
02 Variables2November 15 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 discuss downloading the JDK and Netbeans
02 Variables3November 15 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: 20a3.141fred
02 Variables4November 15 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 Variables5November 15 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; countaverage
02 Variables6November 15 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 Variables7November 15 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 Variables8November 15 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 Variables9November 15 Arithmetic operators Java supports the standard arithmetic operators: all expressions must be given in full OperatorMeaning *multiply /divide %remainder +add -subtract energy = mass * speedOfLight * speedOfLight; //e = mc 2
02 Variables10November 15 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 OperatorPrecedence * / %carried out before + and - + -carried out after *, / and % average = (a + b) / 2;
02 Variables11November 15 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 Variables12November 15 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 Variables13November 15 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 Variables14November 15 //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 Variables15November 15 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 Variables16November 15 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 = ( ) / 4.0; //ave = 2.5 double ave = ( ) / 4; //ave = 2.0 int ave = ( ) / 4; //ave = 2
02 Variables17November 15 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: download instructions are available from the FPT web site
02 Variables18November 15 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 considered downloading the JDK and Netbeans In the next session we will: look at more calculations input data into programs