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

Size: px
Start display at page:

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

Transcription

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

2 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 of them. COMP Programming Basics 2

3 First, think of what we need to do at a high level 1)We need to ask the user to enter numbers and store the result into a variable. 2)We need to repeat this several times 3)As we go, we should store the sum of the numbers they have entered 4)At the end, divide by the total number of numbers they entered. COMP Programming Basics 3

4 First, think of what we need to do at a high level 1)We need to ask the user to enter numbers and store the result into a variable. -->Scanner 2)We need to repeat this several times -->For loop 3)As we go, we should store the sum of the numbers they have entered -->Variable that we keep adding to 4)At the end, divide by the total number of numbers they entered. -->Store how many numbers they enter (additional variable) COMP Programming Basics 4

5 First, think of what we need to do at a high level import java.util.scanner; public class MeanProgram { public static void main(string[] args) { final int TOTAL_NUM = 100; double total = 0; Scanner reader = new Scanner(System.in); for (int i = 0; i < TOTAL_NUM; i++) { double nextnumber = reader.nextdouble(); total += nextnumber; } System.out.println(total / TOTAL_NUM); } COMP Programming Basics 5

6 What if, we needed to store all these numbers? For example, if you were computing the standard deviation of the numbers, you need to store the numbers in order to do the computation. Denote by X the mean of all the values. Then the standard deviation of x1, x2...,x100 is: std(x1,...x100) = sqrt( sum(x_i X)^2 / n-1 ) There is no way to compute this without storing all 100 numbers. How can we do this? COMP Programming Basics 6

7 Option 1 (bad option) Store the 100 numbers into 100 different, unrelated variables. int fml1, fml2, fml3, fml4, fml5, fml6, fml7, fml8, fml9, fml10, fml11, fml12, fml13, fml14, fml15, fml16, fml17, fml18, fml19, fml20, fml21, fml22, fml23, fml24, fml25, fml26, fml27, /*okay I'm tired of typing all these fml's. I need a drink now...*/ fml99, fml100; COMP Programming Basics 7

8 Option 1 (bad option) COMP Programming Basics 8

9 Problems with this? The problems with this are kind of obvious: -Really annoying to write -The only way to understand this is to have several drinks. This means the user will be drunk if he has to do this more than a couple times (well, I guess it's debatable if this is bad :) ) -The user will be hungover the next morning if he has to do this more than a couple times (okay this one's definitely bad) -Easy to make an error (sound familiar?) COMP Programming Basics 9

10 Problems with this? Most importantly, this technique does not take advantage of ANY of the looping techniques we have just used! If we want to store these numbers, we can't use a while loop or a for loop. So we spent time learning this cool thing called loops and now we can't even use it. :( COMP Programming Basics 10

11 COMP Programming Basics 11

12 Part 1: Arrays

13 Array Definition An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. (From tsandbolts/arrays.html) COMP Programming Basics 13

14 Array Definition What this means, is we can store many values of the same type into a single array. This allows us to group related values into a single object Because it is all in one object, we can access the values in the array using the same variable. COMP Programming Basics 14

15 Array type For any type, you can create an array variable of that type, by writing type[] instead of type COMP Programming Basics 15

16 Example public static void main(string[] args) args, is a variable of type String array COMP Programming Basics 16

17 Purpose of array When you create an array, you create an object which contains many values of a specific type. The point is to group together similar pieces of data into one piece of data. COMP Programming Basics 17

18 Examples of array variables String[] classlist; //used to store a list of names int[] grades; /*used to store all the grades of 1 person or 1 grade per student boolean[] wins; //used to store results of a game String[] friendslist; /*used to store a list of friends but only if you are popular! */ COMP Programming Basics 18

19 Creating an array When you create an array, you have to specify how big to make the array. In other words, how many values should the array store. There are 2 ways to do this: COMP Programming Basics 19

20 Initializer List: At the same time as you declare an array variable, you provide a list of the elements to fill in: type[] variablename = {exp1, exp2, exp3}; exp1, exp2, exp3, etc all have to be of type type COMP Programming Basics 20

21 Example: Array of days of the week String[] days = { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; COMP Programming Basics 21

22 Example: Arrays storing hockey results (a small array) int[] rangerswin = {1928, 1933, 1940, 1994, 2012}; int[] habswin = {1916,1924,1930,1931,1944,1946,1953,1956, 1957,1958,1959,1960,1965,1966,1968,1969,1971,19 72, 1973, 1976, 1977, 1978, 1979,1986,1993}; COMP Programming Basics 22

23 More general way... This way only works when you know ahead of time (i.e. as you are typing the code) how many values you want to store in your array. Very often, this is a reasonable assumption: -games in a hockey season -number of countries in North America -the number of wheels on a Hummer COMP Programming Basics 23

24 Sometimes, you don't know Sometimes, you will not know how many values you want to store: Example: -storing a list of books bought in an order from Amazon.com -storing a list of games won by the Montreal Canadiens -... COMP Programming Basics 24

25 new operator (1) In this case, you will use the new operator to create an array and specify how big it is: type[] variablename = new type[size]; This will create an array, referred to by the name variablename. The array will contain size values of type type. COMP Programming Basics 25

26 new operator (2) When you use this approach, you will have to set the values in a different step. It is not necessary to specify the size when you create the variable, but it is necessary when you create the array itself (using new) Scanner scanner = new Scanner(System.in); int numberquestions = scanner.nextint(); String[] jeopardy = new String[numberQuestions]; COMP Programming Basics 26

27 Using an array When you create an array of size n, you are creating n places in memory that can store values of that type. These places in memory are all contiguous. That is, they are all in a row. In addition to storing a value, each place is assigned an index. The index is what you use to access various values. COMP Programming Basics 27

28 boolean[] beatscasino False True False False False False False False False Here is an array with 9 values in it. The casino almost always wins, and indeed 8 values are false. One is true. COMP Programming Basics 28

29 boolean[] beatscasino False True False False False False False False False Each spot of the array has a value and an index. COMP Programming Basics 29

30 boolean[] beatscasino False True False False False False False False False The value is any legitimate value of the type of the array, in this case boolean. COMP Programming Basics 30

31 boolean[] beatscasino False True False False False False False False False The index is an integer. Interestingly the counting starts from 0 instead of 1. COMP Programming Basics 31

32 Accessing values of beatscasino False True False False False False False False False To get or set values of an array, we will use a similar syntax to a normal variable, except we have to specify which value of the array we wish to get or set. We do this by using the index. COMP Programming Basics 32

33 Accessing values of beatscasino False True False False False False False False False System.out.println(beatsCasino[0]); // prints False beatscasino[1] = false; // sets the value at index 1 to be false if (!beatscasino[0] &&!beatscasino[1] ) { System.out.println( LOSER!!! ); } COMP Programming Basics 33

34 Checking if you lost all your games Let's say we want to test if we won any games at the casino. The naïve way to do this, would be to test every single value. (Remember that in a boolean expression it is not necessary to compare the boolean to true) if (beatscasino[0] beatscasino[1] beatscasino[2] beatscasino[3] beatscasino[4] beatscasino[5] beatscasino[6] beatscasino[7] beatscasino[8] ) { System.out.println( You won! ); } else { System.out.println( You're broke! ); } COMP Programming Basics 34

35 Smarter way... If we do this though, we are not taking advantage of the array at all. We are simply treating the 9 values completely separately. What we'd like to do is create a loop that checked all of these values. Each step of the loop will look at one value of the array. What is it that should change at each step? COMP Programming Basics 35

36 For loop : array int size = 9; boolean foundwinner = false; for (int i = 0; i < size; i++) { if (beatscasino[i]) { foundwinner = true; } } if (foundwinner) { System.out.println( You win ); } else { System.out.println( Sucker! ); } COMP Programming Basics 36

37 While loop : array int size = 9; int i = 0; boolean foundwinner = false; while (i < size) { if (beatscasino[i]) { foundwinner = true; } i++; } if (foundwinner) { System.out.println( You win ); } else { System.out.println( Sucker! ); } COMP Programming Basics 37

38 Arrays as Objects An array is a type of Object. An object is a collection of data along with methods/functions to work with the data. We have seen Objects before: String and Scanner In fact, except for the 8 primitive types, all types in Java are Objects. COMP Programming Basics 38

39 String as an Object Recall some of the things we can do with Strings. String university = McGill ; int size = university.length(); String breathingdevices = university.substring(2).tolowercase() + s This is because a String is an Object. In addition to data, a String also has associated methods/functions. You call these methods by writing the name of a variable referring to the Object you are interested in, followed by a dot, followed by the name of the method (and any arguments the method requires) The significance is that the substring method will differ depending on which String the method is called upon! COMP Programming Basics 39

40 Properties of Objects In addition to calling methods on Objects, you can also access properties of Objects. To access a property, you can do it the same way as a method, except there are no ( ) and no arguments inside. In the case of an array, there is just 1 property, and there are no methods. COMP Programming Basics 40

41 Array property : length When you have an array stored into a variable, you can access the length of the array by writing: variablename.length For example: int[] numbers = {1, 5, 9, 11, 3, 4}; int arraysize = numbers.length; This accesses the property called length of the array referred to by the variable arraysize. COMP Programming Basics 41

42 Preview:Reference vs Primitive Types(1) When we write int x = 10; what we are technically doing is the following: 1)Creating a space in memory that will store an int 2)Specifying that whenever we use the identifier x later in our program, we want to access that memory location. 3)Storing the value of 10 into that memory location. COMP Programming Basics 42

