Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Size: px
Start display at page:

Download "Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal"

Transcription

1 Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

2 Lesson Goals Review what an array is Review how to declare arrays Review what reference variables are Review how to pass arrays to methods Review the enhanced for statement Review basic sorting Review linear and binary searching Review multi-dimensional arrays 2

3 Fundamentals (1) An array is an object that is used to store a list of values. Each array is made out of a contiguous block of memory that is divided into a number of cells Each cell holds a value, and all the values are of the same type For any type (primitive or object) that exists you can have an array of that type Sometimes the cells of an array are called slots In the example array pictured at right, each cell holds an int The name of this array is data The cells are indexed 0 through 9 Each cell can be accessed by using its index. For example, data[0] is the cell which is indexed by zero (which contains the value 23). data[5] is the cell which is indexed by 5 (which contains the value 14) Note the use of square brackets, not parentheses While we say that data is the name of the array, it is also the name of the reference variable which refers to the beginning of the array. In other words, it contains an address 3

4 Fundamentals (2) The cells in an array are numbered sequentially starting at zero. This is called zero based indexing If there are N cells in an array, the indexes will be 0 through N- 1 Sometimes the index is called a subscript. The expression data[5] is usually pronounced "data sub-five The value stored in a cell of an array is sometimes called a value of an element of the array An array has a fixed number of cells, but the values placed in those cells (the elements) may change over time Note: There are other array like structures that can have a variable number of cells Every cell of an array holds a value of the same type. So, for example, you can have an array of type int, an array of type double, and so on. You can also have an array of objects, such as of type String 4

5 Reference Variables There are two types of variables in Java primitive and reference Remember, the 8 primitive variables are byte, short, int, long, double, float, boolean and char Reference variables hold memory addresses Declaring an array in Java creates a reference variable boolean[] truthful Instantiating the array creates an actual reference to a block of memory boolean [] truthful = new boolean [5]; truthful is a reference variable that holds an address, not a boolean value truthful[0] would hold a boolean value at the first address in the array The above example creates 5 boolean cells truthful[0] truthful[4] As we will see shortly, arrays and Strings are given some special properties in Java 5

6 Declaring Array Variables An array is a named sequence of contiguous memory locations capable of holding a collection of data of the same type In order to use arrays, you must learn the basic syntax You can declare an array reference variable in two ways: type[] name or type name[] The first format is more typically used Examples: int[] myarray - an array reference variable of type int called myarray double[] numbers an array reference variable of type double called numbers String [] args an array reference variable of type String called args 6

7 Array Instantiation There are several ways to instantiate an array. All require using the new operator Declare and instantiate in two separate statements type[] name; double[] earnings; name = new type[size] ; earnings = new double[6]; This is useful if you need a reference variable but don t need a new object, such as in a formal parameter Declare and instantiate in one statement type[] name = new type[size]; double[] earnings = new double[6]; Declare, instantiate and initialize in one statement type[] name = v 0, v 1, v 2 v n-1 ; double[] earnings = 25.2, 35.9, 72.6, 12.8, 176.4, 99.0; This example creates an array called earnings of length 6, and populates the cells 7

8 Array Instantiation Basic Rules The values in an array must all be of the same data type The first index in the array is 0 An array is referenced from name[0] name[length-1] Unlike simple variables, the cells of an array are always initialized to 0 by type this is different from primitive variables boolean arrays are initialized to false char initializes to the NUL character (2 bytes) which actually has the value 0 Any reference type initializes the cells to null different from the NUL character for instance an array of String values Until the array is instantiated as an object, it does not have a value unless you assign it the value null 8

9 Instant Check Write the statement(s) necessary to instantiate: - a boolean type honesty with size 7 - a short type days with size 365 9

10 Answers boolean [ ] honesty = new boolean[7]; short [] days = new short[365]; 10

11 Array Length The length of an array is how many cells it has. An array of length N has cells indexed 0..(N-1) The length is a variable associated with the object Example: int [] testarray = new int [10]; Then testarray.length equals 10 You get the length value with every array that is instantiated. It is an instance variable associated with the object If you change the reference to refer to a different array then the length will always be based on the referenced array object Note: There is also an Arrays class which has lots of methods for manipulating an array object. You can look at this but we won t be using these methods until we learn more about objects 11

12 Bounds Checking in Arrays Indexes must be of type int and greater than or equal to 0 and less than length The index cannot be of type long, although byte and short are okay since they cast up Therefore the maximum index of an array is 2,147,483,647 (the maximum value of an int) If an expression is always illegal it will be caught at compile time However, if an index is found to be illegal during execution an exception message will appear and the program is terminated 12

13 Bound Checking in Arrays - Examples Given: int[] data = new int[10]; Example Outcome data[ -1 ] always illegal index must be >= 0 data[ 10 ] data[ 1.5 ] data[ 0 ] data[ 9 ] data[ j ] illegal out of range (given the above declaration) always illegal must be an integer always OK OK (given the above declaration) can't tell (depends on the value of j) 13

