Computer Science is...

Size: px
Start display at page:

Download "Computer Science is..."

Transcription

1 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 seizures through neurostimulation. (X-ray showing a neurostimulator.)

2 How many iterations does the following loop consist of? int x = 5; while (x > 4) { System.out.println( I have to write + this over and over. ); x++; }

3 Infinite Loops: A Common Logical Error An infinite loop executes until the user interrupts (terminates) the program. This is what happens when the statements in the body of a while loop never update the loop condition to false 3

4 What will be displayed? Why is it the wrong result? Correct the program so that it displays the right sum. public class Sum { public static void main(string[] args) { final int MAX = 5; int sum = 0; int i = 1; while(i < MAX) { sum = sum + i; System.out.println("Sum: " + sum); i = i + 1; System.out.println("i: " + i); System.out.println(); } } } System.out.println("The sum of integers from 1 to " + MAX + " is: " + sum); 4

5 Sum: 1 i: 2 Sum: 3 i: 3 Sum: 6 i: 4 Sum: 10 i: 5 The sum of the integers from 1 to 5 is: 10

6 Off-By-One Errors It is a common logical error to write loop conditions that result in the loop body being executed one time too few, or one time too many You should always test your code to check that your loops conditions do not cause such errors 6

7 Nested Loops Like if and if-else statements, loops can be nested Every time the body of the outer loop is executed, the inner loop will go through its entire set of iterations 7

8 What does this display? public class NestedLoop { public static void main(string[] args) { final int MAX = 4; int outercount, innercount; } } outercount = 1; while(outercount <= MAX) { innercount = 1; while (innercount <= MAX) { System.out.print("(" + outercount + "," + innercount + ") "); innercount++; } System.out.println(); outercount++; } System.out.println("Done."); 8

9 (1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2,3) (2,4) (3,1) (3,2) (3,3) (3,4) (4,1) (4,2) (4,3) (4,4) Done.

10 What does this display? public class AnotherNestedLoop { public static void main(string[] args) { final int MAX = 4; int outercount, innercount; } } outercount = 1; while(outercount <= MAX) { innercount = 1; while (innercount <= outercount) { System.out.print("(" + outercount + "," + innercount + ") "); innercount++; } System.out.println(); outercount++; } System.out.println("Done."); 10

11 (1,1) (2,1) (2,2) (3,1) (3,2) (3,3) (4,1) (4,2) (4,3) (4,4) Done.

12 The for Statement 12

13 Example: a for loop for(int x = 0; x < 4; x++) { System.out.println( I have to write + this over and over. ); } I have to write this over and over. I have to write this over and over. I have to write this over and over. I have to write this over and over.

14 for loops are a little different from while loops for (initialization; condition; follow-up action) One statement for (initialization; condition; follow-up action) One block of statements 14

15 Components of a for loop for(int x = 0; x < 4; x++) { System.out.println( I have to write + this over and over. ); } 15

16 Logic of a for Statement initialization false condition evaluated true body follow-up (rest of the program) 16

17 Logic of a for Statement x=0 false x<5 true {loop body} x++ (rest of the program) 17

18 for Loops as while Loops A for loop is equivalent to the following while loop structure: initialization; while ( condition ) { for-loop body; follow-up; } 18

19 AnotherCounter.java public class AnotherCounter { public static void main(string[] args) { final int LIMIT = 3; int count = 0; for (count=1; count <= LIMIT; count++) { System.out.println(count); } } } System.out.println("Done. " + count); 19

20 AnotherCounter.java public class AnotherCounter { public static void main(string[] args) { final int LIMIT = 3; int count = 0; for (count=1; count <= LIMIT; count++) { System.out.println(count); } } } System.out.println("Done. " + count); Console: 1 LIMIT: 3 2 count: Done. 4 20

21 What shape does this display? public class Stars { public static void main (String[] args) { final int MAX_ROWS = 10; } } for (int row = 1; row <= MAX_ROWS; row++) { for (int star = 1; star <= row; star++) { System.out.print("*"); } System.out.println(); }