43 Preview : References One tricky concept that we will see is that a variable of any Object type, does NOT actually store the DATA inside the variable! It actually stores the address in memory that the data is located. We will see the consequences of this in the next unit, but it can lead to some very weird results! An array is an Object, so that means the variable itself, does not actually store the data! It merely stores the address in memory of the data. COMP Programming Basics 43

44 Preview:Reference vs Primitive Types(2) When we write int x[] = {10, 3, 5}; what we are technically doing is the following: 1)Creating a space in memory that will store the address of an int array. 2)Specifying that whenever we use the identifier x later in our program, we want to access that space in memory (which stores an address). 3)Creating an array somewhere else in memory. That array will store the values 10, 3, and 5 and have length of 3. That array must be located somewhere in memory. Call that address a 4)Setting the value of x to be a. (In other words, store into the space of memory reserved for the variable x, the value of a) COMP Programming Basics 44

45 Consequence of Reference Types There are some interesting consequences of storing addresses into variables instead of the values. int[] x = {1, 2, 3}; int[] y = x; y[0] = 2; System.out.println(x[0]); What do you think the above prints? COMP Programming Basics 45

46 Consequence of Reference Types There are some interesting consequences of storing addresses into variables instead of the values. int[] x = {1, 2, 3}; int[] y = x; y[0] = 2; System.out.println(x[0]); What do you think the above prints? It actually prints 2! Because changing y[0] also changes x[0] COMP Programming Basics 46

