Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Size: px
Start display at page:

Download "Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved."

Transcription

1 Chapter 6 Single-Dimensional Arrays rights reserved. 1 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average. Solution AnalyzeNumbers rights reserved. 2

2 Objectives To describe why arrays are necessary in programming ( 7.1). To declare array reference variables and create arrays ( ). To obtain array size using arrayrefvar.length and know default values in an array ( 7.2.3). To access array elements using indexes ( 7.2.4). To declare, create, and initialize an array using an array initializer ( 7.2.5). To program common array operations (displaying arrays, summing all elements, finding the minimum and maximum elements, random shuffling, and shifting elements) ( 7.2.6). To simplify programming using the foreach loops ( 7.2.7). To apply arrays in application development (AnalyzeNumbers, DeckOfCards) ( ). To copy contents from one array to another ( 7.5). To develop and invoke methods with array arguments and return values ( ). To define a method with a variable-length argument ( 7.9). To search elements using the linear ( ) or binary ( ) search algorithm. To sort an array using the selection sort approach ( 7.11). To use the methods in the java.util.arrays class ( 7.12). To pass arguments to the main method from the command line ( 7.13). rights reserved. 3 Introducing Arrays Array is a data structure that represents a collection of the same types of data. rights reserved. 4

3 Declaring Array Variables datatype[] arrayrefvar; Example: double[] mylist; datatype arrayrefvar[]; // This style is allowed, but not preferred Example: double mylist[]; rights reserved. 5 Creating Arrays arrayrefvar = new datatype[arraysize]; Example: mylist = new double[10]; mylist[0] references the first element in the array. mylist[9] references the last element in the array. rights reserved. 6

4 Declaring and Creating in One Step datatype[] arrayrefvar = new datatype[arraysize]; double[] mylist = new double[10]; datatype arrayrefvar[] = new datatype[arraysize]; double mylist[] = new double[10]; rights reserved. 7 The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayrefvar.length For example, mylist.length returns 10 rights reserved. 8

5 Default Values When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types, '\u0000' for char types, and false for boolean types. rights reserved. 9 Indexed Variables The array elements are accessed through the index. The array indices are 0-based, i.e., it starts from 0 to arrayrefvar.length-1. In the example in Figure 6.1, mylist holds ten double values and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: arrayrefvar[index]; rights reserved. 10