22 What Shape does this Display? public class WhatIsThis { public static void main(string[] args) { final int MAX_ROWS = 10; for (int row = 1; row <= MAX_ROWS; row++) { for (int space = 1; space <= MAX_ROWS - row; space++) { System.out.print(" "); } for (int star = 1; star <= row * 2; star++) { System.out.print("*"); } System.out.println(); } for (int base = 3; base > 0; base--) { for (int space = 1; space <= MAX_ROWS-1; space++) { System.out.print(" "); } System.out.println("**"); } } }

23 Details about the for Statement Each expression in the header of a for loop is optional If the initialization portion is left out, no initialization is performed If the condition portion is left out, it is always considered to evaluate to true, and therefore creates an infinite loop If the increment portion is left out, no increment operation is performed Both semi-colons are always required in the for loop header 23

24 24

25 Arrays 25

26 A variable of type int. int number = 5 In memory: 5

27 A variable of type int. int number = 5 In memory: 5 An integer array corresponds to variable of type int[]. int[] weights = {5,6,0,4,0,1,2,12,82,1} In memory:

28 An array is a fixed-size, ordered collection of elements of the same type. int[] weights = {5,6,0,4,0,1,2,12,82,1} In memory:

29 Why use arrays? They make large amounts of data easier to handle. int[] weights = {5,6,0,4,0,1,2,12,82,1} In memory:

30 Each cell in the array has an index. e.g. The cell that contains 82 has index 8. The cell that contains 5 has index 0. In memory:

31 Each cell in the array has an index. e.g. The cell that contains 82 has index 8. The cell that contains 5 has index 0. In memory: So you can write: int j = weights[8] + weights[0];

32 Array Basics 32

33 Array Declaration Examples double[] prices; Declares an array called prices Each element in this array is a double; variable prices is of type double[] char[] code; Declares an array called code Each element of this array is a char; variable code is of type char[] String[] names; Declares an array called names Each element of this array is a String; variable names is of type String[] 33

34 What does this display? public class FirstArray { public static void main(string[] args){ String[] names = {"Jordan", "Jesse", "Joshua"}; for(int i = 1; i >= -1; i = i - 1) System.out.println(names[i+1]); } }

35 What does this display? public class FirstArray { public static void main(string[] args){ String[] names = {"Jordan", "Jesse", "Joshua"}; for(int i = 1; i >= -1; i = i - 1) System.out.println(names[i]); } }

36 Review: how primitive types are stored in memory Example: int number = 5 In memory: number 5 number represents a location in memory where the integer 5 is stored

37 Array types are NOT primitive types Example: int[] weights = {8,6,0,4,0,1,2,12,82,1} weights weights represents a location in memory where the address of the first array cell is stored.

38 Primitive vs. reference types Primitive types: The variable represents the location in memory at which an actual value, like the integer 175. Reference types: The variable represents the location in memory at which another memory address (or reference ) is stored.

39 int[] weights = {5,6,0,4,0,1,2,12,82,1} In memory:

40 Initializer Lists int[] numbers = {2, 3, 5}; The above statement does all the following in one step: It declares a variable of type int[] called numbers It creates an array which contains 3 elements It stores the address in memory of the new array in variable numbers It sets the value of first element of the array to 2, the value of the second element of the array to 3, and the value of the last element of the array to be 5 Often, these steps are carried out separately. 40

41 41

42 Primitive vs. reference types Primitive types: The variable represents the location in memory at which an actual value, like the integer 175. Reference types: The variable represents the location in memory at which another memory address (or reference ) is stored.

43 Array types are reference types The declaration int[] numberarray; creates a reference variable, which holds a reference to an int[] No array is created yet, just a reference to an array. 43

44 Array Allocation (1) final int SIZE = 5; int[] numbers; numbers = new int[2 * SIZE]; numbers - SIZE 5 44

45 Array Allocation (2) final int SIZE = 5; int[] numbers; numbers = new int[2 * SIZE]; numbers SIZE

46 Allocating Arrays (1) variablename = new type [ size ]; The variable in which the location in memory of the newly created array will be stored new is a reserved word in Java An expression which specifies the number of elements in the array The type of the elements to be stored in the array 46