47 Step by step: Memory Addresses Values int[] x = {1, 2, 3}; COMP Programming Basics 47

48 Step by step: Memory Addresses Values int[] x = {1, 2, 3}; The {1, 2, 3} creates an array in memory. The address of the start of this spot in memory is COMP Programming Basics 48

49 Step by step: Memory Addresses Values int[] x = {1, 2, 3}; int[] x creates a variable of type int array. This means we create space in memory to store the address of an int array. In this case, we use the storage space in memory of 2000, to store the space 1000 x COMP Programming Basics 49

50 Step by step: Memory Addresses Values int[] x = {1, 2, 3}; int[] y = x; x This means: 1)Create space in memory to store an int[] variable 2)Store into that int[] variable the value of the expression x COMP Programming Basics 50

51 Step by step: Memory Addresses Values int[] x = {1, 2, 3}; int[] y = x; x y This means: 1)Create space in memory to store an int[] variable 2)Store into that int[] variable the value of the expression x COMP Programming Basics 51

52 Step by step: Memory Addresses Values int[] x = {1, 2, 3}; int[] y = x; y[0] = 2; x y This means: 1)Assign to the first value of the array referred to by y, the value of the expression 2 COMP Programming Basics 52

53 Step by step: Memory Addresses Values int[] x = {1, 2, 3}; int[] y = x; y[0] = 2; x y This means: 1)Assign to the first value of the array referred to by y, the value of the expression 1 COMP Programming Basics 53

54 Step by step: Memory Addresses Values int[] x = {1, 2, 3}; int[] y = x; y[0] = 2; System.out.println(x[0]); x y So printing the first value stored in the array referred to by x now will print 2! COMP Programming Basics 54

55 Equality with arrays int[] x = {1,2,3}; int[] y = new int[3]; y[0] = 1; y[1] = 2; y[2] = 3; System.out.println(x == y); Do you think this prints true or false? COMP Programming Basics 55

56 Equality It actually prints false It does this because it is comparing the values of the variables. The value of each variable is just an address though! x and y refer to 2 different arrays which each happen to have the same values, but they are not the same array! This is why when you compare 2 Strings to each other, you should use.equals() instead of == COMP Programming Basics 56

57 Why? Why would they EVER make things so complicated? It turns out there are some very good reasons that will be crucial to the idea of object oriented programming. One useful feature is the ability to pass addresses of data means it's easier to share (and modify!) data between methods! COMP Programming Basics 57

58 Exceptions and Runtime Errors Sometimes, you will try to execute a command that is impossible to do for various reasons. In this case, you will generate an exception or runtime error. Normally, these will cause your program to crash, but we'll see ways that you can avoid your program crashing. COMP Programming Basics 58

