lecture3 - CS116 OBJECTORIENTED PROGRAMMINGII LECTURE3...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 116 OBJECT ORIENTED  PROGRAMMING II LECTURE 3 GEORGE KOUTSOGIANNAKIS Copyright:  Spring 2010- Illinois Institute of Technology/George Koutsogiannakis 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
OOP Review In previous lecture we discussed: How to read text files with scanner object. The scope of a variable. Formatting of Numbers 2
Background image of page 2
Arrays – Single Dimension Topics  in Today’s Lecture Review Material Declaring and Instantiating Arrays Entering Array Elements Accessing Array Elements Aggregate Array Operations Arrays of Objects. Using Arrays in classes.  New Material not covered in CS115 Creating packages Creating Documentation with javadocs command 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Arrays An   array  is a sequence of variables of the same data type. Arrays are useful for many applications, including calculating  statistics or representing the state of a game.  The data type can be any of Java's primitive types ( int, short, byte,  long, float, double, boolean, char ), an array, or a class.  Each variable in the array is an  element . We use an  index  to specify the position of each element in the  array. An array is an Object by itself. Remember that we can also have arrays of objects.  The objects can be representative of pre defined  classes (library classes) or user defined classes 4
Background image of page 4
Declaring and Instantiating  Arrays Arrays are objects, so creating an array requires two steps: 1. declaring a reference to the array 2. instantiating the array To declare a reference to the array, use this syntax: datatype [] arrayName; To instantiate an array, use this syntax: arrayName = new datatype[ size ]; where size is an expression that evaluates to an integer and specifies the number of elements. 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Examples Declaring arrays: double [] dailyTemps; // elements are doubles String [] cdTracks; // elements are Strings boolean [] answers; // elements are booleans Auto [] cars; // elements are Auto references int [] cs101, bio201; // two integer arrays Instantiating these arrays: dailyTemps = new double[365]; // 365 elements cdTracks = new String[15]; // 15 elements int numberOfQuestions = 30; answers = new boolean[numberOfQuestions]; cars = new Auto[3]; // 3 elements cs101 = new int[5]; // 5 elements bio201 = new int[4]; // 4 elements 6
Background image of page 6
Default Values for Elements When an array is instantiated, the elements are assigned  default values according to the array data type. 7 Array data type Default value byte, short, int, long 0 float, double 0.0 char space boolean false Any object reference (for example, a String ) null
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example array of integers int [ ] myarray=new int[5]; The array size is 5 and the index will run from 0 to 4. index
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 39

lecture3 - CS116 OBJECTORIENTED PROGRAMMINGII LECTURE3...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online