47 Allocating Arrays (3) Once an array has been created, its size cannot be changed As with regular variables, the array declaration and the array allocation operation can be combined: type[] variablename = new type[size]; 47

48 Initializer Lists int[] numbers = {2, 3, 5}; is equivalent to the following code fragment: int[] numbers = new int[3]; numbers[0] = 2; numbers[1] = 3; numbers[2] = 5; 48

49 Exercise on reference types: what does this display? public class ArrayCopy { public static void main(string[] args){ int[] numbers = {1, 2, 3}; int[] differentnumbers = new int[3]; differentnumbers = numbers; numbers[1] = 2; differentnumbers[1] = 3; System.out.println(numbers[1]); System.out.println(differentNumbers[1]); } }

50 Each cell in the array has an index. e.g. The cell that contains 82 has index 8. The cell that contains 5 has index 0. In memory: So you can write: int j = weights[8] + weights[0];

51 Array Access Example numbers[0] = 1; numbers[1] = numbers[0]; numbers[2 * SIZE 1] = 2 * numbers[0] + 1; numbers SIZE

52 Array Length int[] weights = {5,6,0,4,0,1,2,12,82,1} total = weights.length; We can get the length of any array with [arrayname].length Here, total is assigned the value 10.

53 Array Length int[] weights = {5,6,0,4,0,1,2,12,82,7} total = weights.length; What is the index of the cell containing 7 in terms of weights.length?

54 Array Length int[] weights = {5,6,0,4,0,1,2,12,82,7} total = weights.length; What is the index of the cell containing 7 in terms of weights.length? Answer: weights.length - 1

55 The length of an array is a constant int[] weights = {5,6,0,4,0,1,2,12,82,1} weights.length = 2; // illegal! The length field of an array can be used like any other final variable of type int.