59 Ex: Scanning for the wrong kind of value An example of a Runtime Error happens if you try to scan for an int and get a String: dan:~/comp202$ java ScannerTest Enter a number: foo Exception in thread "main" java.util.inputmismatchexception at java.util.scanner.throwfor(scanner.java:819) at java.util.scanner.next(scanner.java:1431) at java.util.scanner.nextint(scanner.java:2040) at java.util.scanner.nextint(scanner.java:2000) at ScannerTest.main(ScannerTest.java:4) COMP Programming Basics 59

60 Information from an Exception Exception in thread "main" java.util.inputmismatchexception at java.util.scanner.throwfor(scanner.java:819) at java.util.scanner.next(scanner.java:1431) at java.util.scanner.nextint(scanner.java:2040) at java.util.scanner.nextint(scanner.java:2000) at ScannerTest.main(ScannerTest.java:4) An Exception contains a lot of information to help you fix the problem: -The type of Exception -The line number and file that the exception occurred in -The stack trace which helps you see which methods called the method that crashed your program COMP Programming Basics 60

61 Divide by Zero Exception Another kind of exception is a divide by zero exception: dan:~/examples daniel$ java DivideByZero I'm going to divide 1 by 0 Exception in thread "main" java.lang.arithmeticexception: / by zero at DivideByZero.main(DivideByZero.java:4) This message contains the same sorts of information. COMP Programming Basics 61

62 Exceptions with arrays When we use arrays, there are 2 kinds of Exceptions that we must be careful to avoid: 1)ArrayIndexOutOfBoundsException 2)NullPointerException COMP Programming Basics 62

63 ArrayIndexOutOfBounds An ArrayIndexOutOfBounds exception occurs when you try to access an invalid index. This happens when you use an index < 0 or >= the length of the array: Ex: int[] x = {1, 2, 3}; System.out.println( x[-1] ); COMP Programming Basics 63

64 ArrayIndexOutOfBounds dan:~/examples daniel$ java ArrayOOB Exception in thread "main" java.lang.arrayindexoutofboundsexception: -1 at ArrayOOB.main(DivideByZero.java:4) The information in an ArrayIndexOutOfBoundsException is very similar to the other Exceptions. One useful thing is in addition to the stacktrace and line number, it provides the index that caused the ArrayIndexOutOfBoundsException (in this case -1) This is really useful when you have a complicated statement such as x[y/2*z]; COMP Programming Basics 64

65 NullPointerException The other kind of exception you get is a NullPointerException. This happens when you have an array variable but it is not actually assigned to an array! For example: int[] x; //creates a variable which can store the address of an int[] System.out.println(x.length) Because x does not actually contains the address of an array yet, it has no length. This causes a NullPointerException! Sometimes, the compiler will catch the error, but not always. COMP Programming Basics 65

66 Preview: Variables vs Objects Because variables of types that are not primitive store addresses, this means that we can create a variable but not actually an Object. There are thus three different steps: 1)Create a variable which could store the address of an Object 2)Create an Object 3)Link the variable to the Object COMP Programming Basics 66

67 Multidimensional Arrays So far, the elements of all the arrays we have seen have been simple values: primitive types or Strings Such arrays are one-dimensional However, we can create arrays whose elements are themselves one-dimensional arrays Such an array is really an array of arrays These arrays are two-dimensional arrays (or 2D arrays) COMP Arrays 67

68 Two-Dimensional Arrays Elements in a two dimensional array are accessed using two indices The first index specifies the one-dimensional array containing the element we want to access The second index specifies the element we want to access within the one-dimensional array specified by the first index COMP Arrays 68

69 Creating a 2d array Like 1d arrays, there are 2 ways to create a 2d array: 1)Initializer list 2)Using the new operator. COMP Arrays 69

70 Initializer List We said to create an array using an initializer list, you could write: type[] variablename = {exp1, exp2, exp3,...} where exp1, exp2, exp3, etc all evaluated to type Well, in this case type is just an array: COMP Arrays 70

71 Initializer List int[][] board = { {1,2,3}, {1,4}, {6,2} } This is creating an array of int[]. The first array in the array is {1,2,3} the second is {1,4} and the third is {6,2} COMP Arrays 71

72 Using the new keyword There are two ways to create a 2d array using the new keyword: 1)type[][] variablename= new type[size_1][size_2]; This creates a rectangular array. You can then fill in values or access values using 2 indices. COMP Arrays 72

