COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Size: px
Start display at page:

Download "COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand"

Transcription

1 COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

2 COSC 236 Web Site You will always find the course material at: or or From this site you can click on the COSC-236 tab to download the PowerPoint lectures, the Quiz solutions and the Laboratory assignments. 2

3 3

4 Review of Quiz 17 PART 1: You were given the program and asked to modify it to read the file Quiz17.java You were to run the file and confirm the output as shown. You were encouraged to copy and paste the program from the Quiz 17 assignment sheet. 4

5 Review of Quiz 17 PART 1: Only one minor change required: Change "Test.txt" to "Quiz17.java" import java.io.*; // for File import java.util.*; // Java utilities public class FileRead { public static void main(string[] args) throws FileNotFoundException { File f = new File("Test.txt"); Scanner input = new Scanner(f); System.out.println("exists returns " + f.exists()); System.out.println("canRead returns " + f.canread()); System.out.println("length returns " + f.length()); System.out.println("getAbsolutePath returns " + f.getabsolutepath()); int count = 0; while (input.hasnext()) { String word = input.next(); count++; } System.out.println("total words = " + count); } } 5

6 Review of Quiz 17 PART 1: Only one minor change required: Change "Test.txt" to "Quiz17.java" import java.io.*; // for File import java.util.*; // Java utilities public class FileRead { public static void main(string[] args) throws FileNotFoundException { File f = new File("Test.txt"); File( Quiz17.java); Scanner input = new Scanner(f); System.out.println("exists returns " + f.exists()); System.out.println("canRead returns " + f.canread()); System.out.println("length returns " + f.length()); System.out.println("getAbsolutePath returns " + f.getabsolutepath()); int count = 0; while (input.hasnext()) { String word = input.next(); count++; } System.out.println("total words = " + count); } } 6

7 Review of Quiz 17 PART 2: Modify the program to print out any comment lines and exclude them from the count In class you were also told to add a comment at the beginning of the program: // Report some basic information about a file. 7

8 Review of Quiz 17 // Report some basic information about a file. import java.io.*; // for File import java.util.*; // Java utilities ADD Comment Change file name public class Quiz17 { public static void main(string[] args) throws FileNotFoundException { File f = new File("Quiz17.java"); Scanner input = new Scanner(f); System.out.println("exists returns " + f.exists()); System.out.println("canRead returns " + f.canread()); System.out.println("length returns " + f.length()); System.out.println("getAbsolutePath returns " + f.getabsolutepath()); 8

9 Review of Quiz 17 Set up counter for words } int count1 = 0; int count2 = 0; while (input.hasnext()) { String word = input.next(); if (word.equals("//")) { count2++; String temp = input.nextline(); Set up counter for comments Check token for comment System.out.println("Comment " + count2 + " = //" + " " + temp);} else {count1++;} } System.out.println("total words excluding comments = " + count1 + "\nthere are " + count2 + " comments."); } 9

10 } Review of Quiz 17 Increment comment counter int count1 = 0; int count2 = 0; while (input.hasnext()) { String word = input.next(); if (word.equals("//")) { count2++; String temp = input.nextline(); System.out.println("Comment " + count2 + " = //" + " " + temp);} The rest of the line is comment Print out comment number and comment If not a comment, increment word counter else {count1++;} } System.out.println("total words excluding comments = " + count1 + "\nthere are " + count2 + " comments."); } No change in the rest of the program 10

11 Chapter 7 Arrays (pp ) 7.1 Array Basics (Today s Lecture, pp ) Constructing and Traversing an Array Accessing an Array A Complete Array Program Random Access Arrays and Methods The For-Each Loop Initializing Arrays The Arrays Class 11

12 Chapter 7 Arrays (pp ) 7.2 Array-Traversal Algorithms (Lecture 19) Printing an Array Searching and Replacing Testing for Equality Reversing an Array String Traversal Algorithms 12

13 Chapter 7 Arrays (pp ) 7.3 Reference Semantics (Lecture 20) Multiple Objects 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array Arrays of Objects Command-Line Arguments Nested Loop Algorithms 13

14 Chapter 7 Arrays (pp ) 7.5 Multidimensional Arrays (Lecture 21) Rectangular Two-Dimensional Arrays Jagged Arrays 7.6 Case Study: Benford s Law (Lecture 21) Tallying Values Completing the Program 14

