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

Size: px
Start display at page:

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

Transcription

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

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

3 Grades for COS-236 as of

4 Grades for COS-236 as of

5 5

6 Review of Quiz 19 User enters the number of test scores and then is prompted to enter each test score Program calculates the mean, median, high and low score The Weather Program of Lecture 18, slide 42 does most of what is needed. We need only to replace the day s above average loop with the calculation of low, high and median 6

7 Review of Quiz 19 The Weather Program: // Reads temperatures from the user, computes average and # days above average. import java.util.*; public class Weather { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int days = console.nextint(); double[] temps = new double[days]; // array to store days' temperatures double sum = 0; for (int i = 0; i < days; i++) { // read/store each day's temperature System.out.print("Day " + (i + 1) + "'s high temp: "); temps[i] = console.nextdouble(); sum += temps[i]; double average = sum / days; int Arrays.sort(temps); count = 0; // see if each day is above average for (int i = 0; i < days; i++) { Replace double median = 0; if (temps.length if (temps[i] > % average) 2 == 1) {median { = temps[temps.length/2]; else { count++; this median = (temps[temps.length/2] + temps[temps.length/2-1])/2; // report results System.out.printf("Average High Score: temp = %6.2f\n %.1f\n", Mean average); Score: %6.2f\n", temps[temps.length-1], average); System.out.println(count System.out.printf("Median + Score: " days %6.2f\n above average"); Low Score: %6.2f\n", median, temps[0]); 7

8 Review of Quiz 19 The Weather Program: // Reads temperatures from the user, computes average and # days above average. import java.util.*; public class Weather { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int days = console.nextint(); double[] temps = new double[days]; // array to store days' temperatures double sum = 0; for (int i = 0; i < days; i++) { // read/store each day's temperature System.out.print("Day " + (i + 1) + "'s high temp: "); temps[i] = console.nextdouble(); sum += temps[i]; double average = sum / days; int Arrays.sort(temps); count = 0; // see if each day is above average for double (intmedian i = 0; = i 0; < days; i++) { if (temps.length if (temps[i] > % average) 2 == 1) {median { = temps[temps.length/2]; else { count++; median = (temps[temps.length/2] + temps[temps.length/2-1])/2; // report results System.out.printf("Average High Score: temp = %6.2f\n %.1f\n", Mean average); Score: %6.2f\n", temps[temps.length-1], average); System.out.println(count System.out.printf("Median + Score: " days %6.2f\n above average"); Low Score: %6.2f\n", median, temps[0]); 8

9 Review of Quiz 19 OUTPUT: We need only to modify the prompts to get the desired output 9

10 Review of Quiz 19 // Reads test scores and calculates high, low, mean and median import java.util.*; Complete Program: public class Quiz19 { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How many test scores do you wish to enter? "); int number = console.nextint(); double[] scores = new double[number]; // array to store scores double sum = 0; for (int i = 0; i < number; i++) { // read/store each score System.out.print("Please enter test " + (i + 1) + "'s score: "); scores[i] = console.nextdouble(); sum += scores[i]; double mean = sum / number; Arrays.sort(scores); double high = scores[number-1]; double low = scores[0]; double median = 0; if(number%2 == 0) { median = (scores[number/2] + scores[number/2-1])/2; else { median = scores[number/2]; // report results System.out.printf("Test Statistics:\n"); System.out.printf(" High Score: %6.2f\n", high); System.out.printf(" Mean Score: %6.2f\n", mean); System.out.printf(" Median Score: %6.2f\n", median); System.out.printf(" Low Score: %6.2f\n", low); 10

11 Review of Quiz 19 // Reads test scores and calculates high, low, mean and median import java.util.*; Complete Program: public class Quiz19 { public static void main(string[] args) { Scanner console = new Scanner(System.in); System.out.print("How many test scores do you wish to enter? "); int number = console.nextint(); double[] scores = new double[number]; // array to store scores double sum = 0; for (int i = 0; i < number; i++) { // read/store each score System.out.print("Please enter test " + (i + 1) + "'s score: "); scores[i] = console.nextdouble(); sum += scores[i]; double mean = sum / number; Arrays.sort(scores); double high = scores[number-1]; double low = scores[0]; double median = 0; if(number%2 == 0) { median = (scores[number/2] + scores[number/2-1])/2; else { median = scores[number/2]; // report results System.out.printf("Test Statistics:\n"); System.out.printf(" High Score: %6.2f\n", high); System.out.printf(" Mean Score: %6.2f\n", mean); System.out.printf(" Median Score: %6.2f\n", median); System.out.printf(" Low Score: %6.2f\n", low); 11

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