73 Accessing values of a 2d array int[][] x = new int[3][4]; //creates an array which contains 3 arrays of size 4. x[0][1] = 3; //assigns to the 2 nd spot of the 1 st array, the value 3; x[2][3] = 10; //assigns to the 4 th spot of the 3 rd array, the value of 10; x[10][2] = 5; //causes an array out of bounds exception int[] y = x[2]; //stores into y the address of the 3 rd array COMP Arrays 73

74 Using the new keyword The second way to use the new keyword with 2d arrays is less commonly used but also possible. Since it is an array of arrays, you can create each array separately: COMP Arrays 74

75 Using the new keyword int[][] x = new int[4][]; //creates an array which will store 4 arrays x[0] = new int[5]; /*creates an array of size 5 and puts it into the first spot of the array x */ x[2] = new int[3]; /*creats an array of size 3 and puts it into the 3 rd spot of the array x */ COMP Arrays 75

76 Higher dimensional arrays You can actually create arrays of even more dimensions: int[][][][][][][][][][] x = new int[2][2][2][2][2][2][2][2][2][2]; creates a 10 dimensional array! COMP Arrays 76

77 Arrays and Strings in loops Both arrays and Strings translate very well to loop operations. We will see some examples in class of this. COMP Arrays 77

78 Useful String methods The following are some useful methods you can use to operate on a String. For all of the following, you have to provide an example of a String to call the method on. (e.g. foo.length() where foo is a variable of the type String). All of these return values without modifying the original String.length() - gets the length of a String.charAt(int i) gets the ith symbol of the String (count from 0).substring(a,b) gets String consisting of from a of the original up to but not including a+b.equals(string other) compare two Strings values to each other (since == compares addresses).compareto(string other) tells you which String is larger.indexof(char c) returns the index of the first instance of c.tolowercase() - returns a lowercase version of String COMP Arrays 78

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

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015 Announcements Assignment 2 posted and due on 30 th of May (23:30). Extra class tomorrow

More information

COMP-202 Unit 9: Exceptions

COMP-202 Unit 9: Exceptions COMP-202 Unit 9: Exceptions Announcements - Assignment 4: due Monday April 16th - Assignment 4: tutorial - Final exam tutorial next week 2 Exceptions An exception is an object that describes an unusual

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

COMP-202 Unit 9: Exceptions

COMP-202 Unit 9: Exceptions COMP-202 Unit 9: Exceptions Course Evaluations Please do these. -Fast to do -Used to improve course for future. (Winter 2011 had 6 assignments reduced to 4 based on feedback!) 2 Avoiding errors So far,

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

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

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

COMP-202 Unit 4: Programming with Iterations

COMP-202 Unit 4: Programming with Iterations COMP-202 Unit 4: Programming with Iterations Doing the same thing again and again and again and again and again and again and again and again and again... CONTENTS: While loops Class (static) variables

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

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016 COMP-202: Foundations of Programming Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016 Review What is the difference between a while loop and an if statement? What is an off-by-one

More information

Last Class. While loops Infinite loops Loop counters Iterations

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

More information

COMP-202 More Complex OOP

COMP-202 More Complex OOP COMP-202 More Complex OOP Defining your own types: Remember that we can define our own types/classes. These classes are objects and have attributes and behaviors You create an object or an instance of

More information

COMP-202: Foundations of Programming. Lecture 9: Arrays and Practice Jackie Cheung, Winter 2016

