17 Feb 2004 1 of 20 Engineering Software 1 Week 3 Data types and control statements Prepared by Stavros Dimitriou Engineering Software 1 - Week 3 2 of 2017 Feb 2004 Engineering Software 1 Contact details Stavros Dimitriou dimitrsa@lsbu.ac.uk Tower Block - Room T820 Tel.: 0207 815 7580 Unit’s information can be found on the local network at eent3 http://eent3.sbu.ac.uk/staff/dimitrsa/ Engineering Software 1 - Week 3 3 of 2017 Feb 2004 Objectives of session In the preceding session, you learned how to create, compile and run a simple Java program and you were introduced to the structure of a program (identifier, variables, methods, symbols, Java keywords). In this session, (a) you will be introduced to the rest of the data types and operation and, (b) you will learn various selection and loop control statements Engineering Software 1 - Week 3 4 of 2017 Feb 2004 A Java program Compute the area of a circle Algorithm 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x π 3. Display the area Engineering Software 1 - Week 3 5 of 2017 Feb 2004 Compute the area of a circle // ComputeArea.java: Compute the area of a circle public class ComputeArea { Public static void main(String[] args) { Double radius; Double area; // Assign a radius Radius = 20; // Compute area Area = radius * radius * 3.14159; // Display results System.out.println(“The area for the circle of radius” + radius + “ is ” + area); } } Engineering Software 1 - Week 3 6 of 2017 Feb 2004 Identifiers Every entity has a name. Java uses special symbols called identifiers to name programming entities as variables, constants, methods, classes and packages. Rules for naming identifiers: An identifier is a sequence of characters that consists of letters, digits, underscores (_), and dollar signs ($) An identifier must start with a letter, an underscore (_), or a dollar signs ($). It cannot start with a digit. An identifier cannot be a reserved word (See Java keywords) An identifier cannot be true, false or null. An identifier can be of any length. Java is case sensitive, ie. A and a are different entities Engineering Software 1 - Week 3 7 of 2017 Feb 2004 Identifiers and Java Keywords abstract class extends implements import instanceof interface native new package private protected public super this volatile break case catch default do else finally for if return switch synchronised throw throws try while boolean byte char double false final float int long short static transient true void ObjectsControlData Engineering Software 1 - Week 3 8 of 2017 Feb 2004 Variables Variables are used to store data input, data output or intermediate data. In our example program, radius and area are variables of double-precision, floating-point type. To use a variable, you declare it by telling the compiler the name of the variable as well as what type of data it represents (variable declaration). Here are some examples: int x; //declare x to be an integer variable; double radius; // declare radius to be a double variable; char a; // declare a to be a character variable; Engineering Software 1 - Week 3 9 of 2017 Feb 2004 Assignments Statements After a variable is declared, you can assign a value to it by using an assignment statement and the syntax is as follows: variable = expression; An expression represents a computation involving values, variables and operators that evaluates to a value. Examples: int x =1; // Assign 1 to variable x; double radius = 1.0; // Assign 1.0 to variable radius; a = ‘A’; // Assign ‘A’ to variable a; x = 5*(3/2)+3*2; // Assign the value of the expression to x Area = radius * radius * 3.14159; //Compute area Engineering Software 1 - Week 3 10 of 2017 Feb 2004 Constants The value of a variable may change during the execution of the program, but a constant represents permanent data that never change. In our ComputeArea example program, π is a constant. Here is the syntax for declaring a constant: final datatype CONSTANTNAME = VALUE; The word final is a Java keyword which means that the constant cannot be changed. Example final double PI = 3.14159; //Declare a constant Engineering Software 1 - Week 3 11 of 2017 Feb 2004 Numeric data Types Every data type has a range of values. The compiler allocates memory space to store each variable or constant according to its data type. Java has six numeric types: four for integers and two for floating points numbers. 64-bit IEEE 754-1.7E308 to 1.7E308double 32-bit IEEE 754-3.4E38 to 3.4E38float 64-bit signed(-263) to (263) long 32-bit signed(-231) to (231) int 16-bit signed(-215) to (215) short 8-bit signed(-27) to (27) byte Storage SizeRangeName Engineering Software 1 - Week 3 12 of 2017 Feb 2004 Numeric Operators The arithmetic operators for numeric data types include addition (+), subtraction (-), multiplication (*), division (/) and remainder (%). Examples: int i1= 34 +1; // i1 becomes 35 double d1 = 34.0 – 0.1; // d1 becomes 33.9 long i2 = 300 * 30; // i2 becomes 9000 double d2 = 1.0 /2.0; // d2 becomes 0.5 int i3 = 1 / 2; // i3 becomes 0 byte i4 = 20 % 3 // i4 becomes 2 Shortcut Operators (e.g. i = i + 8; or i += 8;) Engineering Software 1 - Week 3 13 of 2017 Feb 2004 Further data types and operators Numerical Type Conversions Character data type (char) Casting between char and numeric types Boolean data type (less tan, greater than, equal to…) Convert strings to numbers (ie. Convert a string into an int or double value) Engineering Software 1 - Week 3 14 of 2017 Feb 2004 Control Statements Program control specifies the order in which statements are executed in a program. Java provides selection statements that let you choose actions with two or more atlernatives. In our ComputeArea example program we can use selection statements in the following pseudocode: If the radius is negative the program displays a message indicating a wrong input; else the program computes the area and displays the result; Engineering Software 1 - Week 3 15 of 2017 Feb 2004 Selection Control Statements if … else statements A simple if statement executes an action only if the condition is true. If the condition is false, nothing is done Here is the syntax for this type of statement: if (booleanExpression) { Statement(s) for the true case; } else { Statements for the false case } Engineering Software 1 - Week 3 16 of 2017 Feb 2004 Selection Control Statements Nested if statements The statement in an if or if … else statement can be any legal Java statement, including another if or if … else statement. Switch statements Java provides a switch statement to handle multiple conditions efficiently. You can use switch statement to replace the nested if statement switch (switch expression) { case value1: statement(s)1; break; ... Engineering Software 1 - Week 3 17 of 2017 Feb 2004 Selection Control Statements Conditional expressions Example: if (x > 0) y = 1; else y = -1; This statement assigns a to y if x is greater than 0 and -1 to y if x is less than or equal to 0. Alternatively: y = (x > 0) ? 1 ; -1; Expression: booleanExpression ? Expression1 : expression2; Engineering Software 1 - Week 3 18 of 2017 Feb 2004 Loop statements Loops are structures that control repeated executions of a block of statements. The while loop while (loop continuation condition) { //loop body; } The do-while loop Is a variation of the while loop; its syntax is given below: do { // loop body; } while (loop continuation condition); Engineering Software 1 - Week 3 19 of 2017 Feb 2004 Loop statements The for loop Syntax: for (initial action; loop continuation condition; action after each iteration) { // loop body; } A for loop generally uses a variable to control how many times the loop body is executed and when the loop terminates. Engineering Software 1 - Week 3 20 of 2017 Feb 2004 Questions ? …Comments END