56 Fill in the method init so that it initializes the array list with a decreasing list of integers starting at start. e.g. If myarray has length 5, a call to init(myarray, 20) will assign the following values to an array: {20, 19, 18, 17, 16}. public static void init(int[] list, int start){ // use a loop to assign a value to each cell }

57 57

58 Bounds Checking (2) int[] myarray = new int[5]; myarray[5] = 42; // Array myarray contains 5 elements, so // the valid indices for myarray are 0-4 // inclusive // Therefore, the program crashes When this occurs, the error message will mention that the program threw an ArrayIndexOutOfBoundsException The error message should also mention which line in your program caused the latter to crash 58

59 Reading Exception Output (1) Exception in thread "main" java.lang. ArrayIndexOutOfBoundsException: 5 ` at IndexOutOfBoundsDemo.main(IndexOutOfBoundsDemo.java:11) Method where the problem occurred File where the problem occurred Line number where the problem occurred Nature of the problem and additional information Index we tried to access and caused the crash 59

60 Review Exercises Which of the following choices is the correct syntax for quickly declaring/initializing an array of integers to store a particular list of values? (1) int[] a = {17, -3, 42, 5, 9, 28}; (2) int a {17, -3, 42, 5, 9, 28}; (3) int[] a = new int[6] {17, -3, 42, 5, 9, 28}; (4) int[6] a = {17, -3, 42, 5, 9, 28}; (5) int[] a = new {17, -3, 42, 5, 9, 28} [6];

61 Review Exercises Which of the following choices is the correct syntax for quickly declaring/initializing an array of integers to store a particular list of values? (1) int[] a = {17, -3, 42, 5, 9, 28}; (2) int a {17, -3, 42, 5, 9, 28}; (3) int[] a = new int[6] {17, -3, 42, 5, 9, 28}; (4) int[6] a = {17, -3, 42, 5, 9, 28}; (5) int[] a = new {17, -3, 42, 5, 9, 28} [6];

62 Review Exercises Which of the following choices is the correct syntax for declaring/initializing an array of integers? (1) []int a = [10]int; (2) int a[10]; (3) int[10] a = new int[10]; (4) int[] a = new int[10]; (5) int a[10] = new int[10];

63 Review Exercises Which of the following choices is the correct syntax for declaring/initializing an array of integers? (1) []int a = [10]int; (2) int a[10]; (3) int[10] a = new int[10]; (4) int[] a = new int[10]; (5) int a[10] = new int[10];

64 Review Exercises What values are stored in data after this code executes? int[] data = new int[8]; data[0] = 3; data[7] = -18; data[4] = 5; data[1] = data[0]; int x = data[4]; data[4] = 6; data[x] = data[0] * data[1];

65 Review Exercises

66 Review Exercises What values are stored in list after this code executes? int[] list = {2, 18, 6, -4, 5, 1}; for (int i = 0; i < list.length; i++) { list[i] = list[i] + (list[i] / list[0]); }

67 Review Exercises

68 Multidimensional Arrays 68

69 Review: 1-Dim. Array Allocation int[] numbers = new int[10]; numbers

70 2D Array Allocations (2) int[][] numbers = new int[3][5]; numbers

71 Two-Dimensional Arrays (2) It may be useful to think of two-dimensional arrays as tables of values with rows and columns The first index specifies a row or column The second index specifies an element within a row or column 71

72 Exercise Declare and create a 4 by 5 by 6 array of doubles called x

73 Solution Declare and create a 4 by 5 by 6 array of doubles called x double[][][] x = new double[4][5][6]; 73

74 2D Array Access Example (3) numbers[1][3] = 7; numbers

75 2D Array Access Example (3) numbers[1][3] = 7; numbers[2][4] = numbers[1][3] - 2; numbers

76 2D Array Access Example (2) Consider the expression numbers[1] This expression refers to the element at position 1 of a two-dimensional array of ints This element is an entire one-dimensional array of ints Its type is int[], and therefore can be used in the same manner as a regular variable of type int[] 76

77 Example int[][] array = new int[2][2]; int[] x = {1, 2}; array[0] = x; int j = array[0][1]

78 2D Array and length Fields (2) int size = numbers.length; int rowsize = numbers[0].length; numbers size rowsize

79 Exercise Use length to complete the following nested for loops to fill values with a multiplication table: in other words, values[i][j] should contain the result of i*j. int[][] values = new int[5][10]; for(int i = 0; ; ){ for(int j = 0; ; ){ } }

80 Solution int[][] values = new int[5][10]; for(int i = 0; i < values.length ; i++ ){ for(int j = 0; j < values[0].length; j++ ){ values[i][j] = i*j; } }

81 Example from before int[][] array = new int[2][2]; int[] x = {1, 2}; array[0] = x; int j = array[0][0]

82 Example: this compiles, too int[][] array = new int[2][2]; int[] x = {4, 5, 6}; array[0] = x; int j = array[0][0]

83 Example: this compiles, too int[][] array = new int[2][2]; int[] x = {7}; array[0] = x; int j = array[0][0]

84 These variations show us that you can create 2D arrays in which the row arrays have different sizes. These are called jagged arrays

85 A few more details about methods 85

86 public static int countmatches(string s1, String s2) 86

87 General Method Header Syntax modifiers returntype methodname ( parameterlist ) Where: modifiers public, private, static,... (optional) (there can be more than one) returntype primitive or reference type (mandatory) methodname identifier, as defined previously (mandatory) ( (mandatory) parameterlist defined later (optional) ) (mandatory) 87

88 Access Modifiers: public/private When a method is private, only the methods declared in the same class can call it; When a method is public, any method declared in any class can call it Access modifiers are mutually exclusive; that is, a method header cannot specify more than one access modifier 88

89 private vs. public Methods Some of the methods in a class only support other methods in the same class: such methods should always be declared private 89

90 Other Method Modifiers: static Another important modifier is static For now, all the methods we write will be declared static Therefore, the static modifier MUST be included in the header of each method that we write (at least for now) We will cover non-static methods (also called instance methods) when we cover objects 90

91 91

92 Calling Methods (1) A method call has the following syntax: classname. methodname( parameters ) The name of the class in which the method is declared 92

93 A call to a non-void method is an expression Non-void methods can also be used as an operand in a larger expression Examples: int s = add(a, b); Assigns the value returned by the call to method add() with actual parameters a and b to variable s int s = add(a, b) / add(c, d); Divides the value returned by the call to method add() with actual parameters a and b by the value returned by the call to method add() with actual parameters c and d; assigns the quotient to variable s 93

94 A call to a void method is a statement In other words, you can think of it as representing an action or a side-effect e.g. print this, read this file You cannot use void methods inside expressions 94

95 95

96 Overloading Example Consider the following class definition: public class OverloadingDemo { public static double tryme(int x) { return x ; } public static double tryme(int x, double y) { return x * y; } public static void main(string[] args) { double result = tryme(25, 4.32); System.out.println(result); } } 96

97 Method Overloading Method overloading is the process of using the same method name for multiple methods declared in the same class The signature of each overloaded method must be unique The signature of a method consists of: the method's name the number of parameters it accepts the types of the parameters it accepts the order of the parameters' types 97

98 Method Overloading When a method is called, the compiler must be able to determine which version of the method is being invoked by analyzing the types of the actual parameters 98

99 More Overloading Examples Many methods defined in the Java Standard Class Library are overloaded e.g The print() and println() methods 99

100 Reference Types 100

101 Array types are NOT primitive types Example: int[] weights = {8,6,0,4,0,1,2,12,82,1} weights weights represents a location in memory where the address of the first array cell is stored.

102 Primitive vs. reference types Primitive types: The variable represents the location in memory at which an actual value, like the integer 175. Reference types: The variable represents the location in memory at which another memory address (or reference ) is stored.

103 Reference types represent objects. An array is an example of an object. Other examples include Scanner and String. More details on objects in Unit 7.

104 The concepts explained in the following slides apply to ALL reference types, not just arrays.

105 Review: variable declarations for reference types e.g. int[] array; or Scanner keyboard; BUT the variables represent memory addresses instead of values. No object is actually created in memory yet, just an address to where such an object would go

106 Array Allocation (1) final int SIZE = 5; int[] numbers; numbers = new int[2 * SIZE]; numbers - SIZE 5 106

107 Array Allocation (2) final int SIZE = 5; int[] numbers; numbers = new int[2 * SIZE]; numbers SIZE

108 Creating Objects (1) In general, we use the new operator to create an object: numbers = new int[10]; 2 exceptions. We create objects without using new : When we use array initializer lists int[] numbers = {1, 2, 3}; When we use string literals: fullname = firstname + Frydrychowicz 108

109 Primitive Types In Memory int i1, i2 = 8; // Line 1 i1 = 6; // Line 2 i2 = i1; // Line 3 Line 1: i1 i2 8 Line 2: i1 6 i2 8 Line 3: i1 6 i

110 Reference Types in Memory int[] a1, a2 = {2, 3, 5}; // Line 1 a1 = new int[3]; // Line 2 a2 = a1; // Line 3 a a2 Line 1: a a2 Line 2: a a2 Line 3:

111 a2 is an alias for a1 (they both point to the same thing) a a2 Line 3:

112 Example from a few lectures ago... public class ArrayCopy { public static void main(string[] args){ int[] numbers = {1, 2, 3}; int[] differentnumbers = new int[3]; differentnumbers = numbers; numbers[1] = 2; differentnumbers[1] = 3; System.out.println(numbers[1]); System.out.println(differentNumbers[1]); } }

113 Aliasing can only happen with reference types!

114 Another alias example int[] a1 = {2, 3, 5}; // Line 1 int[] a2 = a1; // Line 2 a1[0] = 7; // Line 3 int v = a2[0]; // Line 4 a1 Line 1:

115 Another alias example int[] a1 = {2, 3, 5}; // Line 1 int[] a2 = a1; // Line 2 a1[0] = 7; // Line 3 int v = a2[0]; // Line 4 a1 Line 1: a1 Line 2: a

116 Another alias example int[] a1 = {2, 3, 5}; // Line 1 int[] a2 = a1; // Line 2 a1[0] = 7; // Line 3 int v = a2[0]; // Line 4 a a2 Line 3:

117 Another alias example int[] a1 = {2, 3, 5}; // Line 1 int[] a2 = a1; // Line 2 a1[0] = 7; // Line 3 int v = a2[0]; // Line 4 a a2 Line 3: a a2 v

118 Comparing objects 118

119 Comparing Objects Using == int[] a1 = {2, 3, 5}; // Line 1 int[] a2 = a1; // Line 2 boolean b = (a1 == a2); // Line 3 a Line 1: a a2 Line 2: a a2 b true Line 3:

120 Comparing Objects Using == int[] a1 = {2, 3, 5}; // Line 1 int[] a2 = {2, 3, 5}; // Line 2 boolean b = (a1 == a2); // Line 3 a Line 1: a a2 Line 2: a a2 b false Line 3:

121 More on Comparing Objects To see check whether two objects have the same contents, we need to compare their contents manually For arrays, this can be done using a loop to verify whether all elements are the same For other kinds of objects (like Strings), we can use the equals() method 121

122 Null references 122

123 NullPointerDemo.java public class NullPointerDemo { public static void main(string[] args) { int[] a = null; } } System.out.println("Attempting to retrieve the " + "element stored at index 0 of an array through a " + "null reference variable..."); System.out.println("The value stored at index 0 of " + "this array is: " + a[0]); 123

124 Reading Exception Output (2) The program's output: Attempting to retrieve the element stored at index 0 of an array through a null reference variable... Exception in thread "main" java.lang.nullpointerexception at NullPointerDemo.main(NullPointerDemo.java:8) ` Nature of the problem 124

125 How reference variables behave when passed as parameters to a method 125

126 Example public static void main(string[] args){ int[] list = {5, 1, 3}; scramble(list); System.out.print(list[2]); } public static void scramble(int[] input) { int temp = input[0]; input[0] = input[input.length 1]; input[input.length - 1] = temp; }

127 What is the content of a3 after calling method mystery. int[] a3 = {7, 2, 8, 10}; mystery(a3); public static void mystery(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] < a[i + 1]) { a[i] = a[i + 1]; } } }