COMP-202: Foundations of Programming. Lecture 9: Arrays and Practice Jackie Cheung, Winter 2016 COMP-202: Foundations of Programming Lecture 9: Arrays and Practice Jackie Cheung, Winter 2016 Review: for Loops for (initialization; condition; update) { Happens once per loop only, before the first check

More information

Controls Structure for Repetition

Controls Structure for Repetition Controls Structure for Repetition So far we have looked at the if statement, a control structure that allows us to execute different pieces of code based on certain conditions. However, the true power

More information

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015 COMP-202: Foundations of Programming Lecture 7: Strings and Methods Jackie Cheung, Winter 2015 Announcements Quiz 2 due tonight at 11:59pm New grades policy: lowest quiz mark dropped Assignment 2 due in

More information

Primitive vs Reference

Primitive vs Reference Primitive vs Reference Primitive types store values Reference types store addresses This is the fundamental difference between the 2 Why is that important? Because a reference type stores an address, you

More information

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics COMP-202 Unit 1: Introduction Announcements Did you miss the first lecture? Come talk to me after class. If you want

More information

Warm-Up: COMP Programming with Iterations 1

Warm-Up: COMP Programming with Iterations 1 Warm-Up: Suppose I have a method with header: public static boolean foo(boolean a,int b) { if (a) return true; return b > 0; Which of the following are valid ways to call the method and what is the result?

More information

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Michele Van Dyne Museum 204B  CSCI 136: Fundamentals of Computer Science II, Spring Michele Van Dyne Museum 204B mvandyne@mtech.edu http://katie.mtech.edu/classes/csci136 CSCI 136: Fundamentals of Computer Science II, Spring 2016 1 Review of Java Basics Data Types Arrays NEW: multidimensional

More information

EXCEPTIONS. Fundamentals of Computer Science I

EXCEPTIONS. Fundamentals of Computer Science I EXCEPTIONS Exception in thread "main" java.lang.numberformatexception: For input string: "3.5" at java.lang.numberformatexception.forinputstring(numberformatexception.java:48) at java.lang.integer.parseint(integer.java:458)

More information

BBM 102 Introduction to Programming II Spring Exceptions

BBM 102 Introduction to Programming II Spring Exceptions BBM 102 Introduction to Programming II Spring 2018 Exceptions 1 Today What is an exception? What is exception handling? Keywords of exception handling try catch finally Throwing exceptions throw Custom

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

More information

Garbage Collection (1)

Garbage Collection (1) Coming up: Today: Finish unit 6 (garbage collection) start ArrayList and other library objects Wednesday: Complete ArrayList, basics of error handling Friday complete error handling Next week: Recursion

More information

Computer Science is...

Computer Science is... Computer Science is... Computational complexity theory Complexity theory is the study of how algorithms perform with an increase in input size. All problems (like is n a prime number? ) fit inside a hierarchy

More information

4. Java Project Design, Input Methods

4. Java Project Design, Input Methods 4-1 4. Java Project Design, Input Methods Review and Preview You should now be fairly comfortable with creating, compiling and running simple Java projects. In this class, we continue learning new Java

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

CS S-08 Arrays and Midterm Review 1

CS S-08 Arrays and Midterm Review 1 CS112-2012S-08 Arrays and Midterm Review 1 08-0: Arrays ArrayLists are not part of Java proper 08-1: Arrays Library class Created using lower-level Java construct: Array Arrays are like a stripped-down

More information

Topic 5: Enumerated Types and Switch Statements

Topic 5: Enumerated Types and Switch Statements Topic 5: Enumerated Types and Switch Statements Reading: JBD Sections 6.1, 6.2, 3.9 1 What's wrong with this code? if (pressure > 85.0) excesspressure = pressure - 85.0; else safetymargin = 85.0 - pressure;!

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

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Topic 4 Expressions and variables

Topic 4 Expressions and variables Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides

More information

CS 251 Intermediate Programming Java Basics

CS 251 Intermediate Programming Java Basics CS 251 Intermediate Programming Java Basics Brooke Chenoweth University of New Mexico Spring 2018 Prerequisites These are the topics that I assume that you have already seen: Variables Boolean expressions

More information

Object Oriented Programming Exception Handling

Object Oriented Programming Exception Handling Object Oriented Programming Exception Handling Budditha Hettige Department of Computer Science Programming Errors Types Syntax Errors Logical Errors Runtime Errors Syntax Errors Error in the syntax of

More information

Warmup : Name that tune!

Warmup : Name that tune! Warmup : Name that tune! Write, using a loop, Java code to print the lyrics to the song 99 Bottles of Beer on the Wall 99 bottles of beer on the wall. 99 bottles of beer. Take one down, pass it around,

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

CS159. Nathan Sprague

CS159. Nathan Sprague CS159 Nathan Sprague What s wrong with the following code? 1 /* ************************************************** 2 * Return the mean, or -1 if the array has length 0. 3 ***************************************************

More information

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

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

More information

BBM 102 Introduction to Programming II Spring 2017

BBM 102 Introduction to Programming II Spring 2017 BBM 102 Introduction to Programming II Spring 2017 Exceptions Instructors: Ayça Tarhan, Fuat Akal, Gönenç Ercan, Vahid Garousi Today What is an exception? What is exception handling? Keywords of exception

More information

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015 COMP-202: Foundations of Programming Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015 Assignment Due Date Assignment 1 is now due on Tuesday, Jan 20 th, 11:59pm. Quiz 1 is

More information

6.001 Notes: Section 15.1

6.001 Notes: Section 15.1 6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define

More information

Arrays. Eng. Mohammed Abdualal

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

More information

Programming - 2. Common Errors

Programming - 2. Common Errors Common Errors There are certain common errors and exceptions which beginners come across and find them very annoying. Here we will discuss these and give a little explanation of what s going wrong and

More information

Java Errors and Exceptions. Because Murphy s Law never fails

Java Errors and Exceptions. Because Murphy s Law never fails Java Errors and Exceptions Because Murphy s Law never fails 1 Java is the most distressing thing to hit computing since MS-DOS. Alan Kay 2 Corresponding Book Sections Pearson Custom Computer Science: Chapter

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

CSCI 261 Computer Science II

CSCI 261 Computer Science II CSCI 261 Computer Science II Department of Mathematics and Computer Science Lecture 2 Exception Handling New Topic: Exceptions in Java You should now be familiar with: Advanced object-oriented design -

More information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional) COMP-202 Unit 7: More Advanced OOP CONTENTS: ArrayList HashSet (Optional) HashMap (Optional) Managing a big project Many times, you will need to use an Object type that someone else has created. For example,

More information

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 Announcements Midterm Exams on 4 th of June (12:35 14:35) Room allocation will be announced soon

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

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

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

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7 CPS109 Course Notes 7 Alexander Ferworn Unrelated Facts Worth Remembering The most successful people in any business are usually the most interesting. Don t confuse extensive documentation of a situation

More information

Announcements. 1. Forms to return today after class:

Announcements. 1. Forms to return today after class: Announcements Handouts (3) to pick up 1. Forms to return today after class: Pretest (take during class later) Laptop information form (fill out during class later) Academic honesty form (must sign) 2.

More information

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class? Administration Objects and Arrays CS 99 Summer 2000 Michael Clarkson Lecture 6 Read clarified grading policies Lab 6 due tomorrow Submit.java files in a folder named Lab6 Lab 7 Posted today Upson Lab closed

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

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

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

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

More information

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Building Java Programs. Chapter 2: Primitive Data and Definite Loops Building Java Programs Chapter 2: Primitive Data and Definite Loops Copyright 2008 2006 by Pearson Education 1 Lecture outline data concepts Primitive types: int, double, char (for now) Expressions: operators,

More information

Chapter 3. Selections

Chapter 3. Selections Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of

More information

What did we talk about last time? Course overview Policies Schedule

What did we talk about last time? Course overview Policies Schedule Week 1 - Wednesday What did we talk about last time? Course overview Policies Schedule The book talks about stuff that you know pretty well as Java programmers I just want to talk about a few issues

More information

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

Chapter 3. Ch 1 Introduction to Computers and Java. Selections Chapter 3 Ch 1 Introduction to Computers and Java Selections 1 The if-else Statement 2 Flow Chart Deconstructed Decision: Make a choice Start Terminator: Show start/stop points T boolean expression F true

More information

Introducing arrays. Week 4: arrays and strings. Creating arrays. Declaring arrays. Initialising arrays. Declaring and creating in one step

Introducing arrays. Week 4: arrays and strings. Creating arrays. Declaring arrays. Initialising arrays. Declaring and creating in one step School of Computer Science, University of Birmingham Java Lecture notes. M. D. Ryan. September 2001. Introducing arrays Week 4: arrays and strings Arrays: a data structure used to store multiple data,

More information

Example Program. public class ComputeArea {

Example Program. public class ComputeArea { COMMENTS While most people think of computer programs as a tool for telling computers what to do, programs are actually much more than that. Computer programs are written in human readable language for

More information

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling Course Name: Advanced Java Lecture 5 Topics to be covered Exception Handling Exception HandlingHandlingIntroduction An exception is an abnormal condition that arises in a code sequence at run time A Java

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Conditional Statements Boolean Expressions and Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Conditional Statements Boolean Expressions and Methods Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu

More information

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige CSC 308 2.0 System Development with Java Exception Handling Department of Statistics and Computer Science 1 2 Errors Errors can be categorized as several ways; Syntax Errors Logical Errors Runtime Errors

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

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics WIT COMP1000 Java Basics Java Origins Java was developed by James Gosling at Sun Microsystems in the early 1990s It was derived largely from the C++ programming language with several enhancements Java

More information

4. Java language basics: Function. Minhaeng Lee

4. Java language basics: Function. Minhaeng Lee 4. Java language basics: Function Minhaeng Lee Review : loop Program print from 20 to 10 (reverse order) While/for Program print from 1, 3, 5, 7.. 21 (two interval) Make a condition that make true only

More information

CSCI 1103: File I/O, Scanner, PrintWriter

CSCI 1103: File I/O, Scanner, PrintWriter CSCI 1103: File I/O, Scanner, PrintWriter Chris Kauffman Last Updated: Wed Nov 29 13:22:24 CST 2017 1 Logistics Reading from Eck Ch 2.1 on Input, File I/O Ch 11.1-2 on File I/O Goals Scanner for input

More information

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015 Announcements Slides will be posted before the class. There might be few

More information

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print): Name (print): INSTRUCTIONS: o Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. o Do NOT communicate with anyone other than the professor/proctor for ANY reason

More information

Final Exam Practice. Partial credit will be awarded.

Final Exam Practice. Partial credit will be awarded. Please note that this problem set is intended for practice, and does not fully represent the entire scope covered in the final exam, neither the range of the types of problems that may be included in the

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

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 45

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables COMP-202 Unit 2: Java Basics CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables Part 1: Printing to the Screen Recall: HelloWorld public class

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

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software. CISC-124 20180129 20180130 20180201 This week we continued to look at some aspects of Java and how they relate to building reliable software. Multi-Dimensional Arrays Like most languages, Java permits

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

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

1.3 Conditionals and Loops

1.3 Conditionals and Loops 1 1.3 Conditionals and Loops any program you might want to write objects functions and modules graphics, sound, and image I/O arrays conditionals and loops to infinity and beyond Math primitive data types

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 Copyright 2009 by Pearson Education Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 Copyright

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

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events CS1020 Data Structures and Algorithms I Lecture Note #8 Exceptions Handling exceptional events Objectives Understand how to use the mechanism of exceptions to handle errors or exceptional events that occur

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

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing. CISC-124 20180115 20180116 20180118 We continued our introductory exploration of Java and object-oriented programming by looking at a program that uses two classes. We created a Java file Dog.java and

More information

CEN 414 Java Programming

CEN 414 Java Programming CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

More information

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11 Administration Exceptions CS 99 Summer 2000 Michael Clarkson Lecture 11 Lab 10 due tomorrow No lab tomorrow Work on final projects Remaining office hours Rick: today 2-3 Michael: Thursday 10-noon, Monday

More information

Programming assignment A

Programming assignment A Programming assignment A ASCII Minesweeper Official release on Feb 14 th at 1pm (Document may change before then without notice) Due 5pm Feb 25 th Minesweeper is computer game that was first written in

More information

COMP-202 Unit 8: Recursion. CONTENTS: Recursion in Java More complex examples

COMP-202 Unit 8: Recursion. CONTENTS: Recursion in Java More complex examples COMP-202 Unit 8: Recursion CONTENTS: Recursion in Java More complex examples Recursion To understand recursion, you must first understand recursion. 2 Recursion 3 Recursion 4 Silly Definition recursion

More information

LOOPS. Repetition using the while statement

LOOPS. Repetition using the while statement 1 LOOPS Loops are an extremely useful feature in any programming language. They allow you to direct the computer to execute certain statements more than once. In Python, there are two kinds of loops: while

More information

COMP 110 Programming Exercise: Simulation of the Game of Craps

COMP 110 Programming Exercise: Simulation of the Game of Craps COMP 110 Programming Exercise: Simulation of the Game of Craps Craps is a game of chance played by rolling two dice for a series of rolls and placing bets on the outcomes. The background on probability,

More information

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

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

More information

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling CS115 Pi Principles i of fcomputer Science Chapter 17 Exception Handling Prof. Joe X. Zhou Department of Computer Science CS115 ExceptionHandling.1 Objectives in Exception Handling To know what is exception

More information

COMP-202 Unit 6: The Basics of Object Oriented Programming. CONTENTS: Organizing data and methods into Objects Reference types

COMP-202 Unit 6: The Basics of Object Oriented Programming. CONTENTS: Organizing data and methods into Objects Reference types COMP-202 Unit 6: The Basics of Object Oriented Programming CONTENTS: Organizing data and methods into Objects Reference types Example: Creating a first person shooter 2 What do we need to store? -double

More information

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012) COE318 Lecture Notes: Week 3 1 of 8 COE318 Lecture Notes Week 3 (Week of Sept 17, 2012) Announcements Quiz (5% of total mark) on Wednesday, September 26, 2012. Covers weeks 1 3. This includes both the

More information

CS 113 PRACTICE FINAL

CS 113 PRACTICE FINAL CS 113 PRACTICE FINAL There are 13 questions on this test. The value of each question is: 1-10 multiple choice (4 pt) 11-13 coding problems (20 pt) You may get partial credit for questions 11-13. If you

More information

6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

More information

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Lesson 10: Quiz #1 and Getting User Input (W03D2) Lesson 10: Quiz #1 and Getting User Input (W03D2) Balboa High School Michael Ferraro September 1, 2015 1 / 13 Do Now: Prep GitHub Repo for PS #1 You ll need to submit the 5.2 solution on the paper form

More information