Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018
Objectives q By the end of this lecture you should be able to : ü Know rules of programming language ü Know how to start writing a program in java ü Use the input methods of Scanner class to get data from the keyboard ü Distinguish between the eight built-in primitive types of java ü Create constant values with the keyword final
Outlines Ø A Java program syntax Ø Identifiers and Reserved Words Ø Primitive Data Types Ø Type casting Ø ASCII Code Ø Arithmetic Expressions, Rational & Boolean Operators Ø Increment and Decrement Ø Input statement
A Java program /* Here you describe what your program does. */ public class CLASS-NAME { public static void main (String args[]) { // your program goes here } // end of main } // end of class
Some notes q Java is a case-sensitive language. There is a difference between small-letters and capital-letters, for example: o o There is a difference between if and IF or If. The first word is a reserved word, while the two others are not. It s a compilation error to write the word public in capital as Public, or Static. q The java file name, and class name should be the same. q Every class must contain at least one method. q Everything in the class has to be between two braces. q The five output statements can be reduced to a single statement: o System.out.println( ( + x +, + y + ) ) ;
Identifiers q Identifiers are the words and symbolic name that a programmer uses in a program. q These can be assigned to : o Name of a variables, o Name of a methods, o Name of a classes, o Name of functions.
Naming rules 1 2 3 4 Characters allowed to be used in names are: 1 All English Capital Letters (Upper Case Letters ) 2 All English Small Letters (Lower Case Letters) 3 Underscore ( _ ) 4 Currency symbols ( $,, ) Digits( 0, 1, 2,,8,9). Digits are not allowed to be used at the beginning of the name, 5 Spaces are also not allowed, Java keywords are not allowed to be used as identifier name.
Some legal and illegal identifier names Book legal System illegal book legal class illegal _book legal public illegal $book legal @car illegal $Book legal Bo-ok illegal X legal?x illegal _B_O_O_K legal boo k illegal bo$k legal Stu%dent illegal book73 legal 73book illegal add2number legal 2NumberAdd illegal MathewClass legal import illegal While legal while illegal For legal for illegal
Some Java Reserved Words or Keywords: Note: 1. All java reserved words are in small letters, But, don t use those words as a name of a variable, method, or a class. 2. For class names, use names with first letter Capital, like: Ex16, Math12, 3. For methods and variables use names like : addnumbers,multnumbers,...
Primitive Data Types q A data type is a classification mechanism whereby it can be identified that what kind of data is stored inside the variable, and what operations it supports. Type Storage byte Allows for Range byte 1 very small integers -128 to 127 short 2 small integers -32,768 to 32,767 int 4 big integers -2,147,483,648 to 2,147,483,647 long 8 very big integers -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 4 real numbers Approx : +/- 1.4 *10-45 To 3.4 * 10 38 double 8 very big real number boolean 1 true or false true or false. Approx: +/- 4.9 * 10-324 To 1.8 * 10 308 char 2 characters UNICODE character set
Default Values of Java's Primitive Types q The following table lists the default values for Java's primitive data types
Primitive Data Types q Examples: ² int x, y, z ; ² int sum = 0; ² float f; ² double d = 1.4; ² char capitalc = 'C'; ² boolean result = true; ² byte b = 100; ² short s = 10000; ² int i = 100000;
Creating constants q There will be values in program that cannot change during program execution such as: o o o The maximum score in an exam(100). The number of hours in a day(24). The mathematical value of pi. q Constants are declared much like variables in java except that they are preceded by the keyword final. Then that value is fixed and can not later be changed. ² final double PI = 3.14159; ² final int hours= 24; ² hours = 12; // will not compile
Type casting q It s the process of converting type of data from one type to another type. q Example: converting double to int and int to double.
Type casting Example: converting char to int and int to char.
ASCII Code q In computer world, characters can be represented by numbers ASCII, stands for: American Standard Code for Information Interchange. They ranged between 0 to 255. A à 65 B à 66 C à 67.... Z à 90 a à 97 b à 98 c à 99.... z à 122 0 à 48 1 à 49 2 à 50. 9 à 57 à 32
Braces, Parentheses, and Brackets }, braces, used for enclosing body of classes, body of methods, body of loops, body of switch, body of if else statements, q { q ( ), parentheses, used for enclosing method parameters, constructor parameters, arithmetic operations,. q [ ], brackets used for indicating size of arrays
Arithmetic Expressions: q Expression: is a collections of literals (constant), variables, operators, and parentheses: q Literals: q Variables: 2, -3, 3.29,.. x, book, car, a, q Operators: +, -, %, *, / q Parentheses ( ) q Expression Example: o 1- (x+10) 1 and 10 are literals, x is a variable, - and + are operators, ( ) are parentheses. o ((book+2)*5) 2 and 5 are literals, book is a variable, + and * are operators, ( ) are parentheses.
Arithmetic Operators: Ø / * % Ø + - q There are Two Types of Operations: o o Integer operations: Integer and integer 10/3 = 3 floating-point operations: floating-point and floating-point 10.0/3.0=3.3333 floating-point and integer 10.0/3 =3.3333 Integer and floating-point 10 /3.0=3.3333
Arithmetic Operators example
Relational Operators Math Symbol Java Notation Description = == Equal to!= Not equal to < < Less than <= Less than or equal > > Greater than >= Greater than or equal ² They are also known as Comparison operators. ² The Comparison operators give a boolean result of true or false. ² Examples: 3 > 1 true 3 == 8 false 3 >= 2 true 3!= 8 true 3 >= 3 true 3 <= 1 false
Boolean Operators: à OR && à AND X Y X Y X Y X && Y true true true true true true true false true true false false false true true false true false false false false false false false Ex: int x =3; (x>2) &&(x<10) = T && T = T Ex: int x =3; (x<2) (x>7) = F F = F Ex: int x =1; (x>2) &&(x<=6) = F && T = F Ex: int x =2; (x>3) (x< 5) = F T = T
Precedence Rule: Level 1 parentheses ( ) Level 2 % / * Level 3 + - Level 4 From left to right, for same level operations Ex: 3. + 2* 3.5 +4 = 3.+7.+4 = 10.+4 = 14.0 Ex: (3. + 2)* (3.5 *4) = 5.*14. = 70.0 Ex: (3+2)*3.5+4 = 5*3.5+4 = 17.5+4 = 21.5 Ex: 3.0+2.*(3.5+4) = 3.0+ 2.* 7.5 = 3.0+ 15. = 18.0 Ex: 3.0+2.*3.5+4 = 3.0+7.0+4 = 10+4 = 14.0 Ex: (10%3+4)-(4%3) = (1+4)-(1) = 5 1 = 4
Increment and Decrement q Postfix version (var++, var--): use value of var in expression, then increment or decrement. Ø ++ increment by 1 Ø -- decrement by 1 Ø y = x++ ; means assign the value of x to y, then increment x by 1 ; It is true for x--. q Example: Ø count++; is meaning count = count + 1; Ø count--; is meaning count = count - 1;
Increment and Decrement q Prefix version (++var, --var): use value of var in expression, then increment or decrement. When used in prefix mode, it increments the operand and evaluates to the incremented value of that operand. Ø ++ increment by 1 Ø -- decrement by 1 Ø y = ++x ; means increment x by 1, then assign this new value of x to y ; It is true for --x.
Increment
Decrement
Increment & Decrement
Increment (prefix & postfix)
Input in Java: Scanner class q Scanner class : Is a special class, which makes it easy for user to write a program that obtains information that is typed in at the keyboard. q Firstly, import Scanner package (import java.util.scanner) q Secondly, Create a Scanner object Ø Scanner keyboard = new Scanner(System.in); q The Scanner class has several input methods include: o nextint(), is used to input the value of an integer, o nextdouble(), is used to input the value of an double o next(), is used to input a string (word), o nextline(), is used to input a sentence o nextboolean(), return boolean data type
Example import java.util.scanner; public class add2numbers { public static void main( String[] args ) { Scanner input = new Scanner( System.in ); int x; int y; int sum; System.out.print( "Enter first integer: " ); x = input.nextint(); System.out.print( "Enter second integer: " ); y = input.nextint(); sum = x + y; System.out.printf( "The Resul is = "+ sum ); } } I / O Enter first integer: 2 Enter second integer: 4 The Resul is = 6
Example Q/ Write a Java program for determining average of four numbers? import java.util.scanner; public class Average4numbers { public static void main (String args[]) { Scanner input = new Scanner(System.in); System.out.print("Enter First Number is"); double a = input.nextdouble(); System.out.print("Enter Second Number"); double b = input.nextdouble(); System.out.print("Enter Third Number"); double c = input.nextdouble(); System.out.print("Enter Fourth Number"); double d = input.nextdouble(); double average; average = (a+b+c+d)/4 ; System.out.println("The Average is = average); } } I / O Enter First Number is 50 Enter Second Number is 60 Enter Third Number is 70 Enter Fourth Number is 80 The Average is = 65.0