Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lecture02.pdf

Avatar for William Albritton William Albritton
August 06, 2014
190

 lecture02.pdf

Avatar for William Albritton

William Albritton

August 06, 2014
Tweet

Transcript

  1. Memory Upload • Primitive data types & objects • Declare

    (make space for) arrays • Instantiate (create) arrays • Initialize (give values to) arrays • Out of Bounds Error (remember that computers start counting at zero) • jGRASP IDE and example programs
  2. Java Language Basics • You should already know the Java

    language basics  Objects, primitive data types, conditional statements, loops, methods, arrays, exceptions, file I/O, classes, inheritance, and polymorphism
  3. Please Review! • Don’t have to know every single detail,

    but you should have a general understanding • If your Java is rusty, please review the basics  For example, a seven year gap
  4. Primitives vs. Objects • How do our programs store data?

    • The Java language uses both objects and primitive data types to store data  Primitive data types are the basic way of storing data (building blocks)  Objects are composed of primitive data types
  5. Primitives & Java Language 1. Primitive data types are a

    part of the Java language  When folks at Sun Microsystems made the Java language, primitive data types were an essential component  Note that Java is now owned by Oracle
  6. Objects & Java Language 1. The blueprint for objects are

    the classes from Java API (Application Programming Interface), so objects are not part of the Java language  See the link from the class web site to the Java API
  7. Creating Primitives 2. Create a primitive data type by using

    name of the primitive data type int number = 1024;
  8. Using Primitives 3. Use operators to manipulate data for primitive

    data types  For example, asterisk (*) is the multiplication operator number = number * 2;
  9. Using Objects 3. Use methods to access and manipulate data

    for objects  For example, the substring method String name = new String("Nami"); String firstLetter; firstLetter = name.substring(0,1); //firstLetter is character "N"
  10. Remember! firstLetter = name.substring(0,1); • REMEMBER: Computers are a bit

    funky! • They start counting at zero (0), NOT one (1) •Robot sports teams often shout “We’re number 0!”
  11. Variables & Primitives 4. A variable for a primitive data

    type contains the primitive data type’s data  Visually represented by a box containing data int number = 1024; number 1024
  12. Variables & Objects 4. A variable for an object contains

    an address that points to the object’s data  Visually represented by a box pointing to the data String name = new String("Nami"); name Nami
  13. Wrapper Classes • Wrapper classes are the corresponding classes for

    the eight (8) primitive data types • Often wrapper classes are used to store data in a data structure
  14. Primitives & Wrappers • Primitive data type Wrapper class 1.

    byte Byte 2. short Short 3. int Integer 4. long Long 5. float Float 6. double Double 7. char Character 8. boolean Boolean
  15. Autoboxing • Autoboxing automatically converts from a primitive data type

    to a wrapper object int w = 1024; Integer x = w; //autoboxing w 1024 x 1024
  16. Unboxing • Unboxing automatically converts from a wrapper object to

    a primitive data type Integer y = new Integer(108); int z = y; //unboxing y 108 x 108
  17. Arrays • An array is an example of a very

    simple data structure  A data structure is a way to organize and store data
  18. Arrays • An array is a numbered list of data

    of the same type  Numbers from 0, 1, 2, 3, … • Easy way to store lots of data
  19. Terminology • Array index (subscript)  A number corresponding to

    the position of an element in an array • Numbered from 0, 1, 2, . . . N-1, where N is the size of the array
  20. Array of Integers examScores 0 80 1 77 2 92

    3 55 4 80 5 95 index address element
  21. Array of Doubles finalGrades 0 83.33 1 73.56 2 91.29

    3 55.55 4 67.02 5 83.33 index address element
  22. Declaring Arrays • Syntax for an array declaration  DataType

    [] arrayName; • Example Java code  Integer [] examScores;  Double [] finalGrades;
  23. Declaring Arrays • A declaration creates a variable that is

    used to store the address to an array, but not the array itself  Since arrays are objects in Java, you must declare an array variable (which will later contain the address to the location of the array object)
  24. Declaring Arrays • These declarations create a reference variable with

    a null value (does not point to an object)  Integer [] examScores; examScores null  Double [] finalGrades; finalGrades null
  25. Instantiating Arrays • Syntax for array instantiation (creation)  arrayName

    = new DataType[SIZE]; • SIZE is the number of elements in the array
  26. Instantiating Arrays • Example Java code  final Integer SIZE

    = 6;  examScores = new Integer[SIZE];  finalGrades = new Double[SIZE];
  27. Instantiating Arrays • Instantiating an array creates an array object

    & stores the array object’s address in a variable • The array object itself will contain no addresses, because elements have not been assigned to the array yet
  28. Example Array Instantiation • Array of integers examScores 0 null

    1 null 2 null 3 null index 4 null address 5 null • Array of doubles finalGrades 0 null 1 null 2 null 3 null 4 null 5 null
  29. Initializing Arrays • Syntax for initializing arrays  arrayName[i] =

    value; • Where i is the index of an element in the array • Example Java code  examScores[0] = 80;  examScores[1] = 77;  examScores[2] = 92;
  30. Initializer List • You can also declare, instantiate, & initialize

    an array with one statement • Syntax  DataType [] arrayName = {value0, value1, value2, . . ., valueN-1}; • The size of the array is automatically calculated
  31. Initializer List • Example Java code  Double [] finalGrades

    = {83.33, 73.56, 91.29, 55.55, 67.02,83.33}; • Double precision floating point is a format for decimal number by the IEEE (Institute of Electrical and Electronics Engineers)
  32. Array Initialization examScores 0 80 1 77 2 92 3

    55 4 80 5 95 index address element
  33. Array Initialization finalGrades 0 83.33 1 73.56 2 91.29 3

    55.55 4 67.02 5 83.33 index address element
  34. Array Length • Size of an array is stored in

    a public variable called length  length is not a method!  Bug: array.length()  Useful in loops for(int i=0;i<examScores.length;i++){ System.out.print(examScores[i]+ ", "); }
  35. Out of Bounds Error • What if an array index

    is greater or less than range of the array?  An exception will be thrown & the program will end
  36. Out of Bounds Error • Example Java code for(int i=0;i<=examScores.length;

    i++){ System.out.print(examScores[i]); } • Program will end and display a message about an ArrayIndexOutOfBoundsException • This is bad
  37. Out of Bounds Error • Since arrays start their index

    numbering at zero (0), a common bug is to be “off by one”  Range for arrays is 0 to SIZE-1 (length-1)
  38. Example Code • See InitializingArrays.java  This program shows how

    to • Declare arrays • Instantiate arrays • Initialize arrays • Display arrays
  39. Three Steps • How are we going to run this

    program!? 1.Edit a program with an editor  Write program in Java language & store on computer • Creates Program.java (text file)
  40. Three Steps 2. Compile a program with a compiler 

    Check for errors & create Java bytecode • Creates Program.class (bytecode file)
  41. Three Steps 3. Run (execute) a program with an interpreter

     Translate each bytecode to machine code & implement machine code • Creates screen output
  42. Software Installation • See the ICS 211 class webpage for

    links to sites to download the software  JDK (Java Development Kit) • Used to compile programs • Also includes JRE (Java Runtime Environment) which is the interpreter used to run programs
  43. Software Installation • We also need an IDE (Integrated Development

    Environment) to edit, compile and run programs  jGRASP (Graphical Representations of Algorithms, Structures and Processes)
  44. Software Installation  jGRASP (Graphical Representations of Algorithms, Structures and

    Processes) • Used in education & research • Made at Auburn University in Alabama! • Always use jGRASP for assignments in ICS 211
  45. Java Coding Standard • In ICS 211, we have a

    Java Coding Standard that should be followed when writing programs  The main purpose is to make sure the programs are neat and easy to understand • See the “links” column of the class webpage
  46. Java Coding Standard • Basic Coding Rules to Follow 1.

    Use descriptive and appropriate names for all identifiers (variables, method names, class names, constants, etc.) 2. Comment every 3-7 lines of code 3. Be neat
  47. Example Code II • See ArraysLoopsModulus.java  This program shows

    how to • Initialize an array with a loop • Examples of integer division and modulus  Modulus is the remainder of a division • For example: 10/7 = 1 remainder 3 • 27/5 = 5 remainder 2
  48. Memory Defragmenter • Primitive data types & objects • Declare

    (make space for) arrays • Instantiate (create) arrays • Initialize (give values to) arrays • Out of Bounds Error (remember that computers start counting at zero) • jGRASP IDE and example programs
  49. Task Manager • Before the next class, you need to:

    1.Do the assignment corresponding to this lecture 2.Download necessary software 3.Email me any questions you may have about the material 4.Turn in the assignment before the due date