6 Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in mylist[0] and mylist[1] to mylist[2]. mylist[2] = mylist[0] + mylist[1]; rights reserved. 11 Array Initializers Declaring, creating, initializing in one step: double[] mylist = {1.9, 2.9, 3.4, 3.5; This shorthand syntax must be in one statement. rights reserved. 12

7 Declaring, creating, initializing Using the Shorthand Notation double[] mylist = {1.9, 2.9, 3.4, 3.5; This shorthand notation is equivalent to the following statements: double[] mylist = new double[4]; mylist[0] = 1.9; mylist[1] = 2.9; mylist[2] = 3.4; mylist[3] = 3.5; rights reserved. 13 CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double[] mylist; mylist = {1.9, 2.9, 3.4, 3.5; rights reserved. 14

8 Trace Program with Arrays Declare array variable values, create an array, and assign its reference to values public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the array is created rights reserved. 15 Trace Program with Arrays i becomes 1 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the array is created rights reserved. 16

9 Trace Program with Arrays i (=1) is less than 5 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the array is created rights reserved. 17 Trace Program with Arrays After this line is executed, value[1] is 1 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the first iteration rights reserved. 18

10 Trace Program with Arrays After i++, i becomes 2 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the first iteration rights reserved. 19 Trace Program with Arrays public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; i (= 2) is less than 5 After the first iteration rights reserved. 20

11 Trace Program with Arrays After this line is executed, values[2] is 3 (2 + 1) public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the second iteration rights reserved. 21 Trace Program with Arrays After this, i becomes 3. public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the second iteration rights reserved. 22

12 Trace Program with Arrays i (=3) is still less than 5. public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the second iteration rights reserved. 23 Trace Program with Arrays After this line, values[3] becomes 6 (3 + 3) public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the third iteration rights reserved. 24

13 Trace Program with Arrays After this, i becomes 4 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the third iteration rights reserved. 25 Trace Program with Arrays i (=4) is still less than 5 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the third iteration rights reserved. 26

14 Trace Program with Arrays After this, values[4] becomes 10 (4 + 6) public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the fourth iteration rights reserved. 27 Trace Program with Arrays After i++, i becomes 5 public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the fourth iteration rights reserved. 28

15 Trace Program with Arrays i ( =5) < 5 is false. Exit the loop public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; After the fourth iteration rights reserved. 29 Trace Program with Arrays After this line, values[0] is 11 (1 + 10) public class Test { public static void main(string[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; values[0] = values[1] + values[4]; rights reserved. 30

16 Processing Arrays See the examples in the text. 1. (Initializing arrays with input values) 2. (Initializing arrays with random values) 3. (Printing arrays) 4. (Summing all elements) 5. (Finding the largest element) 6. (Finding the smallest index of the largest element) 7. (Random shuffling) 8. (Shifting elements) rights reserved. 31 Initializing arrays with input values java.util.scanner input = new java.util.scanner(system.in); System.out.print("Enter " + mylist.length + " values: "); for (int i = 0; i < mylist.length; i++) mylist[i] = input.nextdouble(); rights reserved. 32

17 Initializing arrays with random values for (int i = 0; i < mylist.length; i++) { mylist[i] = Math.random() * 100; rights reserved. 33 Printing arrays for (int i = 0; i < mylist.length; i++) { System.out.print(myList[i] + " "); rights reserved. 34

18 Summing all elements double total = 0; for (int i = 0; i < mylist.length; i++) { total += mylist[i]; rights reserved. 35 Finding the largest element double max = mylist[0]; for (int i = 1; i < mylist.length; i++) { if (mylist[i] > max) max = mylist[i]; rights reserved. 36

19 Random shuffling you need to randomly reorder the elements in an array. This is called a shuffling. To accomplish this, for each element mylist[i], randomly generate an index j and swap mylist[i] with mylist[j], as follows: for (int i = 0; i < mylist.length - 1; i++) { // Generate an index j randomly int j = (int)(math.random() * mylist.length); // Swap mylist[i] with mylist[j] double temp = mylist[i]; mylist[i] = mylist[j]; mylist[j] = temp; A random index i mylist [0] [1] [i] [j].... swap rights reserved. 37 Shifting Elements Sometimes you need to shift the elements left or right. Here is an example to shift the elements one position to the left and fill the last element with the first element: rights reserved. 38

20 Enhanced for Loop (for-each loop) JDK 1.5 introduced a new for loop that enables you to traverse the complete array sequentially without using an index variable. For example, the following code displays all elements in the array mylist: for (double value: mylist) System.out.println(value); In general, the syntax is for (elementtype value: arrayrefvar) { // Process the value You still have to use an index variable if you wish to traverse the array in a different order or change the elements in the array. rights reserved. 39 Analyze Numbers Read one hundred numbers, compute their average, and find out how many numbers are above the average. AnalyzeNumbers Run rights reserved. 40

21 Copying Arrays Often, in a program, you need to duplicate an array or a part of an array. In such cases you could attempt to use the assignment statement (=), as follows: 2 = 1; rights reserved. 41 Copying Arrays This statement (2 = 1;) does not copy the contents of the array referenced by 1 to 2, But, copies the reference value from 1 to 2. After this statement, 1 and 2 reference to the same array. The array previously referenced by 2 is no longer referenced; it becomes garbage, which will be automatically collected by the Java Virtual Machine. rights reserved. 42

22 Copying Arrays In Java, you can use assignment statements to copy primitive data type variables, but not arrays. There are three ways to copy arrays: Use a loop to copy individual elements one by one. Use the static arraycopy method in the System class. Use the clone method to copy arrays; this will be introduced in Chapter 14, Abstract Classes and Interfaces. rights reserved. 43 Copying Arrays (Using a loop) int[] sourcearray = {2, 3, 1, 5, 10; int[] targetarray = new int[sourcearray.length]; for (int i = 0; i < sourcearrays.length; i++) targetarray[i] = sourcearray[i]; rights reserved. 44

23 The arraycopy Utility arraycopy(sourcearray, src_pos, targetarray, tar_pos, length); Example: System.arraycopy(sourceArray, 0, targetarray, 0, sourcearray.length); rights reserved. 45 Passing Arrays to Methods public static void printarray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); Invoke the method int[] = {3, 1, 2, 6, 4, 2; printarray(); Invoke the method printarray(new int[]{3, 1, 2, 6, 4, 2); Anonymous array rights reserved. 46

24 Pass By Value rights reserved. 47 Simple Example rights reserved. 48

25 Call Stack When invoking m(x, y), the values of x and y are passed to number and numbers. Since y contains the reference value to the array, numbers now contains the same reference value to the same array. rights reserved. 49 Call Stack When invoking m(x, y), the values of x and y are passed to number and numbers. Since y contains the reference value to the array, numbers now contains the same reference value to the same array. rights reserved. 50

26 Heap Heap Space required for the main method int[] y: reference int x: The arrays are stored in a heap. The JVM stores the array in an area of memory, called heap, which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order. rights reserved. 51 Passing Arrays as Arguments Objective: Demonstrate differences of passing primitive data type variables and array variables. TestPassArray Run rights reserved. 52

27 Example, cont. Stack Space required for the swap method n2: 2 n1: 1 Space required for the main method int[] a reference Invoke swap(int n1, int n2). The primitive type values in a[0] and a[1] are passed to the swap method. Heap a[1]: 2 a[0]: 1 The arrays are stored in a heap. Stack Space required for the swapfirsttwoinarray method int[] array reference Space required for the main method int[] a reference Invoke swapfirsttwoinarray(int[] array). The reference value in a is passed to the swapfirsttwoinarray method. rights reserved. 53 Returning an Array from a Method public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); rights reserved. 54

28 Trace the reverse Method int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; Declare and create array rights reserved. 55 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i = 0 and j = rights reserved. 56

29 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i (= 0) is less than rights reserved. 57 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i = 0 and j = 5 Assign [0] to [5] rights reserved. 58

30 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; After this, i becomes 1 and j becomes 4 i <.length; i++, j--) { [j] = [i]; rights reserved. 59 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i (=1) is less than 6 i <.length; i++, j--) { [j] = [i]; rights reserved. 60

31 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i = 1 and j = 4 Assign [1] to [4] rights reserved. 61 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; After this, i becomes 2 and j becomes rights reserved. 62

32 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i (=2) is still less than rights reserved. 63 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i = 2 and j = 3 Assign [i] to [j] rights reserved. 64

33 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; After this, i becomes 3 and j becomes rights reserved. 65 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i (=3) is still less than rights reserved. 66

34 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i = 3 and j = 2 Assign [i] to [j] rights reserved. 67 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; After this, i becomes 4 and j becomes rights reserved. 68

35 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i (=4) is still less than rights reserved. 69 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i = 4 and j = 1 Assign [i] to [j] rights reserved. 70

36 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; After this, i becomes 5 and j becomes rights reserved. 71 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i (=5) is still less than rights reserved. 72

37 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i = 5 and j = 0 Assign [i] to [j] rights reserved. 73 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; After this, i becomes 6 and j becomes rights reserved. 74

38 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; i (=6) < 6 is false. So exit the loop rights reserved. 75 Trace the reverse Method, cont. int[] 1 = {1, 2, 3, 4, 5, 6; int[] 2 = reverse(1); public static int[] reverse(int[] ) { int[] = new int[.length]; i <.length; i++, j--) { [j] = [i]; Return rights reserved. 76

39 Problem: Counting Occurrence of Each Letter Generate 100 lowercase letters randomly and assign to an array of characters. Count the occurrence of each letter in the array. (a) Executing createarray in Line 6 Stack Space required for the createarray method char[] chars: ref Space required for the main method char[] chars: ref Heap Array of 100 characters (b) After exiting createarray in Line 6 Stack Space required for the main method char[] chars: ref Heap Array of 100 characters CountLettersInArray Run rights reserved. 77 Problem: Calculator Objective: Write a program that will perform binary operations on integers. The program receives three parameters: an operator and two integers. java Calculator java Calculator 2-3 Calculator Run java Calculator 2 / 3 java Calculator 2. 3 rights reserved. 78

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average.

Opening Problem EXAMPLE. 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average. Chapter 6 Arrays 1 Opening Problem EXAMPLE 1. Read one hundred numbers, 2. compute their average, and 3. find out how many numbers are above the average. 2 Introducing Arrays Array is a data structure

More information

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. Chapter 6 Arrays 1 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average. 2 Solution AnalyzeNumbers Run Run with prepared input 3 Objectives

More information

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved 1 To describe why arrays are necessary in programming ( 6.1). To declare array reference variables and create arrays ( 6.2.1-6.2.2). To initialize the values in an array ( 6.2.3). To access array elements

More information

Module 7: Arrays (Single Dimensional)

Module 7: Arrays (Single Dimensional) Module 7: Arrays (Single Dimensional) Objectives To describe why arrays are necessary in programming ( 7.1). To declare array reference variables and create arrays ( 7.2.1 7.2.2). To obtain array size

More information

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Announcements PS 4 is ready, due next Thursday, 9:00pm Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Room TBD Scope: Lecture 1 to Lecture 9 (Chapters 1 to 6 of text) You may bring a sheet of paper (A4, both

More information

CS1150 Principles of Computer Science Arrays

CS1150 Principles of Computer Science Arrays CS1150 Principles of Computer Science Arrays Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Opening Problem Read one hundred numbers, compute their

More information

CS1150 Principles of Computer Science Arrays

CS1150 Principles of Computer Science Arrays CS1150 Principles of Computer Science Arrays Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Opening Problem Read one hundred numbers, compute their

More information

Chapter 7 Single-Dimensional Arrays

Chapter 7 Single-Dimensional Arrays Chapter 7 Single-Dimensional Arrays 1 Arrays Array is a data structure that represents a collection of same-types data elements. A single-dimensional array is one that stores data elements in one row.

More information

Chapter 7: Single-Dimensional Arrays. Declaring Array Variables. Creating Arrays. Declaring and Creating in One Step.

Chapter 7: Single-Dimensional Arrays. Declaring Array Variables. Creating Arrays. Declaring and Creating in One Step. Chapter 7: Single-Dimensional Arrays Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average CS1: Java Programming Colorado State University

More information

CS115 Principles of Computer Science

CS115 Principles of Computer Science CS5 Principles of Computer Science Chapter Arrays Prof. Joe X. Zhou Department of Computer Science CS5 Arrays. Re: Objectives in Methods To declare methods, invoke methods, and pass arguments to a method

More information

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsoned.co.uk Pearson Education Limited 2014

More information

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF - 1

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF -  1 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 06: Arrays Instructor: AbuKhleif, Mohammad Noor Sep 2017 AbuKhleiF - 1 Instructor AbuKhleif, Mohammad Noor Computer Engineer

More information

Java-Array. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables.

Java-Array. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables. -Array Java provides a data structure, the array, which stores 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

14. Array Basics. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

14. Array Basics. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 14. Array Basics Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Introduction Declaring an Array Creating Arrays Accessing an Array Simple Processing on Arrays Copying Arrays References Introduction

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

15. Arrays and Methods. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

15. Arrays and Methods. Java. Summer 2008 Instructor: Dr. Masoud Yaghini 15. Java Summer 2008 Instructor: Dr. Masoud Yaghini Outline Passing Arrays to Methods Returning an Array from a Method Variable-Length Argument Lists References Passing Arrays to Methods Passing Arrays

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

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

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

Passing Array to Methods

Passing Array to Methods Passing Array to Methods Lecture 13 Based on Slides of Dr. Norazah Yusof 1 Passing Array Elements to a Method When a single element of an array is passed to a method it is handled like any other variable.

More information

array Indexed same type

array Indexed same type ARRAYS Spring 2019 ARRAY BASICS An array is an indexed collection of data elements of the same type Indexed means that the elements are numbered (starting at 0) The restriction of the same type is important,

More information

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Chapter 7 Multidimensional Arrays rights reserved. 1 Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent

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

Chapter 8 Multi-Dimensional Arrays

Chapter 8 Multi-Dimensional Arrays Chapter 8 Multi-Dimensional Arrays 1 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections of elements. myarray: 6 4 1 9 7 3 2 8 Now think

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

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing Java Programming MSc Induction Tutorials 2011 Stefan Stafrace PhD Student Department of Computing s.stafrace@surrey.ac.uk 1 Tutorial Objectives This is an example based tutorial for students who want to

More information

Chapter 7 Multidimensional Arrays

Chapter 7 Multidimensional Arrays Chapter 7 Multidimensional Arrays 1 Motivations You can use a two-dimensional array to represent a matrix or a table. Distance Table (in miles) Chicago Boston New York Atlanta Miami Dallas Houston Chicago

More information

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved 1 Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a matrix or a table. For example, the following table that describes

More information

Computer programming Code exercises [1D Array]

Computer programming Code exercises [1D Array] Computer programming-140111014 Code exercises [1D Array] Ex#1: //Program to read five numbers, from user. public class ArrayInput public static void main(string[] args) Scanner input = new Scanner(System.in);

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

1 class Lecture5 { 2 3 "Arrays" 4. Zheng-Liang Lu Java Programming 136 / 174

1 class Lecture5 { 2 3 Arrays 4. Zheng-Liang Lu Java Programming 136 / 174 1 class Lecture5 { 2 3 "Arrays" 4 5 } Zheng-Liang Lu Java Programming 136 / 174 Arrays An array stores a large collection of data which is of the same type. 2 // assume the size variable exists above 3

More information

LAB 13: ARRAYS (ONE DIMINSION)

LAB 13: ARRAYS (ONE DIMINSION) Statement Purpose: The purpose of this Lab. is to practically familiarize student with the concept of array and related operations performed on array. Activity Outcomes: As a second Lab on Chapter 7, this

More information

Lecture 6 Sorting and Searching

Lecture 6 Sorting and Searching Lecture 6 Sorting and Searching Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101 There are many algorithms for sorting a list

More information

Java, Arrays and Functions Recap. CSE260, Computer Science B: Honors Stony Brook University

Java, Arrays and Functions Recap. CSE260, Computer Science B: Honors Stony Brook University Java, Arrays and Functions Recap CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 1 Objectives Refresh information from CSE160 2 3 How Data is Stored? What

More information

Top-down programming design

Top-down programming design 1 Top-down programming design Top-down design is a programming style, the mainstay of traditional procedural languages, in which design begins by specifying complex pieces and then dividing them into successively

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

Chapter 6 Single-dimensional Arrays

Chapter 6 Single-dimensional Arrays Chapter 6 Single-dimensional s 1. See the section "Declaring and Creating s." 2. You access an array using its index. 3. No memory is allocated when an array is declared. The memory is allocated when creating

More information

Recapitulate CSE160: Java basics, types, statements, arrays and methods

Recapitulate CSE160: Java basics, types, statements, arrays and methods Recapitulate CSE160: Java basics, types, statements, arrays and methods CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 1 Objectives Refresh information from

More information

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

1. Which of the following is the correct expression of character 4? a. 4 b. 4 c. '\0004' d. '4' Practice questions: 1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4' 2. Will System.out.println((char)4) display 4? a. Yes b. No 3. The expression "Java

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

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

SCALA ARRAYS. Following picture represents array mylist. Here, mylist holds ten double values and the indices are from 0 to 9.

SCALA ARRAYS. Following picture represents array mylist. Here, mylist holds ten double values and the indices are from 0 to 9. http://www.tutorialspoint.com/scala/scala_arrays.htm SCALA ARRAYS Copyright tutorialspoint.com Scala provides a data structure, the array, which stores a fixed-size sequential collection of elements of

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

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. Chapter 4 Loops 1 Motivations Suppose that you need to print a string (e.g., "Welcome to Java!") a hundred times. It would be tedious to have to write the following statement a hundred times: So, how do

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

CS1150 Principles of Computer Science Loops (Part II)

CS1150 Principles of Computer Science Loops (Part II) CS1150 Principles of Computer Science Loops (Part II) Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Review Is this an infinite loop? Why (not)?

More information

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming Lecture slides for: Chapter 6 Arrays Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 28. ISBN: 978-1-84448-933-2 http://www.ii.uib.no/~khalid/jac/

More information

Chapter 8 Multidimensional Arrays

Chapter 8 Multidimensional Arrays Chapter 8 Multidimensional Arrays 8.1 Introduction Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a matrix or a

More information

Example. Password generator

Example. Password generator Example Password generator Write a program which generates ten characters as a password. There may be lower-case letters, upper-case letters, and digital characters in the character sequence. Recall that

More information

Problems with simple variables

Problems with simple variables Problems with simple variables Hard to give up values high number of variables. Complex to sort a high number of variables by value. Impossible to use loops. Example problem: Read one hundred numbers,

More information

CS 171: Introduction to Computer Science II. Arrays. Li Xiong

CS 171: Introduction to Computer Science II. Arrays. Li Xiong CS 171: Introduction to Computer Science II Arrays Li Xiong 1 Fundamentals Roadmap Types, variables, assignments, expressions Control flow statements Methods Arrays and binary search algorithm Programming

More information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Department Lecture 3: C# language basics Lecture Contents 2 C# basics Conditions Loops Methods Arrays Dr. Amal Khalifa, Spr 2015 3 Conditions and

More information

Computer Programming, I. Laboratory Manual. Final Exam Solution

Computer Programming, I. Laboratory Manual. Final Exam Solution Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Final Exam Solution

More information

Variables and Java vs C++

Variables and Java vs C++ Variables and Java vs C++ 1 What can be improved? (variables) public void godirection(string directionname) { boolean wenttoroom = false; for (Direction direction : currentroom.getdirections()) { if (direction.getdirectionname().equalsignorecase(directionname))

More information

Introduction. Data in a table or a matrix can be represented using a two-dimensional array. For example:

Introduction. Data in a table or a matrix can be represented using a two-dimensional array. For example: Chapter 7 MULTIDIMENSIONAL ARRAYS Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk ١ Introduction Data in a table or a matrix can be

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. Data structures Collections of related data items. Discussed in depth in Chapters 16 21. Array objects Data

More information

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Announcements PS 3 is due Thursday, 10/6 Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Room TBD Scope: Lecture 1 to Lecture 9 (Chapters 1 to 6 of text) You may bring a sheet of paper (A4, both sides) Tutoring

More information

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Outline Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Variable Length Parameter Lists split() Method from String Class Integer & Double Wrapper

More information

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Chapter 6 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 A Solution int sum = 0; for (int i = 1; i

More information

How to swap values of two variables without tmp? However, this naive algorithm is biased. 1

How to swap values of two variables without tmp? However, this naive algorithm is biased. 1 Shuffling over array elements 1... 2 for (int i = 0; i < A.length; ++i) { 3 // choose j randomly 4 int j = (int) (Math.random() A.length); 5 // swap 6 int tmp = A[i]; 7 A[i] = A[j]; 8 A[j] = tmp; 9 } 10...

More information

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Chapter 6 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 A Solution int sum = 0; for (int i = 1; i

More information

Java Coding 3. Over & over again!

Java Coding 3. Over & over again! Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java

More information

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

More information

Chapter 1-9, 12-13, 18, 20, 23 Review Slides. What is a Computer?

Chapter 1-9, 12-13, 18, 20, 23 Review Slides. What is a Computer? Chapter 1-9, 12-13, 18, 20, 23 Review Slides CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox rights reserved. 1 What is a Computer? A computer

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

Spring 2010 Java Programming

Spring 2010 Java Programming Java Programming: Guided Learning with Early Objects Chapter 7 - Objectives Learn about arrays Explore how to declare and manipulate data in arrays Learn about the instance variable length Understand the

More information

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, Cloning Arrays In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, 1... 2 T[] A = {...}; // assume A is an array 3 T[] B = A;

More information

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays: College of Computer and Information Sciences CSC111 Computer Programming I Exercise 1: Tutorial 11 Arrays: A. Write a code snippet to define the following arrays: 1. An int array named nums of size 10.

More information

COE318 Lecture Notes Week 4 (Sept 26, 2011)

COE318 Lecture Notes Week 4 (Sept 26, 2011) COE318 Software Systems Lecture Notes: Week 4 1 of 11 COE318 Lecture Notes Week 4 (Sept 26, 2011) Topics Announcements Data types (cont.) Pass by value Arrays The + operator Strings Stack and Heap details

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

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

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University 9/5/6 CS Introduction to Computing II Wayne Snyder Department Boston University Today: Arrays (D and D) Methods Program structure Fields vs local variables Next time: Program structure continued: Classes

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

Arrays. Chapter The Java Array Object. Goals

Arrays. Chapter The Java Array Object. Goals Chapter 7 Arrays Goals This chapter introduces the Java array for storing collections of many objects. Individual elements are referenced with the Java subscript operator []. After studying this chapter

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

CS2401 QUIZ 5 February 26, questions / 20 points / 20 minutes NAME:..

CS2401 QUIZ 5 February 26, questions / 20 points / 20 minutes NAME:.. CS2401 QUIZ 5 February 26, 2014 18 questions / 20 points / 20 minutes NAME:.. Questions on Objects and Classes 1 An object is an instance of a. A. program B. class C. method D. data 2 is invoked to create

More information

Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types:

Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types: Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types: int, short, byte, long, float, double, boolean, char The data

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

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal 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

More information

Research Group. 3: Loops, Arrays

Research Group. 3: Loops, Arrays Research Group 3: Loops, Arrays Assignment 2 Foo Corporationneeds a program to calculate how much to pay theiremployees. 1. Pay = hours worked x base pay 2. Hours over 40 get paid 1.5 the base pay 3. The

More information

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, Cloning Arrays In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example, 1... 2 T[] A = {...}; // assume A is an array 3 T[] B = A;

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

Lecture 5: Methods CS2301

Lecture 5: Methods CS2301 Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int

More information

Chapter 12: Arrays Think Java: How to Think Like a Computer Scientist by Allen B. Downey

Chapter 12: Arrays Think Java: How to Think Like a Computer Scientist by Allen B. Downey Chapter 12: Arrays Think Java: How to Think Like a Computer Scientist 5.1.2 by Allen B. Downey Current Options for Storing Data 1) You need to store the room temperature, such as 62.5, in a variable. What

More information

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal APCS A Midterm Review You will have a copy of the one page Java Quick Reference sheet. This is the same reference that will be available to you when you take the AP Computer Science exam. 1. n bits can

More information

What is a Computer? Chapter 1-9, 12-13, 18, 20, 23 Review Slides

What is a Computer? Chapter 1-9, 12-13, 18, 20, 23 Review Slides Chapter 1-9, 12-13, 18, 20, 23 Review Slides What is a Computer? A computer consists of a CPU, memory, hard disk, floppy disk, monitor, printer, and communication devices. CS1: Java Programming Colorado

More information

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection

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

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

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

Exceptions, Templates, and the STL

Exceptions, Templates, and the STL Exceptions, Templates, and the STL CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 16 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/

More information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

Principles of Computer Science

Principles of Computer Science Principles of Computer Science Lecture 4 Dr. Horia V. Corcalciuc Horia Hulubei National Institute for R&D in Physics and Nuclear Engineering (IFIN-HH) February 10, 2016 Pointers: Assignment Pointer Assignment

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015 Arrays Lecture 11 CGS 3416 Fall 2015 October 26, 2015 Arrays Definition: An array is an indexed collection of data elements of the same type. Indexed means that the array elements are numbered (starting

More information

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc. Chapter 1 GETTING STARTED SYS-ED/ Computer Education Techniques, Inc. Objectives You will learn: Java platform. Applets and applications. Java programming language: facilities and foundation. Memory management

More information

AP Computer Science Lists The Array type

AP Computer Science Lists The Array type AP Computer Science Lists There are two types of Lists in Java that are commonly used: Arrays and ArrayLists. Both types of list structures allow a user to store ordered collections of data, but there

More information

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011 CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011 Date: 01/18/2011 (Due date: 01/20/2011) Name and ID (print): CHAPTER 6 USER-DEFINED FUNCTIONS I 1. The C++ function pow has parameters.

More information