15 Introduction to Chapter 7 and Arrays The sequential nature of files severely limits the number of interesting things that you can do easily with them. The algorithms we have examined so far have all been sequential algorithms Sequential algorithms MUST be performed by examining each data item once, in sequence. An entirely different class of algorithms can be performed when you can access the data items multiple times and in an arbitrary order 15

16 Introduction to Chapter 7 and Arrays This chapter examines a new object called an array that provides this more flexible kind of access. The concept of arrays is not complex, but it can take a while for a novice to learn all of the different ways that an array can be used. The chapter begins with a general discussion of arrays The discussion then moves into a discussion of common array manipulations Finally advanced array techniques are covered. The chapter also includes a discussion of special rules known as reference semantics that apply only to objects like arrays strings 16

17 Constructing and Traversing an Array (pp ) Accessing an Array (pp ) A Complete Array Program (pp ) Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 17

18 Constructing and Traversing an Array (pp ) An array is a flexible structure that holds multiple values of the same type A String is a special case of an array that holds multiple values of type char Like Strings, each element of the array has an index Like Strings, the index is zero biased (that is the index starts at zero) 18

19 Constructing and Traversing an Array (pp ) Suppose you want to write a program that keeps track of the high temperature each day You could store each temperature in a separate variable: double temperature1; double temperature2; double temperature3; That works for three temperatures, but what if you have 100 temperatures to store! The solution to the problem is an array: double[] temperature = new double[100]; 19

20 Constructing and Traversing an Array (pp ) double[] temperature = new double[100]; The array object created sets up 100 temperatures with index running from 0 to 99 The array is auto-initialized (i.e.: automatically initialized) to all zeros: However, you can easily change any value you wish: temperature[0] = 74.3; temperature[1] = 68.4; temperature[2] = 70.3; NOTE: You do not have to change the values in order you can change any value you want! 20

21 Constructing and Traversing an Array (pp ) What the array is auto-initialized to depends on the type of the array: int[] is initialized to 0 double[] is initialized to 0.0 char[] is initialized to null boolean[] is initialized to false object[] is initialized to null 21

22 Constructing and Traversing an Array (pp ) Instead of setting array values in the program, we often read them in from the console or a file: temperature[0] = input.nextdouble(); temperature[1] = input.nextdouble(); temperature[2] = input.nextdouble(); For a large number of temperatures, this is more easily done in a loop: for (int i = 0; i < temperature.length; i++) { temperature[i] = input.nextdouble();} NOTICE: temperature.length is an array method that provides us the length of the array temperature 22

23 Constructing and Traversing an Array (pp ) A loop that fills up an array completely is referred to as array transversal for (int i = 0; i < temperature.length; i++) { temperature[i] = input.nextdouble();} The array transversal pattern will be used often 23

24 Constructing and Traversing an Array (pp ) Accessing an Array (pp ) A Complete Array Program (pp ) Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 24

25 Accessing an Array (pp ) A Complete Array Program (pp ) Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 25

26 Accessing an Array (pp ) Consider the following: int[] list = new int[5]; for (int i = 0; i < list.length; i++) { list[i] = 2 * i + 1; } First we create the array: Then we put the first five odd integers into the array: 26

27 Accessing an Array (pp ) In this example we have data in ascending order (lowest to highest) int[] list = new int[5]; for (int i = 0; i < list.length; i++) { list[i] = 2 * i + 1; } Whenever we have data sorted into ascending (or descending) order, we can easily find the low, high and median values: low = list[0]; median = list[2]; high = list[4]; 27

28 Accessing an Array (pp ) Whenever we have data sorted into ascending order with length ODD, we can easily find the low, high and median values: low = list[0]; This works for any ODD array (list.length is odd) median = list[list.length/2]; high = list[list.length-1]; 28

29 Accessing an Array (pp ) If the data is even (list.length is even) we need to modify the calculation of the median: This works for any EVEN array (list.length is even) median = (list[list.length/2] + list[list.length/2 1])/2; low = list[0]; high = list[list.length-1]; 29

30 Constructing and Traversing an Array (pp ) Accessing an Array (pp ) A Complete Array Program (pp ) Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 30

31 A Complete Array Program (pp ) Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 31