128

129 A few review exercises about loops 129

130 How does the value of j change during the execution of this loop? int j; for(j = 50; j > 0; j=j-1){ } j = j/2;

131 Iteration Value of j Statement 0 50 int j = j = j/ j = j j = j/

132 Iteration Value of j Statement 0 50 int j = j = j / j = j j = j / j = j j = j / j = j j = j / j = j j = j / j = j - 1

133 What is printed? for(int j = 50; j > -2; j=j-1){ j = j/2; System.out.println(j); } System.out.println(j);

134 Infinite loop:

Last Class. While loops Infinite loops Loop counters Iterations

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

More information

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

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

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

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money

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

Top-Down Program Development

Top-Down Program Development Top-Down Program Development Top-down development is a way of thinking when you try to solve a programming problem It involves starting with the entire problem, and breaking it down into more manageable

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

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

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19 Arrays Lecture 11 CGS 3416 Spring 2017 March 6, 2017 Lecture 11CGS 3416 Spring 2017 Arrays March 6, 2017 1 / 19 Arrays Definition: An array is an indexed collection of data elements of the same type. Indexed

More information

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

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

More information

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1 COMP 202 Programming With Iterations CONTENT: The WHILE, DO and FOR Statements COMP 202 - Loops 1 Repetition Statements Repetition statements or iteration allow us to execute a statement multiple times

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