14 Using an Expression as an Index class ArrayExample01 public static void main ( String[] args ) double[] val = new double[4]; val[0] = 0.12; val[1] = 1.43; val[2] = 2.98; int j = 3; System.out.printf("cell 4: %01.2f\n, val[ j ] ); System.out.printf("cell 3: %01.2f\n, val[ j-1 ] ); j = j-2; System.out.printf( "cell 2: %01.2f\n, val[ j ] ); System.out.printf( hash value of val is %H\n, val); 14

15 Copying Values from Cell to Cell versus Creating Two References to the Same Object public class ArrayExample02 public static void main ( String[] args ) int[] vala = 12, 23, 45, 56 ; int[] valb = new int[4]; //a new array valb[ 0 ] = vala[ 0 ] ; valb[ 1 ] = vala[ 1 ] ; valb[ 2 ] = vala[ 2 ] ; valb[ 3 ] = vala[ 3 ] ; int [ ] valc ; valc = valb; //This can be done since valc and valb are the same type of reference variables System.out.printf( Values in vala are: ); for (int i=0; i<vala.length; i++) System.out.printf( %d, vala[i]); System.out.printf( \nvalues in valb are: ); for (int i=0; i<valb.length; i++) System.out.printf( %d, valb[i]); System.out.printf( \nvalues in valc are: ); for (int i=0; i<valc.length; i++) System.out.printf( %d, valc[i]); System.out.printf( \n ); System.out.printf( It is %b that (vala==valb)\n, (vala==valb\n )); //this is false System.out.printf( It is %b that (vala==valc)\n, (vala==valc)); //this is false System.out.printf( It is %b that( valb==valc)\n,(valb==valc ); //this is true System.out.printf( It is %b that ((vala[0] == valb[0]) && (vala[0] == valc[0]) && (valb[0] == valc[0]))\n, ((vala[0] == valb[0]) && (vala[0] == valc[0]) && (valb[0] == valc[0]))); //this is true 15

16 Visualizing the Previous Program vala 12 valb 12 valc Two references variable can refer to the same object If the object (cell element in an array) is changed under one reference variable, those values are the same values are used when the array is referred to via the second reference variable 16

17 Why Arrays? Using arrays allows us to reference large amounts of data by index. With a single name we can store and access a large number of elements, sorting, searching and organizing them For instance, instead of listing grade0, grade1, grade2, grade3, grade4,. You can just loop through an array called grade using grade[0], grade[1], grade[2], grade[3], grade[4],... With arrays and looping we have combined two powerful weapons of programming 17

18 Enhanced for Looping With Arrays (1) The enhanced for is a mechanism for easily passing through an entire array, or other array like structure public class EnhancedFor public static void main(string ] args) int [] numbers = 10, 20, 30, 40, 50; for(int x : numbers ) // replaces for (int i=0; i < numbers.length; i++) System.out.printf( %d, ", x); replaces System.out.print(number[i] ); System.out.printf( \n ); //Prints out 10, 20, 30, 40, 50 18

19 Enhanced For Looping With Arrays (2) The format is: for (parameter : arrayname) statement 1; statement n; The parameter takes on the value in the array during each iteration and therefore must be of the same data type (primitive or reference) This means that the first time through the loop parameter = arrayname[0], second time through parameter = arrayname[1] etc. 19

20 Using Arrays - Populating an Array public class ArrayDemo public static void main(string[] args) int[] anarray; // DECLARE a reference for an array of integers anarray = new int[10]; // CREATE an array of integers // assign a value to each array element for (int i = 0; i < anarray.length; i++) anarray[i] = i; // print a value from each array element for (int value : anarray) //using the enhanced for System.out.printf( %d, value); System.out.printf( \n ); 20

21 Using Arrays - Populating an Array with a RunTime Array Length import java.util.scanner; public class ArrayDemo2 public static void main(string[] args) int size; int[] anarray; // DECLARE a reference for an array of integers Scanner keyboard = new Scanner(System.in); System.out.printf("Input size of the array: "); size= keyboard.nextint(); anarray = new int[size]; // CREATE an array of integers // assign a value to each array element for (int i = 0; i < anarray.length; i++) anarray[i] = i; // print a value from each array element for (int value : anarray) //using enhanced for System.out.printf( %d, 2*value); System.out.printf( \n ); Deciding the size of the array in real-time allows the program to be more efficient and flexible it is data driven rather than hard-coded There are other types of array like objects that allow for more dynamic lists also known as queues 21