32 A Complete Array Program (pp ) Consider this weather charting program that DOES NOT use arrays: import java.util.*; // Reads temperatures from the user, computes average temperature. public class Weather1 { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int days = console.nextint(); double sum = 0; for (int i = 0; i < days; i++) { // read each day's temperature System.out.print("Day " + (i + 1) + "'s high temp: "); double temp = console.nextdouble(); sum += temp;} double average = sum/days; System.out.printf("Average temp = %.1f\n", average); // report results } } 32

33 How can we add to this a calculation of how many days were above average? Consider the following program (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = days were above average. 33

34 Why the problem is hard We need each input value twice: to compute the average (a cumulative sum) to count how many were above average We could read each value into a variable... but we: don't know how many days are needed until the program runs don't know how many variables to declare We need a way to declare many variables in one step. 34

35 Arrays are the perfect solution array: object that stores many values of the same type. element: One value in an array. index: A 0-based integer to access an element from an array. index value element 0 element 4 element 9 35

36 Array declaration type[] name = new type[length]; Example: int[] numbers = new int[10]; index value

37 Array declaration, cont. The length can be any integer expression. int x = 2 * 3 + 1; int[] data = new int[x % 5 + 2]; Each element initially gets a "zero-equivalent" value. Type int 0 double 0.0 boolean String or other object false Default value null (means, "no object") 37

38 Accessing elements name[index] name[index] = value; Example: // access // modify numbers[0] = 27; numbers[3] = -6; System.out.println(numbers[0]); if (numbers[3] < 0) { System.out.println("Element 3 is negative."); } index value

39 Arrays of other types double[] results = new double[5]; results[2] = 3.4; results[4] = -0.5; index value boolean[] tests = new boolean[6]; tests[3] = true; index value false false false true false false 39

40 Out-of-bounds Legal indexes: between 0 and the array's length - 1. Reading or writing any index outside this range will throw an ArrayIndexOutOfBoundsException. Example: int[] data = new int[10]; System.out.println(data[0]); System.out.println(data[9]); System.out.println(data[-1]); System.out.println(data[10]); index value // okay // okay // exception // exception 40

41 Weather question Use an array to solve the weather problem: How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = days were above average. 41

42 Weather answer (p. 451) // Reads temperatures from the user, computes average and # days above average. import java.util.*; public class Weather { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int days = console.nextint(); double[] temps = new double[days]; // array to store days' temperatures double sum = 0; for (int i = 0; i < days; i++) { // read/store each day's temperature System.out.print("Day " + (i + 1) + "'s high temp: "); temps[i] = console.nextdouble(); sum += temps[i];} double average = sum / days; int count = 0; // see if each day is above average for (int i = 0; i < days; i++) { if (temps[i] > average) { count++;} } // report results System.out.printf("Average temp = %.1f\n", average); System.out.println(count + " days above average"); } } 42

43 Weather answer (p. 451) // Reads temperatures from the user, computes average and # days above average. import java.util.*; public class Weather { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int days = console.nextint(); double[] temps = new double[days]; // array to store days' temperatures Standard boilerplate for arrays 43

44 Weather answer (p. 451) } double sum = 0; for (int i = 0; i < days; i++) { // read/store each day's temperature System.out.print("Day " + (i + 1) + "'s high temp: "); temps[i] = console.nextdouble(); sum += temps[i];} double average = sum / days; int count = 0; for (int i = 0; i < days; i++) { } if (temps[i] > average) { count++;} // report results System.out.printf("Average temp = %.1f\n", average); System.out.println(count + " days above average"); } // see if each day is above average 44

45 Constructing and Traversing an Array (pp ) Accessing an Array (pp ) A Complete Array Program (pp ) Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 45

46 Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 46

47 Accessing array elements (pp ) int[] numbers = new int[8]; numbers[1] = 3; numbers[4] = 99; numbers[6] = 2; int x = numbers[1]; numbers[x] = 42; numbers[numbers[6]] = 11; // use numbers[6] as index x 3 index numbers value

48 Constructing and Traversing an Array (pp ) Accessing an Array (pp ) A Complete Array Program (pp ) Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 48

49 Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 49

50 Arrays and Methods (pp ) 50

51 Arrays and Methods (pp ) Arrays.copyOf(array, newsize) Arrays.copyOfRange(array, startix, stopix) Arrays.equals(array1, array2) Arrays.fill(array, value) Arrays.sort(array) Arrays.toString(array) 51

52 Limitations of arrays You cannot resize an existing array: int[] a = new int[4]; a.length = 10; // error You cannot compare arrays with == or equals: int[] a1 = {42, -7, 1, 15}; int[] a2 = {42, -7, 1, 15}; if (a1 == a2) {... } if (a1.equals(a2)) {... } An array does not know how to print itself: int[] a1 = {42, -7, 1, 15}; System.out.println(a1); // false! // false! // [I@98f8c4] 52

53 Class Arrays in package java.util has useful static methods for manipulating arrays: Method name binarysearch(array, value) copyof(array, length) equals(array1, array2) fill(array, value) sort(array) tostring(array) Description returns the index of the given value in a sorted array (or < 0 if not found) returns a new copy of an array returns true if the two arrays contain same elements in the same order sets every element to the given value arranges the elements into sorted order returns a string representing the array, such as "[10, 30, -25, 17]" Syntax: Arrays.methodName(parameters) 53

54 Constructing and Traversing an Array (pp ) Accessing an Array (pp ) A Complete Array Program (pp ) Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 54

55 The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 55

56 Arrays and for loops It is common to use for loops to access array elements. for (int i = 0; i < 8; i++) { System.out.print(numbers[i] + " "); } System.out.println(); // output: Sometimes we assign each element a value in a loop. for (int i = 0; i < 8; i++) { numbers[i] = 2 * i; } index value

57 The length field An array's length field stores its number of elements. name.length for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); } // output: It does not use parentheses like a String's.length(). What expressions refer to: The last element of any array? The middle element? 57

58 Constructing and Traversing an Array (pp ) Accessing an Array (pp ) A Complete Array Program (pp ) Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 58

59 Initializing Arrays (p. 459) The Arrays Class (p. 460) 59

60 Quick array initialization type[] name = {value, value, value}; Example: int[] numbers = {12, 49, -2, 26, 5, 17, -6}; index value Useful when you know what the array's elements will be The compiler figures out the size by counting the values 60

61 "Array mystery" problem traversal: An examination of each element of an array. What element values are stored in the following array? int[] a = {1, 7, 5, 6, 4, 14, 11}; for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { a[i + 1] = a[i + 1] * 2; } index } value