13 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) In Java, arrays are objects. We have been using objects since Chapter 3 We haven t yet discussed in detail how they are stored However, we have illustrated the storage differently: Primitive Object 13

14 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) Storing Primitives vs Objects. When we store a primitive (eg: x = 8;) the variable stores the actual data: When we store an object (eg: int[] list = new int[5];) the variable list stores a reference to where the data is actually stored: 14

15 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) Storing Primitives vs Objects. The difference between value (primitives) and reference (objects) makes a slight difference in the efficiency of storage But the real significant difference is in how data is stored One example is what happens when we call a method with a primitive vs and object 15

16 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) Storing Primitives vs Objects in Methods: Let s look at a simple method that takes two parameters: The primitive x The object list[] The method then returns a primitive (int) 16

17 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) import java.util.*; public class Lecture20S16 { public static void main(string[] args) { int x = 0; Storing Primitives vs Objects in Methods: Let s look at a simple method that takes two int[] list = new int[1]; parameters: int y = valref(x, list); System.out.printf("x = %d, list[0] = %d, y = %d\n", x, list[0], y); public static int valref(int x, int[] list) { The primitive x x The = 8; object list[] list[0] = 8; The return method x; then returns a primitive (int) 17

18 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) import java.util.*; public class Lecture20S16 { public static void main(string[] args) { int x = 0; Storing Primitives vs Objects in Methods: Let s look at a simple method that takes two int[] list = new int[1]; parameters: int y = valref(x, list); System.out.printf("x = %d, list[0] = %d, y = %d\n", x, list[0], y); public static int valref(int x, int[] list) { The primitive x x The = 8; object list[] list[0] = 8; In the main program: Set integer x to zero The return method x; then returns a primitive (int) 18

19 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) import java.util.*; public class Lecture20S16 { public static void main(string[] args) { int x = 0; Storing Primitives vs Objects in Methods: Let s look at a simple method that takes two int[] list = new int[1]; parameters: int y = valref(x, list); System.out.printf("x = %d, list[0] = %d, y = %d\n", x, list[0], y); public static int valref(int x, int[] list) { In the main program: Set integer x to zero The primitive x x The = 8; object list[] list[0] = 8; Set integer y to result of calling method valref The return method x; then returns a primitive (int) Print x, list[0], and y Set object list[] to zero 19

20 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) import java.util.*; public class Lecture20S16 { public static void main(string[] args) { int x = 0; Storing Primitives vs Objects in Methods: Let s look at a simple method that takes two int[] list = new int[1]; parameters: int y = valref(x, list); System.out.printf("x = %d, list[0] = %d, y = %d\n", x, list[0], y); public static int valref(int x, int[] list) { The primitive x x The = 8; object list[] list[0] = 8; In the method valref: Set x to 8 Set list[0] to 8 Return x = 8 The return method x; then returns a primitive (int) 20

21 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) import java.util.*; public class Lecture20S16 { public static void main(string[] args) { int x = 0; Storing Primitives vs Objects in Methods: Let s look at a simple method that takes two int[] list = new int[1]; parameters: int y = valref(x, list); System.out.printf("x = %d, list[0] = %d, y = %d\n", x, list[0], y); public static int valref(int x, int[] list) { The primitive x x The = 8; object list[] list[0] = 8; In the method valref: Set x to 8 Set list[0] to 8 Return x = 8 The return method x; then returns a primitive (int) 21

22 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) import java.util.*; public class Lecture20S16 { public static void main(string[] args) { int x = 0; Storing Primitives vs Objects in Methods: Let s look at a simple method that takes two int[] list = new int[1]; parameters: int y = valref(x, list); System.out.printf("x = %d, list[0] = %d, y = %d\n", x, list[0], y); public static int valref(int x, int[] list) { The primitive x x The = 8; object list[] list[0] = 8; Since x is passed to the method by value, changing x to 8 in the method has no effect on x in the main program The return method x; then returns a primitive (int) 22

23 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) import java.util.*; public class Lecture20S16 { public static void main(string[] args) { int x = 0; Storing Primitives vs Objects in Methods: Let s look at a simple method that takes two int[] list = new int[1]; parameters: int y = valref(x, list); System.out.printf("x = %d, list[0] = %d, y = %d\n", x, list[0], y); public static int valref(int x, int[] list) { The primitive x x The = 8; object list[] list[0] = 8; However, list[0] is passed by reference, so changing list[0] in the method also changes list[0] in the main program The return method x; then returns a primitive (int) 23

24 7.3 Reference Semantics (Lecture 20) Reference vs Value (pp ) import java.util.*; public class Lecture20S16 { public static void main(string[] args) { int x = 0; Storing Primitives vs Objects in Methods: Let s look at a simple method that takes two int[] list = new int[1]; parameters: int y = valref(x, list); System.out.printf("x = %d, list[0] = %d, y = %d\n", x, list[0], y); public static int valref(int x, int[] list) { The primitive x x The = 8; object list[] list[0] = 8; Since we return the value x=8 from the method to the main program, y will contain the value of x in the method (x=0) The return method x; then returns a primitive (int) 24

25 Reference semantics (pp ) Let s explore more deeply value vs reference 25 25

26 A swap method? (pp ) Does the following swap method work? Why or why not? public static void main(string[] args) { int a = 7; int b = 35; // swap a with b? swap(a, b); System.out.println(a + " " + b); public static void swap(int a, int b) { int temp = a; a = b; b = temp; 26

27 Value semantics (pp ) value semantics: Behavior where values are copied when assigned, passed as parameters, or returned. All primitive types in Java use value semantics. When one variable is assigned to another, its value is copied. Modifying the value of one variable does not affect others. int x = 5; int y = x; // x = 5, y = 5 y = 17; // x = 5, y = 17 x = 8; // x = 8, y = 17 27

28 Reference semantics (multiple objects pp ) reference semantics: Behavior where variables actually store the address of an object in memory. When one variable is assigned to another, the object is not copied; both variables refer to the same object. Modifying the value of one variable will affect others. int[] a1 = {4, 15, 8; int[] a2 = a1; // refer to same array as a1 a2[0] = 7; System.out.println(Arrays.toString(a1)); // [7, 15, 8] a1 index value a2 28

29 References and objects (pp ) Arrays and objects use reference semantics. Why? efficiency. Copying large objects slows down a program. sharing. It's useful to share an object's data among methods. DrawingPanel panel1 = new DrawingPanel(80, 50); DrawingPanel panel2 = panel1; // same window panel2.setbackground(color.cyan); panel1 panel2 29

30 Objects as parameters (pp ) When an object is passed as a parameter, the object is not copied. The parameter refers to the same object. If the parameter is modified, it will affect the original object. public static void main(string[] args) { DrawingPanel window = new DrawingPanel(80, 50); window.setbackground(color.yellow); example(window); window public static void example(drawingpanel panel) { panel.setbackground(color.cyan);... panel 30

31 Arrays pass by reference (pp ) Arrays are passed as parameters by reference. Changes made in the method are also seen by the caller. public static void main(string[] args) { int[] iq = {126, 167, 95; increase(iq); System.out.println(Arrays.toString(iq)); public static void increase(int[] a) { for (int i = 0; i < a.length; i++) { a[i] = a[i] * 2; Output: [252, 334, 190] a iq index value

32 Array reverse question 2 (pp ) Turn your array reversal code into a reverse method. Accept the array of integers to reverse as a parameter. int[] numbers = {11, 42, -5, 27, 0, 89; reverse(numbers); Solution: public static void reverse(int[] numbers) { for (int i = 0; i < numbers.length / 2; i++) { int temp = numbers[i]; numbers[i] = numbers[numbers.length i]; numbers[numbers.length i] = temp; 32

33 Array parameter questions (pp ) Write a method swap that accepts an array of integers and two indexes and swaps the elements at those indexes. int[] a1 = {12, 34, 56; swap(a1, 1, 2); System.out.println(Arrays.toString(a1)); // [12, 56, 34] Write a method swapall that accepts two arrays of integers as parameters and swaps their entire contents. Assume that the two arrays are the same length. int[] a1 = {12, 34, 56; int[] a2 = {20, 50, 80; swapall(a1, a2); System.out.println(Arrays.toString(a1)); // [20, 50, 80] System.out.println(Arrays.toString(a2)); // [12, 34, 56] 33

34 Array parameter answers (pp ) // Swaps the values at the given two indexes. public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; // Swaps the entire contents of a1 with those of a2. public static void swapall(int[] a1, int[] a2) { for (int i = 0; i < a1.length; i++) { int temp = a1[i]; a1[i] = a2[i]; a2[i] = temp; 34

35 Array return question (pp ) Write a method merge that accepts two arrays of integers and returns a new array containing all elements of the first array followed by all elements of the second. int[] a1 = {12, 34, 56; int[] a2 = {7, 8, 9, 10; int[] a3 = merge(a1, a2); System.out.println(Arrays.toString(a3)); // [12, 34, 56, 7, 8, 9, 10] Write a method merge3 that merges 3 arrays similarly. int[] a1 = {12, 34, 56; int[] a2 = {7, 8, 9, 10; int[] a3 = {444, 222, -1; int[] a4 = merge3(a1, a2, a3); System.out.println(Arrays.toString(a4)); // [12, 34, 56, 7, 8, 9, 10, 444, 222, -1] 35

36 Array return answer 1 (pp ) // Returns a new array containing all elements of a1 // followed by all elements of a2. public static int[] merge(int[] a1, int[] a2) { int[] result = new int[a1.length + a2.length]; for (int i = 0; i < a1.length; i++) { result[i] = a1[i]; for (int i = 0; i < a2.length; i++) { result[a1.length + i] = a2[i]; return result; 36

37 Array return answer 2 (pp ) // Returns a new array containing all elements of a1,a2,a3. public static int[] merge3(int[] a1, int[] a2, int[] a3) { int[] a4 = new int[a1.length + a2.length + a3.length]; for (int i = 0; i < a1.length; i++) { a4[i] = a1[i]; for (int i = 0; i < a2.length; i++) { a4[a1.length + i] = a2[i]; for (int i = 0; i < a3.length; i++) { a4[a1.length + a2.length + i] = a3[i]; return a4; // Shorter version that calls merge. public static int[] merge3(int[] a1, int[] a2, int[] a3) { return merge(merge(a1, a2), a3); 37

38 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Arrays of Objects (pp ) Command-Line Arguments (pp ) Nested Loop Algorithms (pp ) 38

39 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the left: 39

40 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the left: We need to save the first value in a temporary location Then we need to shift each element of the array to the left 40

41 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the left: We need to save the first value in a temporary location Then we need to shift each element of the array to the left Finally, we need to take the temporarily stored value and replace the right-most place with the element originally in the left-most place 41

42 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the left: public static void rotateleft(int[] list) { int first = list[0]; for (int i = 0; i < list.length 1; i++) { list[i] = list[i + 1]; list[list.length 1] = first; 42

43 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the right: Start at the end of the array Save the last (right most) value in temporary storage int last = list[list.length 1]; Copy the shift from the top to the bottom of the array for (int i = list.length 1; i >= 1; i ) { list[i] = list[i 1]; Replace the list[0] with last list[0] = last; 43

44 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the right: Save last last 44

45 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the right: Save last Shift 3 to 4 last 45

46 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the right: Save last Shift 3 to 4 Shift 2 to 3 last 46

47 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the right: Save last Shift 3 to 4 Shift 2 to 3 Shift 1 to 2 last 47

48 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the right: Save last Shift 3 to 4 Shift 2 to 3 Shift 1 to 2 Shift 0 to 1 last 48

49 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the right: Save last last Shift 3 to 4 Shift 2 to 3 Shift 1 to 2 Shift 0 to 1 Shift last to 0 49

50 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Rotating an array one place to the right: public static void rotateright(int[] list) { int last = list[list.length 1]; for (int i = list.length 1; i >= 1; i ) { list[i] = list[i 1]; list[0] = last; 50

51 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Arrays of Objects (pp ) Command-Line Arguments (pp ) Nested Loop Algorithms (pp ) 51

52 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Arrays of Objects (pp ) 52

53 7.4 Advanced Array Techniques (Lecture 20) Arrays of Objects (pp ) All of the arrays we have looked at so far have stored primitive values like simple int values However, you can have arrays of any Java type. Arrays of objects behave slightly differently, though, because objects are stored as references rather than as data values. Constructing an array of objects is usually a two-step process: First construct the array Then construct the individual objects 53

54 7.4 Advanced Array Techniques (Lecture 20) Arrays of Objects (pp ) As an example, Java has a Point class as part of its java.awt package. Each Point object is used for storing the (x, y) coordinates of a point in two-dimensional space. We will discuss this class in more detail in the next chapter, but for now we will just construct a few objects from it. 54

55 7.4 Advanced Array Techniques (Lecture 20) Arrays of Objects (pp ) Suppose that you want to construct an array of Point objects: Point[] points = new Point[3]; This statement declares a variable called points that refers to an array of length 3 that stores references to Point objects. Using the new keyword to construct the array doesn t construct any actual Point objects. Instead it constructs an array of length 3, each element of which can store a reference to a Point. When Java constructs the array, it auto-initializes these array elements to the zero-equivalent for the type. 55

56 7.4 Advanced Array Techniques (Lecture 20) Arrays of Objects (pp ) First construct the array initialized to null Point[] points = new Point[3]; Then construct the individual points: points[0] = new Point(3, 7); points[1] = new Point(4, 5); points[2] = new Point(6, 2); 56

57 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Arrays of Objects (pp ) Command-Line Arguments (pp ) Nested Loop Algorithms (pp ) 57

58 7.4 Advanced Array Techniques (Lecture 20) Command-Line Arguments (pp ) Nested Loop Algorithms (pp ) 58

59 7.4 Advanced Array Techniques (Lecture 20) Command-Line Arguments (pp ) As you ve seen since Chapter 1, whenever you define a main method: You are required to include as its parameter String[] args This is an array of String objects 59

60 7.4 Advanced Array Techniques (Lecture 20) Command-Line Arguments (pp ) Java itself initializes this array if the user provides what are known as command-line arguments when invoking Java. The user could execute a Java class called DoSomething from a command prompt or terminal by using a command like: java DoSomething The user has the option to type extra arguments, as in the following: java DoSomething temperature.dat temperature.out 60

61 7.4 Advanced Array Techniques (Lecture 20) Command-Line Arguments (pp ) In this case the user has specified two extra arguments that are file names that the program should use: The names of an input and output file The String[] args parameter to main is initialized to an array of length 2 that stores these two strings: 61

62 7.4 Advanced Array Techniques (Lecture 20) Shifting Values in an Array (pp ) Arrays of Objects (pp ) Command-Line Arguments (pp ) Nested Loop Algorithms (pp ) 62

63 7.4 Advanced Array Techniques (Lecture 20) Nested Loop Algorithms (pp ) Consider the problem of printing inversions in an array of numbers An inversion is a pair of numbers in which the first number is greater than the second A sorted (ascending) array has no inversions A reversed (descending) array has many inversions 63

64 7.4 Advanced Array Techniques (Lecture 20) Nested Loop Algorithms (pp ) Inversions in a reversed (descending) array [4, 3, 2, 1]: 64

65 7.4 Advanced Array Techniques (Lecture 20) Nested Loop Algorithms (pp ) Inversions in a reversed (descending) array [4, 3, 2, 1]: public class Lecture20S62 { public static void main(string[] args) { int[] data = {4, 3, 2, 1; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data.length; j++) { if (data[i] > data[j]) { This works, but is not optimized System.out.println("(" + data[i] + ", " + data[j] + ")"); 65

66 7.4 Advanced Array Techniques (Lecture 20) Nested Loop Algorithms (pp ) Inversions in a reversed (descending) array [4, 3, 2, 1]: public class Lecture20S62 { public static void main(string[] args) { int[] data = {4, 3, 2, 1; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data.length; j++) { if (data[i] > data[j]) { We do not need to check the last location in the outer loop The inner loop does not have to check 0 through i System.out.println("(" + data[i] + ", " + data[j] + ")"); 66

67 7.4 Advanced Array Techniques (Lecture 20) Nested Loop Algorithms (pp ) Inversions in a reversed (descending) array [4, 3, 2, 1]: public class Lecture20S62 { public static void main(string[] args) { int[] data = {4, 3, 2, 1; for (int i = 0; i < data.length-1; i++) { for (int j = i + 1; j < data.length; j++) { if (data[i] > data[j]) { System.out.println("(" + data[i] + ", " + data[j] + ")"); Optimized algorithm 67

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

Topic 22 arrays - part 2. Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from

Topic 22 arrays - part 2. Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from http://www.buildingjavaprograms.com/ Swapping values public static void main(string[] args)

More information

Building Java Programs Chapter 7

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

More information

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

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

More information

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

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

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Reference Semantics; 2D Arrays; Array as State Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Reference Semantics; 2D Arrays; Array as State Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin

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

Topic 21 arrays - part 1

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

More information

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

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

More information

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 16 References and linked nodes reading: 16.1 2 Value semantics value semantics: Behavior where values are copied when assigned, passed as parameters, or returned. All primitive

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

Lecture 10: Arrays II

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

More information

Admin. CS 112 Introduction to Programming. Recap: Arrays. Arrays class. Array Return (call) Array Return (declare) q Exam 1 Max: 50 Avg/median: 32

Admin. CS 112 Introduction to Programming. Recap: Arrays. Arrays class. Array Return (call) Array Return (declare) q Exam 1 Max: 50 Avg/median: 32 Admin 100# 95# 90# 85# 80# CS 112 Introduction to Programming q Exam 1 Max: 50 Avg/median: 32 75# 70# 65# 60# 55# 50# 45# 40# 35# 30# 25# Reference Semantics; 2D Arrays; Array as State Yang (Richard) Yang

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks: Ch. 7 #19-23 exercises: Ch. 7 #5 Section attendance question Write a program that reads

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

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

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

More information

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

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

More information

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

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

More information

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

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

More information

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

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

More information

Passing Array to Methods

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

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 7 Lecture 7-3: File Output; Reference Semantics reading: 6.4-6.5, 7.1, 4.3, 3.3 self-checks: Ch. 7 #19-23 exercises: Ch. 7 #5 Two separate topics File output A lot like printing

More information

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

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

More information

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

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

More information

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

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

More information

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

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

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

Topic 12 more if/else, cumulative algorithms, printf

Topic 12 more if/else, cumulative algorithms, printf Topic 12 more if/else, cumulative algorithms, printf "We flew down weekly to meet with IBM, but they thought the way to measure software was the amount of code we wrote, when really the better the software,

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner 1 Lecture outline console input with Scanner objects input tokens Scanner as a parameter to a method cumulative

More information

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening.

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening. Last Time Announcements The File class. Back to methods Passing parameters by value and by reference. Review class attributes. An exercise to review File I/O, look at passing by reference and the use of

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

CIS November 14, 2017

CIS November 14, 2017 CIS 1068 November 14, 2017 Administrative Stuff Netflix Challenge New assignment posted soon Lab grades Last Time. Building Our Own Classes Why Abstraction More on the new operator Fields Class vs the

More information

CIS 1068 Netflix Challenge New assignment posted soon Lab grades November 14, 2017

CIS 1068 Netflix Challenge New assignment posted soon Lab grades November 14, 2017 Administrative Stuff CIS 1068 Netflix Challenge New assignment posted soon Lab grades November 14, 2017 Last Time. Building Our Own Classes Why Abstraction More on the new operator Fields Class vs the

More information

Manipulating One-dimensional Arrays

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

More information

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

Topic 11 Scanner object, conditional execution

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

More information

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

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

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

More information

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

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

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

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

Nested Loops. A loop can be nested inside another loop.

Nested Loops. A loop can be nested inside another loop. Nested Loops A loop can be nested inside another loop. Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started

More information

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003 Arrays COMS W1007 Introduction to Computer Science Christopher Conway 10 June 2003 Arrays An array is a list of values. In Java, the components of an array can be of any type, basic or object. An array

More information

Arrays in Java Using Arrays

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

More information

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

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

More information

CS 106A, Lecture 16 Arrays

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

More information

CSE 143 Lecture 14. Sorting

CSE 143 Lecture 14. Sorting CSE 143 Lecture 14 Sorting slides created by Marty Stepp and Ethan Apter http://www.cs.washington.edu/143/ Sorting sorting: Rearranging the values in an array or collection into a specific order (usually

More information

Object Oriented Programming. Java-Lecture 1

Object Oriented Programming. Java-Lecture 1 Object Oriented Programming Java-Lecture 1 Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen

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

The Array Tools Lab. Introduction In this laboratory, you will learn how to construct functions that operate on Java arrays of type

The Array Tools Lab. Introduction In this laboratory, you will learn how to construct functions that operate on Java arrays of type The Array Tools Lab Introduction In this laboratory, you will learn how to construct functions that operate on Java arrays of type int[] and double[]. Many of the techniques will generalize to arrays of

More information

Array. Array Declaration:

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

More information

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp! Garfield AP CS User Input, If/Else Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp! Warmup Write a method add10 that takes one integer parameter. Your method should return

More information

Building Java Programs

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

More information

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

ARRAYS and ARRAYLISTS

ARRAYS and ARRAYLISTS CHAPTER 6 ARRAYS and ARRAYLISTS Copyright 2013 by John Wiley & Sons. All rights reserved. Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/2011 Chapter Goals q To collect elements using arrays

More information

Topic 11 Scanner object, conditional execution

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

More information

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

CS 113 MIDTERM EXAM 2 SPRING 2013

CS 113 MIDTERM EXAM 2 SPRING 2013 CS 113 MIDTERM EXAM 2 SPRING 2013 There are 18 questions on this test. The value of each question is: 1-15 multiple choice (3 pts) 17 coding problem (15 pts) 16, 18 coding problems (20 pts) You may get

More information

Module 7: Arrays (Single Dimensional)

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

More information

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

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

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

Building Java Programs

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

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to define and invoke void and return java methods JAVA

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

H212 Introduction to Software Systems Honors

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

More information

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

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

More information

Topic 12 more if/else, cumulative algorithms, printf

Topic 12 more if/else, cumulative algorithms, printf Topic 12 more if/else, cumulative algorithms, printf "We flew down weekly to meet with IBM, but they thought the way to measure software was the amount of code we wrote, when really the better the software,

More information

CSE 143 Lecture 16 (B)

CSE 143 Lecture 16 (B) CSE 143 Lecture 16 (B) Sorting reading: 13.1, 13.3-13.4 slides created by Marty Stepp http://www.cs.washington.edu/143/ Sorting sorting: Rearranging the values in an array or collection into a specific

More information

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff(); COSC 117 Exam 3 Key Fall 2012 Part 1: Definitions & Short Answer (3 Points Each) 1. A method in a class that has no return type and the same name as the class is called what? How is this type of method

More information

1. Find the output of following java program. class MainClass { public static void main (String arg[])

1. Find the output of following java program. class MainClass { public static void main (String arg[]) 1. Find the output of following java program. public static void main(string arg[]) int arr[][]=4,3,2,1; int i,j; for(i=1;i>-1;i--) for(j=1;j>-1;j--) System.out.print(arr[i][j]); 1234 The above java program

More information

Chapter 2: Basic Elements of Java

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

More information

Chapter 4: Control Structures I

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

More information

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

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

More information

Arrays and ArrayLists. Ananda Gunawardena

Arrays and ArrayLists. Ananda Gunawardena Arrays and ArrayLists Ananda Gunawardena Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages Arrays allow us to store arbitrary sized sequences

More information

Building Java Programs

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

More information

CS 112 Introduction to Programming

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

More information

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

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Parameter Passing in Function Calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 3.4, 4.1, 4.5 2 Interactive Programs with Scanner reading: 3.3-3.4 Interactive programs We have written programs that print console

More information

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

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

More information

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

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

More information

Example: Monte Carlo Simulation 1

Example: Monte Carlo Simulation 1 Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149

More information

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

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

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 16 References and linked nodes reading: 16.1 2 Recall: stacks and queues stack: retrieves elements in reverse order as added queue: retrieves elements in same order as added

More information

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington Arrays and Array Lists CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington 1 Motivation Current limitation: We cannot record multiple

More information

CSE 143 Lecture 22. Sorting. reading: 13.1, slides adapted from Marty Stepp and Hélène Martin

CSE 143 Lecture 22. Sorting. reading: 13.1, slides adapted from Marty Stepp and Hélène Martin CSE 143 Lecture 22 Sorting reading: 13.1, 13.3-13.4 slides adapted from Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/ Sorting sorting: Rearranging the values in an array or collection

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

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

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

More information

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

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics Inf1-OP Inf1-OP Exam Review Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics March 20, 2017 Overview Overview of examinable material: Lectures Week 1

More information

1 Short Answer (10 Points Each)

1 Short Answer (10 Points Each) 1 Short Answer (10 Points Each) 1. For the following one-dimensional array, show the final array state after each pass of the three sorting algorithms. That is, after each iteration of the outside loop

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises: #7 videos: Ch. 4 #2-4 Loops with if/else if/else statements can be used with

More information

CS 112 Introduction to Programming

CS 112 Introduction to Programming CS 112 Introduction to Programming Summary of Methods; User Input using Scanner Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone: 432-6400 Email: yry@cs.yale.edu Admin

More information

Building Java Programs

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

More information