22 Using Arrays - Finding The Average of the Numbers in an Array import java.util.scanner; public class ArrayAverage public static void main(string[] args) int size, sum; double average; int[] anarray; // DECLARE an array of integers Scanner keyboard = new Scanner(System.in); System.out.printf("Input size of the array: "); size= keyboard.nextint(); anarray = new int[size]; // CREATE an array of integers dynamically //read in an array for (int i=0; i < anarray.length; i++) System.out.printf("Enter integer value for anarray[%d]:, i); anarray[i] = keyboard.nextint(); sum= 0; for (int value : anarray) //using enhanced for sum = sum + value; average = (double)(sum)/anarray.length; System.out.printf( \nthe average value of the array is: %01.2f\n, average); 22

23 Using Arrays - Finding The Maximum Number in an Array import java.util.scanner; public class ArrayMax public static void main(string[] args) int size, maxvalue; Scanner keyboard = new Scanner(System.in); System.out.printf("Input size of the array: "); size= keyboard.nextint(); int[] anarray = new int[size]; // DECLARE and CREATE an array of integers dynamically //read in an array for (int i=0; i < anarray.length; i++) System.out.printf("Enter integer value for anarray[%d]:, i); anarray[i] = keyboard.nextint(); maxvalue= anarray[0]; for (int i = 1; i < anarray.length; i++) if (anarray[i] > maxvalue) maxvalue = anarray[i]; System.out.printf("The maximum value of the array is: %d\n, maxvalue); Now write a program to find the minimum value in an array. Print out that value and the cell it is located in. Then write a program that sums the numbers in an array of type double. 23

24 Arrays and Methods(1) Passing an Array Reference to a Method An array reference can be passed as a parameter to a method This allows all the current values of the array to be obtained for use by the method Example: Method Header: public static void swap(int[] xlist, int i, int j) In the body of main: int[] list = 1,3,5,7,9; swap(list, 2, 4) sends the reference to the array list to xlist, as well as the values 2 and 4 to i and j respectively Within the method xlist can reference 5 successive integers, the same as those in list. It is referring to the same place. Therefore it can change that array. Even though the reference variable xlist only exists in the swap method, the changes made to the array are persistent and visible in the calling method, since it is referencing to the same array. 24

25 Arrays and Methods(2) Passing an Array Reference to a Method //Swaps second and fourth elements of the array public class ArraySwapper public static void swap(int[] xlist, int i, int j) int temp = xlist[i]; xlist[i] = xlist[j]; xlist[j] = temp; public static void main(string[] args) int [] list = 1,3,5,7,9; System.out.printf("Before: "); for (int i=0; i<list.length; i++) System.out.printf( %d, list[i]); swap(list,2,4); System.out.printf("\nAfter: "); for (int i=0; i<list.length; i++) System.out.printf( %d, list[i]); System.out.printf( \n ); 25

26 Arrays and Methods (3) Passing an Array Reference from a Method The value of the method can be a reference to an array created in the method If the method returns a reference then it must return the reference to an array of like type. Why??? However, you don t need to create a new array, just declare the reference variable in the calling routine that accepts the reference. There is only one array, not two. 26

27 Arrays and Methods (4) Passing an Array Reference from a Method public class ArrayPassingBack public static double[] returnarray( ) double[] xarray; //creating a reference variable xarray = new double[3]; // Create an array of 3 elements xarray [0] = 2.3; xarray [1] = 3.4; xarray [2] = 4.5; return (xarray ); // Return the **reference** (location) of the array public static void main (String[] args) double [ ] anarray; /here anarray.length does not exist anarray = returnarray(); for (int i=0; i < anarray.length; i++) // now a.length does exist System.out.printf( %01.2f\n, anarray[i]); System.out.printf( \n ); 27

28 Visualizing the Previous Program Passing Back An Array Reference xarray anarray The array xarray is first created in the returnarray method A reference to the array xarray is passed back to the calling program. The xarray variable is destroyed, but the object remains and is accessible in main through the reference variable anarray 28

29 Sorting Sorting is a method for reorganizing a list from top to bottom or bottom to top There are many different algorithms for sorting In order to search a list it is often easier to first sort it A random list can only be searched directly by going through the list one element at a time unless you sort it first Alphabetizing is also a type of sorting that we will learn after we study the String class We will look at a modified insertion method of sorting first and look at others, including the selection sort, in the homework 29

30 How Does The Insertion Algorithm Work For each element in the array starting in index 1 and going through all the elements read into the array For each element to it s left counting down in the indices to 0 If the element is lower than the element to its left Swap the elements 30

31 Step By Step Output For The Insertion Algorithm Input number of integers to sort 6 Enter 6 integers Ø Ø Ø Ø Ø Ø Ø Ø