62 Constructing and Traversing an Array (pp ) Accessing an Array (pp ) A Complete Array Program (pp ) Random Access (pp ) Arrays and Methods (pp ) The For-Each Loop (pp ) Initializing Arrays (p. 459) The Arrays Class (p. 460) 62

63 The Arrays Class (p. 460) 63

64 The Arrays class (p. 460) Class Arrays in package java.util has useful static methods for manipulating arrays: Syntax: Method name binarysearch(array, value) copyof(array, length) equals(array1, array2) fill(array, value) sort(array) tostring(array) Arrays.methodName(parameters) Description returns the index of the given value in a sorted array (or < 0 if not found) returns a new copy of an array returns true if the two arrays contain same elements in the same order sets every element to the given value arranges the elements into sorted order returns a string representing the array, such as "[10, 30, -25, 17]" 64

65 Arrays.toString (p. 460) Arrays.toString accepts an array as a parameter and returns a String representation of its elements. int[] e = {0, 2, 4, 6, 8}; e[1] = e[3] + e[4]; System.out.println("e is " + Arrays.toString(e)); Output: e is [0, 14, 4, 6, 8] Must import java.util.*; 65

66 Weather question 2 Modify the weather program to print the following output: How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = days were above average. Temperatures: [45, 44, 39, 48, 37, 46, 53] Two coldest days: 37, 39 Two hottest days: 53, 48 66

67 Weather answer 2 // Reads temperatures from the user, computes average and # days above average. import java.util.*; public class Weather2 { public static void main(string[] args) {... int[] temps = new int[days]; // array to store days' temperatures... (same as Weather program) // report results System.out.printf("Average temp = %.1f\n", average); System.out.println(count + " days above average"); } } System.out.println("Temperatures: " + Arrays.toString(temps)); Arrays.sort(temps); System.out.println("Two coldest days: " + temps[0] + ", " + temps[1]); System.out.println("Two hottest days: " + temps[temps.length - 1] + ", " + temps[temps.length - 2]); 67

