1 / 32

Fundamentals 2

Fundamentals 2. Programs and Data. Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in the computer's memory: space memory . A space memory has three characteristics Identifier Data Type State. Identifier.

Télécharger la présentation

Fundamentals 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamentals 2

  2. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in the computer's memory: space memory. A space memory has three characteristics Identifier Data Type State

  3. Identifier • is a sequence of characters that denotes the name of the space memory to be used. • This name is unique within a program. • Identifier Rules • It cannot begin with a digit (0 – 9). • It may contain the letters a to z, A to Z, the digits 0 to 9, and the underscore symbol, _. • No spaces or punctuation, except the underscore symbol, _, are allowed.  Identifiers in Java are case-sensitive. Thus, the identifiers myNumber and mynumber, are seen as two different identifiers by the compiler.

  4. State • My be changed variable • All lowercase. • Capitalizing the first letter of each word in a multiword identifier, except for the first word. • Cannot be changed  constant • All uppercase, separating words within a multiword identifier with the underscore symbol, _.

  5. Data Type • The data type defines what kinds of values a space memory is allowed to store. • All values stored in the same space memory should be of the same data type. • All constants and variables used in a Java program must be defined prior to their use in the program.

  6. Java built-in Data Types

  7. Primitive Data Types

  8. Variable/Constant Declaration • When the declaration is made, memory space is allocated to store the values of the declared variable or constant. • The declaration of a variable means allocating a space memory which state (value) may change. • The declaration of a constant means allocating a space memory which state (value) cannot change.

  9. Constant Declaration final dataType constIdentifier = literal | expression; final int MAX = 1024; final int MIN = 128; final int AVG = (MAX + MIN) / 2; These are called literals. This is called expression.

  10. Variable Declaration • A variable may be declared: • With initial value. • Without initial value. • Variable declaration with initial value; dataType variableIdentifier = literal | expression; double avg = 0.0;int i = 1; int x =5, y = 7, z = (x+y)*3; • Variable declaration without initial value; dataType variableIdentifier; double avg;int i;

  11. More declaration examples • String declaration • with initial value: • String word="Apple"; • without initial value: • String word; • char declaration • with initial value: • char symbol ='*'; • without initial value: • char symbol; • Boolean declaration: • with initial value: • boolean flag=false; • boolean valid=true; • without initial value: • boolean flag;

  12. Exercises • A-Declare a variable of double type with initial value=0.0; • B- Declare a constant of String type with initial value=“Good” • C- Declare a variable of type string with initial value equals to the value of constant in B. • D-Is the following names are valid , why? • Student name • 1course • course*name

  13. Operators • Operators are special symbols used for: • mathematical functions • assignment statements • logical comparisons • Examples of operators: • 3 + 5 // uses + operator • 14 + 5 – 4 * (5 – 3) // uses +, -, * operators • Expressions: can be combinations of variables and operators that result in a value Dr. S. GANNOUNI & Dr. A. TOUIR

  14. Groups of Operators • There are 5 different groups of operators: • Arithmetic Operators • Assignment Operator • Increment / Decrement Operators • Relational Operators • Logical Operators Dr. S. GANNOUNI & Dr. A. TOUIR

  15. Addition + Subtraction – Multiplication  Division / Remainder (modulus ) % Assignment Operator = Java Arithmetic Operators Dr. S. GANNOUNI & Dr. A. TOUIR

  16. Arithmetic Operators • The following table summarizes the arithmetic operators available in Java. This is an integer division where the fractional part is truncated. Dr. S. GANNOUNI & Dr. A. TOUIR

  17. Example Exampleof division issues: 10 / 3 gives 3 10.0 / 3 gives 3.33333 • As we can see, • if we divide two integers we get an integer result. • if one or both operands is a floating-point value we get a floating-point result. Dr. S. GANNOUNI & Dr. A. TOUIR

  18. Modulus • Generates the remainder when you divide two integer values. • 5%3 gives 2 5%4 gives 1 • 5%5 gives0 5%10 gives 5 • Modulus operator is most commonly used with integer operands. If we attempt to use the modulus operator on floating-point values we will garbage! Dr. S. GANNOUNI & Dr. A. TOUIR

  19. Example: Sum of two integer public class Sum { // main method public static void main( String args[] ){ int a, b, sum; a = 20; b = 10; sum = a + b; System.out.println(a + ” + ” + b + “ = “ + sum); } // end main } // end class Sum Dr. S. GANNOUNI & Dr. A. TOUIR

  20. Java allows combining arithmetic and assignment operators into a single operator: Addition/assignment += Subtraction/assignment = Multiplication/assignment = Division/assignment /= Remainder/assignment %= Arithmetic/Assignment Operators Dr. S. GANNOUNI & Dr. A. TOUIR

  21. Increment/Decrement Operators Only use ++ or  when a variable is being incremented/decremented as a statement by itself. x++; is equivalent to x = x+1; x--; is equivalent to x = x-1; Dr. S. GANNOUNI & Dr. A. TOUIR

  22. Relational Operators • Relational operators compare two values • They Produce a boolean value (true or false) depending on the relationship Dr. S. GANNOUNI & Dr. A. TOUIR

  23. Example • int x = 3; • int y = 5; • boolean result; result = (x > y); • now result is assigned the value false because 3 is not greater than 5 Dr. S. GANNOUNI & Dr. A. TOUIR

  24. Logical Operators Symbol Name && AND || OR ! NOT Dr. S. GANNOUNI & Dr. A. TOUIR

  25. Example boolean x = true; boolean y = false; boolean result; result = (x && y); result is assigned the value false result = ((x || y) && x); (x || y) evaluates to true (true && x) evaluates to true result is then assigned the value true Dr. S. GANNOUNI & Dr. A. TOUIR

  26. Operators Precedence Dr. S. GANNOUNI & Dr. A. TOUIR

  27. Standard Output Window • Using System.out, we can output multiple lines of text to the standard output window. • The exact style of standard output window depends on the Java tool you use. Dr. S. GANNOUNI & Dr. A. TOUIR

  28. int x = 123, y = x + x; System.out.print( " x = “ ); System.out.println( x ); System.out.print( " x + x = “ ); System.out.println( y ); System.out.println( " THE END“ ); The println Method • We use println instead of print to skip a line. x = 123 x + x = 246 THE END Dr. S. GANNOUNI & Dr. A. TOUIR

  29. Standard Input • To input primitive data values, we use the Scanner class. • 4 steps are needed to be able to use input primitive: • Step 1: import the Scanner class: • import Java.util.Scanner; • Step 2 : declaring a reference variable of aScanner • Scanner read ; //we named the object read • Step 3: creating an instance of the Scanner • read = new Scanner (System.in); • Step 4: use specific methods to enter data • int x = read.nextInt(); Dr. S. GANNOUNI & Dr. A. TOUIR

  30. Example import java.util.Scanner; publicclass TestInput { publicstaticvoid main(String[] args) { Scanner input ; int area ,length, width; input = new Scanner (System.in); // creating an instance System.out.println("enter the length "); length = input.nextInt(); //reading the length from the keyboard System.out.println("Enter the Width "); width = input.nextInt(); //reading the width from the keyboard area = length * width ; System.out.println("the length is "+ length); System.out.println("the width is "+ width); System.out.println("the area is "+ area); } } Dr. S. GANNOUNI & Dr. A. TOUIR

  31. Output enter the length 2 Enter the Width 3 the length is 2 the width is 3 the area is 6 Dr. S. GANNOUNI & Dr. A. TOUIR

  32. Common Scanner Methods • Method Example Scanner input = new Scanner (System.in); nextByte( ) byte b = input.nextByte( ); nextDouble( ) double d = input.nextDouble( ); nextFloat( ) float f = input.nextFloat( ); nextInt( ) int i = input.nextInt( ); nextLong( ) long l = input.nextLong( ); nextShort( ) short s = input.nextShort( ); next() String str = input.next(); Dr. S. GANNOUNI & Dr. A. TOUIR

More Related