32 Using Arrays For Sorting Insertion Method import java.util.scanner; public class InsertionArraySorter public static void main(string []args) int size, swap; Scanner keyboard = new Scanner(System.in); System.out.printf("Input number of integers to sort: "); size = keyboard.nextint(); int array[] = new int[size]; System.out.printf("Enter %d integers:, size); for (int c = 0; c < size; c++) array[c] = keyboard.nextint(); for (int c = 0; c < ( size - 1 ); c++) for (int d = 0; d < size - c - 1; d++) if (array[d] > array[d+1]) /* For descending order use < */ swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; System.out.printf("Sorted list of numbers is:\n"); for (int k = 0; k < size; k++) System.out.printf( %d\n, array[k]); System.out.printf( \n ); 32

33 Using Arrays Sorting A Closer Look At The Algorithm Assume the list is array[0] =15 array[1] = 14 array[2] = 13 array[3] = 12 after first round we have after second round after third round c d array[d] array[d+1] swap What we are doing is getting the largest number in the last cell in the first round, and the next largest in the next to last cell in the second round, etc. 33

34 Using Arrays - Search Linear Search Linear search is a method for searching one by one through a list until you find a match It is somewhat laborious For larger lists it becomes very inefficient Review ArrayLinearSearch.java for example Binary Search Requires that the list is sorted you can sort the list first Simply keep dividing the list in half until you find the number Review ArrayBinarySearch.java 34

35 Pseudocode For A Binary Search Read in the number of elements Read in the elements in ascending order (or you could sort the list first) Read in value to find Set a variable for the bottom index of the search (first) to 0 Set a variable for the top index of the search (last) equal to the array size -1 Set the middle index (middle) to (first + last)/2 While (first <= last) if the array[middle] is less than the search Raise the first index to one more than the middle else if the search is at the middle index else if first > last Print out that result is found and at which index and exit Decrease the last index to one less than the middle Set middle to (first + last)/2 Print out that value is not found 35

36 Efficiency of Binary Search n Average linear = n/2 Average binary = (log 2 n) -1 4 (2 2 ) (2 4 ) (2 10 ) (2 20 )

37 Two-Dimensional Arrays A two dimensional array is like a table or a matrix It has rows and columns In Java, both the rows and columns are indexed starting at zero A two-dimensional array is actually an array of arrays As with one-dimensional arrays all values of the array must be of the same type The reference is always first by row and then column - array [row][column] array [0][0] array [0][1] array [0][2] array [0][3] array [1][0] array [1][1] array [1][2] array [1][3] array [2][0] array [2][1] array [2][2] array [2][3] array [3][0] array [3][1] array [3][2] array [3][3] array [4][0] array [4][1] array [4][2] array [4][3] 37

38 Declaring Two-Dimensional Arrays (1) Both the two-step and one step methods are available Two-steps: int[][] table; // declares the variable table = new int[2][3]; //instantiates the array One step: int[][] table = new int[2][3]; It can be processed in for loops by row or by column Either way it looks like this, with all values initialized to 0:

39 Declaring Two-Dimensional Arrays (2) You can also explicitly declare a matrix by rows: int[][] gradetable = 99, 42, 74, 83, 100, 90, 91, 72, 88, 95, 88, 61, 74, 89, 96, 61, 89, 82, 98, 93, 93, 73, 75, 78, 99, 50, 65, 92, 87, 94, 43, 98, 78, 56, 99 ; row Col

40 Simple Example of a Useful Table Student Week The table has 7 rows and 5 columns the headers are not part of this table 40

41 Length In A Two-Dimensional Array The length of a two-dimensional array is its number of rows arrayx.length The number of columns in a row is arrayx[row].length The length of any row in a regular two-dimensional array is the same from row to row In the previous example gradetable.length = 7 and gradetable[i].length = 5 for any value of I from 0 to 6 41

42 Finding the Number of Rows and Columns of Arrays With Unequal Length Ragged Arrays It is possible that the rows in an array can have different numbers of columns The length of a two-dimensional array is its number of rows arrayx.length The number of columns in a row is arrayx[row].length In matrix below: arrayx.length = 5 arrayx[0].length = 4; arrayx[1].length = 3; arrayx[2].length = 4; arrayx[3].length = 2; arrayx[4].length = 4; arrayx [0][0] arrayx [0][1] arrayx [0][2] arrayx [0][3] arrayx [1][0] arrayx [1][1] arrayx [1][2] arrayx [2][0] arrayx [2][1] arrayx [2][2] arrayx [2][3] arrayx [3][0] arrayx [3][1] arrayx [4][0] arrayx [4][1] arrayx [4][2] arrayx [4][3] 42

43 Declaring An Array With Unequal Column Entries int [][] arrayz = new int [4][]; arrayz[0] = new int[5]; arrayz[1] = new int[1]; arrayz[2] = new int[2]; arrayz[3] = new int[4]; Remember We are declaring an Array of Arrays 43

44 Picturing Instantiation Of A Ragged Array ArrayZ arrayz[0] arrayz[1] 0 arrayz[2] 0 0 arrayz[3] ArrayZ[0] through arrayz[3] are reference variables, each of which refers to a row of the array 44

45 Declaring Two-Dimensional Arrays With Uneven Numbers of Columns during Population You can also explicitly declare a matrix by rows: int[][] gradetable = 99, 42, 74, 90, 91, 72, 88, 95, 88, 61, 89, 82, 98, 93, 73, 75, 78, 99, 50, 65, 92, 87, 94, 43, 98 ; Using this method you can create rows with uneven numbers of entries (advanced subject) gradetable.length = 7 gradetable[0].length = 3 gradetable[1].length = 5 gradetable[2].length = 1 gradetable[3].length = 4 gradetable[4].length = 5 gradetable[5].length = 5 gradetable[6].length = 2 row Col

46 Printing a Two-Dimensional Array class ArrayPrint2D public static void main( String[] args ) // declare and construct a 2D array int[][] myarray = 1, 9, 4, 6, 0, 2,4,5, 0, 1, 2, 3 ; // print out the array for ( int row=0; row < myarray.length; row++ ) for ( int col=0; col < myarray[row].length; col++ ) System.out.printf( %4d, myarray[row][col]); System.out.printf( \d"); 46

47 Programming Exercises 1 ArrayData Write two methods that read data from the console and store the data in an array: a. The method int readdata(int[] x) reads a list of at most 100 integers into the array x. A sentinel, -999, terminates the list. The method returns the size of the list. b. The method int[] readdata() reads and returns a reference to a list of integers. The list is preceded by the number of the items in the list. For example, the data indicates that there are six items in the list. The leading 6 is not included in the list. Test both these methods within a single program that includes a method: void printlist(int [] x, int n) that displays x[0] through x[n-1] 47

48 Programming Exercises 2 MaxSort Implement a method void maxsort(int[] x, int size) // that sorts the partially filled array x. The method maxsort( ) first determines the largest value in x and swaps that value with x[size-1]; then maxsort( ) finds the next largest value and swaps that value with x[size- 2], and so on. Include an auxiliary method int max(int[] x, int i) that returns the index of the largest element between x[0] and x[i] inclusive. Test your methods in a program. 48

49 MaxSort_v2 Output Please enter the size of the array: 6 Enter up to 6 integers, terminating the list with See how the maximum value is successively moved to the rightmost place The ordered list is

50 Programming Exercises 3 PrintArrays Implement a method readarr that accepts a reference to a two dimensional integer array and reads in values for all the cells of the array Implement a method writearr that accepts a reference to a two dimensional array and prints out all the cells of the array row by row. Implement a main method that creates 2 twodimensional arrays, by reading in the number of rows and columns and creating the array. For each array call the two methods defined above. 50

51 Programming Exercises 4 LargestAndSmallest Design a method that determines the largest and smallest values stored in an integer array x. Your method should return these values in an array of length 2. Use the following algorithms: Initialize variables currentbig and currentsmall to the larger and smaller values of x[0] and x[1]. Process the rest of the list two elements at a time (optional). Compare the larger of the two elements to currentbig and replace currentbig if necessary. Compare the smaller of the two elements to currentsmall and replace currentsmall if necessary. Test your method in a program and include a method that reads a list, terminated by -999, into an array. 51

52 Programming Exercises 5 MinimumSort (using Selection Sort) Implement a method void minsort(int[ ] x, int size) // (size <= x.length) that sorts the partially filled array x. The method selectionsort( ) first determines the smallest value in x and swaps that value with x[0]; then selectionsort( ) finds the next smallest value and swaps that with x[1] and so on. Include an auxiliary method int min(int[ ] x, int i, int size) that returns the index of the smallest element between x[i] and x[size-1], inclusive. Test your methods in a program. 52

53 MinimumSort_v2 Output Please enter the size of the array: 6 Enter up to 6 integers, terminating the list with The ordered list is

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof Array Lecture 12 Based on Slides of Dr. Norazah Yusof 1 Introducing Arrays Array is a data structure that represents a collection of the same types of data. In Java, array is an object that can store a

More information

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd El-Shorouk Academy Acad. Year : 2013 / 2014 High Institute of Computer Science & Information Technology Term : 1 st Year : 2 nd Computer Science Department Object Oriented Programming Section (1) Arrays

More information

Last Class. While loops Infinite loops Loop counters Iterations

Last Class. While loops Infinite loops Loop counters Iterations Last Class While loops Infinite loops Loop counters Iterations public class January31{ public static void main(string[] args) { while (true) { forloops(); if (checkclassunderstands() ) { break; } teacharrays();

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Object Oriented Programming. Java-Lecture 6 - Arrays

Object Oriented Programming. Java-Lecture 6 - Arrays Object Oriented Programming Java-Lecture 6 - Arrays Arrays Arrays are data structures consisting of related data items of the same type In Java arrays are objects -> they are considered reference types

More information

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of

More information

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier Lesson 4 Arrays and Lists Review CSC 123 Fall 2018 Answer Sheet Short Answers 1. In an array declaration, this indicates the number of elements that the array will have. b a. subscript b. size declarator

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Example: Computing prime numbers

Example: Computing prime numbers Example: Computing prime numbers -Write a program that lists all of the prime numbers from 1 to 10,000. Remember a prime number is a # that is divisible only by 1 and itself Suggestion: It probably will

More information

Last Class. More on loops break continue A bit on arrays

Last Class. More on loops break continue A bit on arrays Last Class More on loops break continue A bit on arrays public class February2{ public static void main(string[] args) { String[] allsubjects = { ReviewArray, Example + arrays, obo errors, 2darrays };

More information

Declaring Array Variable

Declaring Array Variable 5. Arrays Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in Phone: +91-9999-283-283 Array Arrays are used typically

More information

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA. Arrays Defining arrays, declaration and initialization of arrays Introduction Many applications require the processing of multiple data items that have common characteristics (e.g., a set of numerical

More information

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016 Your Name: Exam 2. CSC 121 MW Class Lecturer: Howard Rosenthal April 25, 2016 The following questions (or parts of questions) in numbers 1-7 are all worth 3 points each. 1. Answer the following as true

More information

C++ PROGRAMMING SKILLS Part 4: Arrays

C++ PROGRAMMING SKILLS Part 4: Arrays C++ PROGRAMMING SKILLS Part 4: Arrays Outline Introduction to Arrays Declaring and Initializing Arrays Examples Using Arrays Sorting Arrays: Bubble Sort Passing Arrays to Functions Computing Mean, Median

More information

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017 Your Name: Exam 2. CSC 121 MW Class Lecturer: Howard Rosenthal April 26, 2017 The following questions (or parts of questions) in numbers 1-7 are all worth 3 points each. 1. Answer the following as true

More information

Arrays. Eng. Mohammed Abdualal

Arrays. Eng. Mohammed Abdualal Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 9 Arrays

More information

Programming with Java

Programming with Java 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

More information

Chapter 7: Arrays and the ArrayList Class

Chapter 7: Arrays and the ArrayList Class Chapter 7: Arrays and the ArrayList Class Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 7 discusses the following main topics: Introduction

More information

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

Methods CSC 121 Fall 2016 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal Methods CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

Instructor: Eng.Omar Al-Nahal

Instructor: Eng.Omar Al-Nahal Faculty of Engineering & Information Technology Software Engineering Department Computer Science [2] Lab 6: Introduction in arrays Declaring and Creating Arrays Multidimensional Arrays Instructor: Eng.Omar

More information

All answers will be posted on web site, and most will be reviewed in class.

All answers will be posted on web site, and most will be reviewed in class. Lesson 4 Arrays and Lists Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required.

More information

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it Last Class Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it public class February4{ public static void main(string[] args) { String[]

More information

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals Arrays Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals Can we solve this problem? Consider the following program

More information

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops) Arrays Chapter 7 (Done right after 4 arrays and loops go together, especially for loops) Object Quick Primer A large subset of Java s features are for OOP Object- Oriented Programming We ll get to that

More information

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15 Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

More information

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

Methods CSC 121 Spring 2017 Howard Rosenthal

Methods CSC 121 Spring 2017 Howard Rosenthal Methods CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) GRADED! Section 1: Monday, January 22 nd Assignment 2 (LECTURE 8) GRADED! Section

More information

Principles of Programming. Chapter 6: Arrays

Principles of Programming. Chapter 6: Arrays Chapter 6: Arrays In this chapter, you will learn about Introduction to Array Array declaration Array initialization Assigning values to array elements Reading values from array elements Simple Searching

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II 1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables

More information

Control Statements Review CSC 123 Fall 2018 Howard Rosenthal

Control Statements Review CSC 123 Fall 2018 Howard Rosenthal Control Statements Review CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Review some of Java s control structures Review how to control the flow of a program via selection mechanisms Understand if, if-else,

More information

Computer Programming: C++

Computer Programming: C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming: C++ Experiment #7 Arrays Part II Passing Array to a Function

More information

Computer Science is...

Computer Science is... Computer Science is... Machine Learning Machine learning is the study of computer algorithms that improve automatically through experience. Example: develop adaptive strategies for the control of epileptic

More information

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017 Your Name: Final Exam. CSC 121 Fall 2017 Lecturer: Howard Rosenthal Dec. 13, 2017 The following questions (or parts of questions) in numbers 1-17 are all worth 2 points each. The programs have indicated

More information

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) Chapter - Arrays 1.1 Introduction 2.1 Introduction.2 Arrays.3 Declaring Arrays. Examples Using Arrays.5 Passing Arrays to Functions.6 Sorting Arrays. Case Study: Computing Mean, Median and Mode Using Arrays.8

More information

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types Chapter 4 - Arrays 1 4.1 Introduction 4.2 Arrays 4.3 Declaring Arrays 4.4 Examples Using Arrays 4.5 Passing Arrays to Functions 4.6 Sorting Arrays 4.7 Case Study: Computing Mean, Median and Mode Using

More information

Getting started with Java

Getting started with Java Getting started with Java by Vlad Costel Ungureanu for Learn Stuff Programming Languages A programming language is a formal constructed language designed to communicate instructions to a machine, particularly

More information

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same

More information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Understand Object Oriented Programming The Syntax of Class Definitions Constructors this Object Oriented "Hello

More information

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal The ArrayList class CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Describe the ArrayList class Discuss important methods of this class Describe how it can be used in modeling Much of the information

More information

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty! Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns

More information

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Data Structures Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous

More information

Lecture #8-10 Arrays

Lecture #8-10 Arrays Lecture #8-10 Arrays 1. Array data structure designed to store a fixed-size sequential collection of elements of the same type collection of variables of the same type 2. Array Declarations Creates a Storage

More information

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays A First Book of ANSI C Fourth Edition Chapter 8 Arrays Objectives One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study: Computing Averages and Standard Deviations Two-Dimensional

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) Name: Write all of your responses on these exam pages. 1 Short Answer (10 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss how Java accomplishes this task. 2.

More information

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables 1 6 C Arrays 6.2 Arrays 2 Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name + position number arrayname[ position number ] First element at position

More information

System.out.print("Enter sales for salesperson " + i + ": "); sales[i] = scan.nextint();

System.out.print(Enter sales for salesperson  + i + : ); sales[i] = scan.nextint(); Tracking Sales File Sales.java contains a Java program that prompts for and reads in the sales for each of 5 salespeople in a company. It then prints out the id and amount of sales for each salesperson

More information

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS Computers process information and usually they need to process masses of information. In previous chapters we have studied programs that contain a few variables

More information

Array. Array Declaration:

Array. Array Declaration: Array Arrays are continuous memory locations having fixed size. Where we require storing multiple data elements under single name, there we can use arrays. Arrays are homogenous in nature. It means and

More information

New Concepts. Lab 7 Using Arrays, an Introduction

New Concepts. Lab 7 Using Arrays, an Introduction Lab 7 Using Arrays, an Introduction New Concepts Grading: This lab requires the use of the grading sheet for responses that must be checked by your instructor (marked as Question) AND the submission of

More information

13 th Windsor Regional Secondary School Computer Programming Competition

13 th Windsor Regional Secondary School Computer Programming Competition SCHOOL OF COMPUTER SCIENCE 13 th Windsor Regional Secondary School Computer Programming Competition Hosted by The School of Computer Science, University of Windsor WORKSHOP I [ Overview of the Java/Eclipse

More information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Lesson 7 Part 2 Flags

Lesson 7 Part 2 Flags Lesson 7 Part 2 Flags A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays WIT COMP1000 Arrays Arrays An array is a list of variables of the same type, that represents a set of related values For example, say you need to keep track of the cost of 1000 items You could declare

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Lesson 06 Arrays MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Array An array is a group of variables (called elements or components) containing

More information

Introduction to Java & Fundamental Data Types

Introduction to Java & Fundamental Data Types Introduction to Java & Fundamental Data Types LECTURER: ATHENA TOUMBOURI How to Create a New Java Project in Eclipse Eclipse is one of the most popular development environments for Java, as it contains

More information

Introduction to Software Development (ISD) Week 3

Introduction to Software Development (ISD) Week 3 Introduction to Software Development (ISD) Week 3 Autumn term 2012 Aims of Week 3 To learn about while, for, and do loops To understand and use nested loops To implement programs that read and process

More information

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 7/e This chapter serves as an introduction to data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C s notion of

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Arrays A data structure for a collection of data that is all of the same data type. The data type can be

More information

St. Edmund Preparatory High School Brooklyn, NY

St. Edmund Preparatory High School Brooklyn, NY AP Computer Science Mr. A. Pinnavaia Summer Assignment St. Edmund Preparatory High School Name: I know it has been about 7 months since you last thought about programming. It s ok. I wouldn t want to think

More information

PLD Semester Exam Study Guide Dec. 2018

PLD Semester Exam Study Guide Dec. 2018 Covers material from Chapters 1-8. Semester Exam will be built from these questions and answers, though they will be re-ordered and re-numbered and possibly worded slightly differently than on this study

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative

More information

Array Basics: Outline

Array Basics: Outline Arrays Chapter 7 Array Basics: Outline Creating and Accessing Arrays Array Details The Instance Variable length More About Array Indices Partially-filled Arrays Working with Arrays Creating and Accessing

More information

Chapter 9 Introduction to Arrays. Fundamentals of Java

Chapter 9 Introduction to Arrays. Fundamentals of Java Chapter 9 Introduction to Arrays Objectives Write programs that handle collections of similar items. Declare array variables and instantiate array objects. Manipulate arrays with loops, including the enhanced

More information

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class CS0007: Introduction to Computer Programming Review General Form of a switch statement: switch (SwitchExpression) { case CaseExpression1:

More information

Procedural Programming

Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Procedural Programming Session Five: Arrays Name: First Name: Tutor: Matriculation-Number: Group-Number: Date: Prof. Dr.Ing. Axel Hunger Dipl.-Ing.

More information

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I AL GHURAIR UNIVERSITY College of Computing CSC 209 JAVA I week 2- Arithmetic and Decision Making: Equality and Relational Operators Objectives: To use arithmetic operators. The precedence of arithmetic

More information

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points. Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points. Use the remaining question as extra credit (worth 1/2 of the points earned). Specify which question is

More information

Fall 2017 CISC124 9/16/2017

Fall 2017 CISC124 9/16/2017 CISC124 Labs start this week in JEFF 155: Meet your TA. Check out the course web site, if you have not already done so. Watch lecture videos if you need to review anything we have already done. Problems

More information

20 Standard API: Math: round() 21 Salary analysis 22 Standard API: System: out.printf(): string item 24 Standard API: System: out.printf(): fixed text

20 Standard API: Math: round() 21 Salary analysis 22 Standard API: System: out.printf(): string item 24 Standard API: System: out.printf(): fixed text List of Slides 1 Title 2 Chapter 14: Arrays 3 Chapter aims 4 Section 2: Example:Salary analysis 5 Aim 6 Salary analysis 7 Salary analysis 8 Array 10 Type: array type 11 Salary analysis 12 Variable: of

More information

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

More information

Review of Important Topics in CS1600. Functions Arrays C-strings

Review of Important Topics in CS1600. Functions Arrays C-strings Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why

More information

Manipulating One-dimensional Arrays

Manipulating One-dimensional Arrays Manipulating One-dimensional Arrays Mitsu Ogihara Department of Computer Science University of Miami 1 / 30 Table of Contents 1 For each 2 Exchanging Values 3 Reversing 2 / 30 For-each iteration For enumerating

More information

Arrays. CSE 142, Summer 2002 Computer Programming 1.

Arrays. CSE 142, Summer 2002 Computer Programming 1. Arrays CSE 142, Summer 2002 Computer Programming 1 http://www.cs.washington.edu/education/courses/142/02su/ 5-Aug-2002 cse142-16-arrays 2002 University of Washington 1 Reading Readings and References»

More information

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017 Your Name: Final Exam. CSC 121 Fall 2017 Lecturer: Howard Rosenthal Dec. 13, 2017 The following questions (or parts of questions) in numbers 1-17 are all worth 2 points each. The programs have indicated

More information

Data dependent execution order data dependent control flow

Data dependent execution order data dependent control flow Chapter 5 Data dependent execution order data dependent control flow The method of an object processes data using statements, e.g., for assignment of values to variables and for in- and output. The execution

More information

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers; Arrays What are they? An array is a data structure that holds a number of related variables. Thus, an array has a size which is the number of variables it can store. All of these variables must be of the

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 7 Arrays reading: 7.1 2 Can we solve this problem? Consider the following program (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp:

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

More Arrays. Last updated 2/6/19

More Arrays. Last updated 2/6/19 More Last updated 2/6/19 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16 3 1 4 rows x 5 columns 2 tj 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16

More information

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Chapter 6 SINGLE-DIMENSIONAL ARRAYS Chapter 6 SINGLE-DIMENSIONAL ARRAYS Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk What is an Array? A single array variable can reference

More information

Search,Sort,Recursion

Search,Sort,Recursion Search,Sort,Recursion Searching, Sorting and Recursion Searching Linear Search Inserting into an Array Deleting from an Array Selection Sort Bubble Sort Binary Search Recursive Binary Search Searching

More information

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen) Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen) 1 Lesson 9: Introduction Objectives: To Arrays Write programs that handle collections of similar items. Declare

More information

C Arrays Pearson Education, Inc. All rights reserved.

C Arrays Pearson Education, Inc. All rights reserved. 1 6 C Arrays 2 Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end:

More information

Exam 2. CSC 121 TTH Class. Lecturer: Howard Rosenthal. April 26, 2016

Exam 2. CSC 121 TTH Class. Lecturer: Howard Rosenthal. April 26, 2016 Your Name: Exam 2. CSC 121 TTH Class Lecturer: Howard Rosenthal April 26, 2016 The following questions (or parts of questions) in numbers 1-7 are all worth 3 points each. 1. Answer the following as true

More information

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section: 7 Arrays Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end: then

More information

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz The in-class quiz is intended to give you a taste of the midterm, give you some early feedback about

More information