68 Assignments for this week 1. Laboratory for Chapter 6 due TODAY (Monday 11/03) IMPORTANT: When you me your laboratory Word Document, be sure it is all in one file 2. Laboratory for Chapter 7 due Monday 11/17 IMPORTANT: When you me your laboratory Word Document, be sure it is all in one file 3. Read pp (Chapter 7) for Wednesday 4. Be sure to complete Quiz 18 before leaving class tonight This is another program to write You must demonstrate the program to me before you leave lab 68

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

Topic 21 arrays - part 1

Topic 21 arrays - part 1 Topic 21 arrays - part 1 "Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. " - Stan Kelly-Bootle Copyright Pearson Education, 2010 Based

More information

arrays - part 1 My methods are really methods of working and thinking; this is why they have crept in everywhere anonymously. Emmy Noether, Ph.D.

arrays - part 1 My methods are really methods of working and thinking; this is why they have crept in everywhere anonymously. Emmy Noether, Ph.D. Topic 21 https://commons.wikimedia.org/w/index.php?curid=66702 arrays - part 1 My methods are really methods of working and thinking; this is why they have crept in everywhere anonymously. Emmy Noether,

More information

AP Computer Science. Arrays. Copyright 2010 by Pearson Education

AP Computer Science. Arrays. Copyright 2010 by Pearson Education AP Computer Science Arrays 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: 44 Day 3's high temp: 39

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

Building Java Programs Chapter 7

Building Java Programs Chapter 7 Building Java Programs Chapter 7 Arrays Copyright (c) Pearson 2013. All rights reserved. Can we solve this problem? Consider the following program (input underlined): How many days' temperatures? 7 Day

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Array basics. Readings: 7.1

Array basics. Readings: 7.1 Array basics Readings: 7.1 1 How would you solve this? Consider the following program: How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp:

More information

Array basics. How would you solve this? Arrays. What makes the problem hard? Array auto-initialization. Array declaration. Readings: 7.

Array basics. How would you solve this? Arrays. What makes the problem hard? Array auto-initialization. Array declaration. Readings: 7. How would you solve this? Array basics Readings:. Consider the following program: How many days' temperatures? Day 's high temp: Day 's high temp: Day 's high temp: Day 's high temp: Day 's high temp:

More information

Lecture 9: Arrays. Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp. Copyright (c) Pearson All rights reserved.

Lecture 9: Arrays. Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp. Copyright (c) Pearson All rights reserved. Lecture 9: Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Can we solve this problem? Consider the following program

More information

CSE 143 Lecture 1. Arrays (review) slides created by Marty Stepp

CSE 143 Lecture 1. Arrays (review) slides created by Marty Stepp CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp http://www.cs.washington.edu/143/ Arrays (7.1) array: An object that stores many values of the same type. element: One value in an array.

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG 1 Notice Assignment Class Exercise 19 is assigned Homework 8 is assigned Both Homework 8 and Exercise 19 are

More information

CS 106A, Lecture 16 Arrays

CS 106A, Lecture 16 Arrays CS 106A, Lecture 16 Arrays suggested reading: Java Ch. 11.1-11.5 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin. q Puzzle Day

More information

Arrays Chris Piech CS106A, Stanford University