The Basic Parts of Java

The Basic Parts of Java Data Types Primitive Composite The Basic Parts of Java array (will also be covered in the lecture on Collections) Lexical Rules Expressions and operators Methods Parameter list Argument parsing An example

More information

Loops. CSE 114, Computer Science 1 Stony Brook University

Loops. CSE 114, Computer Science 1 Stony Brook University Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?

More information

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166 Lecture 13 & 14 Single Dimensional Arrays Dr. Martin O Connor CA166 www.computing.dcu.ie/~moconnor Table of Contents Declaring and Instantiating Arrays Accessing Array Elements Writing Methods that Process

More information

Q5. What values are stored in the array at the comment in main? Note that the incrementall returns void, but does take an int[] parameter.

Q5. What values are stored in the array at the comment in main? Note that the incrementall returns void, but does take an int[] parameter. Q1. Which of the following choices is the correct syntax for declaring/initializing an array of integers? 1. int[] a = new int[10]; 2. int a[10] = new int[10]; 3. []int a = [10]int; 4. int a[10]; 5. int[10]

More information

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

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

More information

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

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

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

CS171:Introduction to Computer Science II

CS171:Introduction to Computer Science II CS171:Introduction to Computer Science II Department of Mathematics and Computer Science Li Xiong 9/7/2012 1 Announcement Introductory/Eclipse Lab, Friday, Sep 7, 2-3pm (today) Hw1 to be assigned Monday,

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

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

