computer programming 12 mr. jean april 24, 2014. the plan: video clip of the day upcoming quiz...

24
Computer Programming 12 Mr. Jean April 24, 2014

Upload: abel-horton

Post on 03-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Computer Programming 12

Mr. Jean

April 24, 2014

The plan:

• Video clip of the day

• Upcoming Quiz

• Sample arrays

• Using arrays

• More about arrays

Java Quiz:

• Questions about – Types of variables

• Int, double, spring

– Basic Math operations• + - * / %

– Order of operations in Java– True Tables– Loops

• For & While

– Arrays

Arrays:

• Arrays are a contiguous section of memory occupied by the same type of data. This data is accessed by adding an offset (the index) to the address of the first element in the array.

Declaring Arrays:

• They are declared as follows:

<type>[] <var name> = new <type>[];

2D Arrays:

• If you want a multi-dimensional array (e.g. a 2D array has rows and columns) you need the same number of pairs of brackets as the number of dimensions. So that 2D array would be:

<type>[][] <var name> = new <type>[][];

Sample Code:

• public static void main(String[] args) {    int[] numbers = new int[5];    for (int i = 0; i < numbers.length; i++)         numbers[i] = 1;

    for (int i : numbers)         System.out.println(i);}

Sample Arrays:

2D – Arrays:

• type array_name = new type[rows][cols];

• Example Code:

int multidim[] = new int[3][];

• In a two-dimensional array,– You need to allocate memory for only the first dimension. – You can allocate the remaining dimensions separately.

• When you allocate memory to the second dimension, you can also allocate different number to each dimension.

Accessing Arrays

• You need to access various elements of an array to assign, retrieve, and manipulate the values stored in the array.

• Assigning values to the Elements of an Array– To access a specific array:

• You need to specify the name of the array and the index number of the element.

• The index position of the first element in the array is 0.

Accessing Array:

Accessing Various Elements:

User Input into Array:

Outputting Array Variables:

Sample Code with a “For” loop with regards to { & } logic sets. Do not forget Java defaults.

for(int x=0;x<=9;x++) {System.out.print(" ");numbers[x]=scan.nextDouble();

}

Continue to work on programs

• Now you should have everything you need to complete the last question on JAVA.

User Input into Array:

Outputting Array Variables:

Sample Code with a “For” loop with regards to { & } logic sets. Do not forget Java defaults.

for(int x=0;x<=9;x++) {System.out.print(" ");numbers[x]=scan.nextDouble();

}