Arrays Chris Piech CS106A, Stanford University Arrays Chris Piech CS106A, Stanford University What does this say? 53 305))6*;4826)4 )4 );806*;48 8 60))85;1 (;: *8 83(88)5* ;46(;88*96*?;8)* (;485);5* 2:* (;4956*2(5* 4)8 8*;4069285);)6 8)4 ;1( 9;48081;8:8

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Admin. CS 112 Introduction to Programming. Recap: Exceptions. Summary: for loop. Recap: CaesarFile using Loop. Summary: Flow Control Statements

Admin. CS 112 Introduction to Programming. Recap: Exceptions. Summary: for loop. Recap: CaesarFile using Loop. Summary: Flow Control Statements Admin. CS 112 Introduction to Programming q Puzzle Day from Friday to Monday Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email:

More information

CSc 110, Autumn 2016 Lecture 15: lists. Adapted from slides by Marty Stepp and Stuart Reges

CSc 110, Autumn 2016 Lecture 15: lists. Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges Can we solve this problem? Consider the following program (input underlined): How many days' temperatures? 7 Day

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

Array. Prepared By - Rifat Shahriyar

Array. Prepared By - Rifat Shahriyar Java More Details Array 2 Arrays A group of variables containing values that all have the same type Arrays are fixed length entities In Java, arrays are objects, so they are considered reference types

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

Reading Input from Text File

Reading Input from Text File Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 5 Reading Input from Text File Eng. Mohammed Alokshiya November 2, 2014 The simplest

More information

CSE 143 Lecture 2. reading:

CSE 143 Lecture 2. reading: CSE 143 Lecture 2 Implementing ArrayIntList reading: 15.1-15.3 slides adapted from Marty Stepp, Hélène Martin, Ethan Apter and Benson Limketkai http://www.cs.washington.edu/143/ Exercise Pretend for a

More information

AP Computer Science. File Input with Scanner. Copyright 2010 by Pearson Education

AP Computer Science. File Input with Scanner. Copyright 2010 by Pearson Education AP Computer Science File Input with Scanner Input/output (I/O) import java.io.*; Create a File object to get info about a file on your drive. (This doesn't actually create a new file on the hard disk.)

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 6 File Input with Scanner reading: 6.1 6.2, 5.4 2 Input/output (I/O) import java.io.*; Create a File object to get info about a file on your drive. (This doesn't actually

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site I have decided to keep this site for the whole semester I still hope to have blackboard up and running, but you

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com

More information

Arrays Chris Piech CS106A, Stanford University

Arrays Chris Piech CS106A, Stanford University Arrays Chris Piech CS106A, Stanford University Changing Variable Types int to double? int x = 5; double xdbl = x; int to String? int x = 5; String xstr = + x String to int? String xstr = 5 ; int x = Integer.parseInt(x);

More information

Chapter 2: Basic Elements of Java

Chapter 2: Basic Elements of Java Chapter 2: Basic Elements of Java TRUE/FALSE 1. The pair of characters // is used for single line comments. ANS: T PTS: 1 REF: 29 2. The == characters are a special symbol in Java. ANS: T PTS: 1 REF: 30

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Program Analysis Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin q PS5 Walkthrough Thursday

More information

Lecture 10: Arrays II

Lecture 10: Arrays II Lecture 10: Arrays II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Largest Value Given an array, return the largest

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 6: File Processing Lecture 6-1: File input using Scanner reading: 6.1-6.2, 5.3 self-check: Ch. 6 #1-6 exercises: Ch. 6 #5-7 Input/output ("I/O") import java.io.*; Create

More information

Lab Assignment Three

Lab Assignment Three Lab Assignment Three C212/A592 Fall Semester 2010 Due in OnCourse by Friday, September 17, 11:55pm (Dropbox will stay open until Saturday, September 18, 11:55pm) Abstract Read and solve the problems below.

More information

1. An operation in which an overall value is computed incrementally, often using a loop.

1. An operation in which an overall value is computed incrementally, often using a loop. Practice Exam 2 Part I: Vocabulary (10 points) Write the terms defined by the statements below. 1. An operation in which an overall value is computed incrementally, often using a loop. 2. The < (less than)

More information

Introduction to Computer Science Midterm 3 Fall, Points

Introduction to Computer Science Midterm 3 Fall, Points Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do

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

Topic 11 Scanner object, conditional execution

Topic 11 Scanner object, conditional execution Topic 11 Scanner object, conditional execution "There are only two kinds of programming languages: those people always [complain] about and those nobody uses." Bjarne Stroustroup, creator of C++ Copyright

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else; Cumulative sum; reading: 4.2, 4.4-4.5 2 Advanced if/else reading: 4.4-4.5 Factoring if/else code factoring: Extracting common/redundant code.

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

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly

More information

Give one example where you might wish to use a three dimensional array

Give one example where you might wish to use a three dimensional array CS 110: INTRODUCTION TO COMPUTER SCIENCE SAMPLE TEST 3 TIME ALLOWED: 60 MINUTES Student s Name: MAXIMUM MARK 100 NOTE: Unless otherwise stated, the questions are with reference to the Java Programming

More information

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes Motivation of Loops Loops EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG We may want to repeat the similar action(s) for a (bounded) number of times. e.g., Print the Hello World message

More information

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file. CSC116 Practice Exam 2 - KEY Part I: Vocabulary (10 points) Write the terms defined by the statements below. Cumulative Algorithm 1. An operation in which an overall value is computed incrementally, often

More information

Loops. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG

Loops. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Loops EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Understand about Loops : Motivation: Repetition of similar actions Two common loops: for and while Primitive

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

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

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

Software Practice 1 Basic Grammar

Software Practice 1 Basic Grammar Software Practice 1 Basic Grammar Basic Syntax Data Type Loop Control Making Decision Prof. Joonwon Lee T.A. Jaehyun Song Jongseok Kim (42) T.A. Sujin Oh Junseong Lee (43) 1 2 Java Program //package details

More information

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while CSCI 150 Fall 2007 Java Syntax The following notes are meant to be a quick cheat sheet for Java. It is not meant to be a means on its own to learn Java or this course. For that you should look at your

More information

Arrays in Java Using Arrays

Arrays in Java Using Arrays Recall Length of an Array Arrays in Java Using Arrays Once an array has been created in memory, its size is fixed for the duration of its existence. Every array object has a length field whose value can

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 Announcements Check the calendar on the course webpage regularly for updates on tutorials and office hours.

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

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-4: do/while loops, assertions reading: 5.1, 5.5 1 The do/while loop do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 15 testing ArrayIntList; pre/post conditions and exceptions reading: 4.4 15.1-15.3 2 Searching methods Implement the following methods: indexof returns first index of element,

More information

Bjarne Stroustrup. creator of C++

Bjarne Stroustrup. creator of C++ We Continue GEEN163 I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone. Bjarne Stroustrup creator

More information

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Java Lists and Arrays Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Objectives 1) Create and use arrays of base types and objects. 2) Create

More information

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Computer Programming, I. Laboratory Manual. Experiment #6. Loops 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 #6

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

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution Ch 5 Arrays Multiple Choice Test 01. An array is a ** (A) data structure with one, or more, elements of the same type. (B) data structure with LIFO access. (C) data structure, which allows transfer between

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

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

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

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice CONTENTS: Arrays Strings COMP-202 Unit 5: Loops in Practice Computing the mean of several numbers Suppose we want to write a program which asks the user to enter several numbers and then computes the average

More information

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each Your Name: Your Pitt (mail NOT peoplesoft) ID: Part Question/s Points available Rubric Your Score A 1-6 6 1 pt each B 7-12 6 1 pt each C 13-16 4 1 pt each D 17-19 5 1 or 2 pts each E 20-23 5 1 or 2 pts

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 6: File Processing Lecture 6-2: Advanced file input reading: 6.3-6.5 self-check: #7-11 exercises: #1-4, 8-11 Copyright 2009 by Pearson Education Hours question Given a file

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-4: do/while loops, assertions reading: 5.1, 5.5 1 The do/while loop do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's

More information

Chapter 4: Control Structures I

Chapter 4: Control Structures I Chapter 4: Control Structures I Java Programming: From Problem Analysis to Program Design, Second Edition Chapter Objectives Learn about control structures. Examine relational and logical operators. Explore

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: 6.1-6.2, 5.3 self-check: Ch. 6 #1-6 exercises: Ch. 6 #5-7 videos: Ch. 6 #1-2 Input/output (I/O) import java.io.*; Create a

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

"42 million of anything is a lot." -Doug Burger (commenting on the number of transistors in the Pentium IV processor)

42 million of anything is a lot. -Doug Burger (commenting on the number of transistors in the Pentium IV processor) Topic 20 Arrays part 2 "42 million of anything is a lot." -Doug Burger (commenting on the number of transistors in the Pentium IV processor) Based on slides for Building Java Programs by Reges/Stepp, found

More information

Topic 11 Scanner object, conditional execution

Topic 11 Scanner object, conditional execution https://www.dignitymemorial.com/obituaries/brookline-ma/adele-koss-5237804 Topic 11 Scanner object, conditional execution Logical thinking and experience was as important as theory in using the computer

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

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes Motivation of Loops Loops EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG We may want to repeat the similar action(s) for a (bounded) number of times. e.g., Print

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

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

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution Ch 5 Arrays Multiple Choice 01. An array is a (A) (B) (C) (D) data structure with one, or more, elements of the same type. data structure with LIFO access. data structure, which allows transfer between

More information

Loops. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Loops. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG Loops EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG Learning Outcomes Understand about Loops : Motivation: Repetition of similar actions Two common loops: for

More information

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises: #7 videos: Ch. 4 #2-4 The if/else statement Executes one block if a test is true,

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

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

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

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 4: Conditional Execution 1 loop techniques cumulative sum fencepost loops conditional execution Chapter outline the if statement and the if/else statement relational expressions

More information