More information

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

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

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

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

More information

University of Palestine. Mid Exam Total Grade: 100

University of Palestine. Mid Exam Total Grade: 100 First Question No. of Branches (5) A) Choose the correct answer: 1. If we type: system.out.println( a ); in the main() method, what will be the result? int a=12; //in the global space... void f() { int

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

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods Chapter 7: User-Defined Methods Java Programming: Program Design Including Data Structures Chapter Objectives Understand how methods are used in Java programming Learn about standard (predefined) methods

More information

Creating and Using Objects

Creating and Using Objects Creating and Using Objects 1 Fill in the blanks Object behaviour is described by, and object state is described by. Fill in the blanks Object behaviour is described by methods, and object state is described

More information

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops Introductory Programming Imperative Programming III, sections 3.6-3.8, 3.0, 3.9 Anne Haxthausen a IMM, DTU 1. Loops (while, do, for) (sections 3.6 3.8) 2. Overview of Java s (learnt so far) 3. Program

More information

COMP-202 Unit 5: Basics of Using Objects

COMP-202 Unit 5: Basics of Using Objects COMP-202 Unit 5: Basics of Using Objects CONTENTS: Concepts: Classes, Objects, and Methods Creating and Using Objects Introduction to Basic Java Classes (String, Random, Scanner, Math...) Introduction

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

(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

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

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

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

Chapter 7 User-Defined Methods. Chapter Objectives

Chapter 7 User-Defined Methods. Chapter Objectives Chapter 7 User-Defined Methods Chapter Objectives Understand how methods are used in Java programming Learn about standard (predefined) methods and discover how to use them in a program Learn about user-defined

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

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 10 For Loops and Arrays Outline Problem: How can I perform the same operations a fixed number of times? Considering for loops Performs same operations

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

(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

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions: CS 170 Exam 2 Version: A Fall 2015 Name (as in OPUS) (print): Section: Seat Assignment: Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do

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

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

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char Review Primitive Data Types & Variables int, long float, double boolean char String Mathematical operators: + - * / % Comparison: < > = == 1 1.3 Conditionals and Loops Introduction to Programming in

More information

Recap: Assignment as an Operator CS 112 Introduction to Programming

Recap: Assignment as an Operator CS 112 Introduction to Programming Recap: Assignment as an Operator CS 112 Introduction to Programming q You can consider assignment as an operator, with a (Spring 2012) lower precedence than the arithmetic operators First the expression

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

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

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

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

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting Unit 2, Part 2 Definite Loops Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting Let's say that we're using a variable i to count the number of times that

More information

Building Java Programs Chapter 2

Building Java Programs Chapter 2 Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. Data types type: A category or set of data values. Constrains the operations that can

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #07: September 21, 2015 1/30 We explained last time that an array is an ordered list of values. Each value is stored at a specific, numbered position in

More information

Chapter 4: Control structures. Repetition

Chapter 4: Control structures. Repetition Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

Instructor: Eng.Omar Al-Nahal

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

More information

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

Lecture 13: Two- Dimensional Arrays

Lecture 13: Two- Dimensional Arrays Lecture 13: Two- Dimensional Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Nested Loops Nested loops nested loop:

More information

CSE 114 Computer Science I

CSE 114 Computer Science I CSE 114 Computer Science I Iteration Cape Breton, Nova Scotia What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps?

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming (Spring 2012) Lecture #7: Variable Scope, Constants, and Loops Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112

More information

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables Java Program Structure // comments about the class public class MyProgram { Java Review class header class body Comments can be placed almost anywhere This class is written in a file named: MyProgram.java

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

CS171:Introduction to Computer Science II

CS171:Introduction to Computer Science II CS171:Introduction to Computer Science II Department of Mathematics and Computer Science Li Xiong 1/24/2012 1 Roadmap Lab session Pretest Postmortem Java Review Types, variables, assignments, expressions

More information

Chapter 4: Control structures

Chapter 4: Control structures Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003 1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 7, 2003 Name: Email Address: TA: Section: You have 90 minutes to complete this exam. For coding questions, you do not need to

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

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters Programming Constructs Overview Method calls More selection statements More assignment operators Conditional operator Unary increment and decrement operators Iteration statements Defining methods 27 October

More information

CS111: PROGRAMMING LANGUAGE II

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

More information

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

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

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw. Building Java Programs Chapter 2 bug Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. 2 An Insect Software Flaw 3 4 Bug, Kentucky Bug Eyed 5 Cheesy Movie 6 Punch Buggy

More information

Building Java Programs Chapter 2

Building Java Programs Chapter 2 Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson 2013. All rights reserved. bug 2 An Insect 3 Software Flaw 4 Bug, Kentucky 5 Bug Eyed 6 Cheesy Movie 7 Punch Buggy

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CS212 Midterm. 1. Read the following code fragments and answer the questions. CS1 Midterm 1. Read the following code fragments and answer the questions. (a) public void displayabsx(int x) { if (x > 0) { System.out.println(x); return; else { System.out.println(-x); return; System.out.println("Done");

More information

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is

More information

Lecture 14. 'for' loops and Arrays

Lecture 14. 'for' loops and Arrays Lecture 14 'for' loops and Arrays For Loops for (initiating statement; conditional statement; next statement) // usually incremental body statement(s); The for statement provides a compact way to iterate

More information

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below. C212 Early Evaluation Exam Mon Feb 10 2014 Name: Please provide brief (common sense) justifications with your answers below. 1. What is the type (and value) of this expression: 5 * (7 + 4 / 2) 2. What

More information

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics Grouping Objects Primitive Arrays and Iteration Produced by: Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topic List Primitive arrays Why do we need them? What are they?

More information

Section 003 Fall CS 170 Exam 2. Name (print): Instructions:

Section 003 Fall CS 170 Exam 2. Name (print): Instructions: CS 170 Exam 2 Section 003 Fall 2012 Name (print): Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than

More information

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. Name: Write all of your responses on these exam pages. 1 Short Answer (5 Points Each) 1. What is the difference between a compiler and an interpreter? Also, discuss Java s method. 2. Java is a platform-independent

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

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

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

More information

Pace University. Fundamental Concepts of CS121 1

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

More information

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops. Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8 Handout 5 Loops. Loops implement repetitive computation, a k a iteration. Java loop statements: while do-while for 1. Start with the while-loop.

More information

STUDENT LESSON A12 Iterations

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

More information

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 2 Data and expressions reading: 2.1 3 The computer s view Internally, computers store everything as 1 s and 0

More information

Repetition CSC 121 Fall 2014 Howard Rosenthal

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

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 2 Data types type: A category or set of data

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 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

PROGRAMMING FUNDAMENTALS

PROGRAMMING FUNDAMENTALS PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write

More information

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

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn CPS109 Course Notes 6 Alexander Ferworn Unrelated Facts Worth Remembering Use metaphors to understand issues and explain them to others. Look up what metaphor means. Table of Contents Contents 1 ITERATION...

More information

CMPT 126: Lecture 6 Arrays

CMPT 126: Lecture 6 Arrays CMPT 126: Lecture 6 Arrays Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University September 25, 2007 1 Array Elements An array is a construct used to group and organize data.

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

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1 Name SOLUTION Page Points Score 2 15 3 8 4 18 5 10 6 7 7 7 8 14 9 11 10 10 Total 100 1 P age 1. Program Traces (41 points, 50 minutes)

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

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

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x ); Chapter 4 Loops Sections Pages Review Questions Programming Exercises 4.1 4.7, 4.9 4.10 104 117, 122 128 2 9, 11 13,15 16,18 19,21 2,4,6,8,10,12,14,18,20,24,26,28,30,38 Loops Loops are used to make a program

More information

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007 Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2007 Final Examination Question Max

More information