Why did the programmer quit his job?

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

1 Various Methods of Populating Arrays Randomly generated integers.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: File Output; Reference Semantics reading: , 7.1, 4.3, 3.3 self-checks:
Building Java Programs Chapter 7 Arrays. 2 Can we solve this problem? Consider the following program (input underlined): How many days' temperatures?
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1 Reference semantics. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[] args) { int a = 7; int b.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Chapter 7
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CSCI 162 – Introduction to Programming II William Killian
Building Java Programs
Lecture 13: More Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Dr. Kyung Eun Park Summer 2017
CSc 110, Autumn 2017 Lecture 23: lists as Parameters
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Array traversals, text processing
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Building Java Programs
Building Java Programs
Building Java Programs Chapter 7
Lecture 11 C Parameters Richard Gesick.
Pass by Reference, const, readonly, struct
CSc 110, Spring 2017 Lecture 19: more with lists
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Autumn 2016 Lecture 19: lists as Parameters
Return by Reference CSCE 121 J. Michael Moore.
CSE 143 Lecture 6 References and Linked Nodes reading: 16.1
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from
Why did the programmer quit his job?
Building Java Programs
Building Java Programs Chapter 7
Parameter Passing in Java
CS2011 Introduction to Programming I Arrays (II)
Can we solve this problem?
CSc 110, Spring 2018 Lecture 23: lists as Parameters
Lecture 10: Arrays AP Computer Science Principles
Simulating Reference Parameters in C
Lecture 12: 2D Arrays AP Computer Science Principles
Lecture 10: Arrays II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
References and objects
CSE 143 Lecture 5 References and Linked Nodes
Building Java Programs
Value and reference semantics; Arrays as parameters
Suggested self-checks: Section 7.11 #1-11
Building Java Programs
Building Java Programs
Binary Search Trees reading: 17.3 – 17.4
Building Java Programs
A+ Computer Science PARAMETERS
Lecture 6: References and linked nodes reading: 16.1
CSE 143 Lecture 7 References and Linked Nodes reading: 16.1
CIS 110: Introduction to Computer Programming
Building Java Programs
Lecture 6: References and linked nodes reading: 16.1
Presentation transcript:

Why did the programmer quit his job? Because he didn't get arrays.

Array reversal question Write code that reverses the elements of an array. For example, if the array initially stores: [11, 42, -5, 27, 0, 89] Then after your reversal code, it should store: [89, 0, 27, -5, 42, 11] The code should work for an array of any size. Don’t make an extra array. Reverse the original array Hint: think about swapping various elements...

Algorithm idea Swap pairs of elements from the edges; work inwards: index 1 2 3 4 5 value 89 27 -5 42 11 index 1 2 3 4 5 value 89 -5 27 42 11 index 1 2 3 4 5 value 11 42 -5 27 89 index 1 2 3 4 5 value 89 42 -5 27 11

Value semantics 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 Make boxes for the future

Reference semantics (objects) 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] memory a1 index 1 2 value 7 15 8 index 1 2 value 4 15 8 a2

References and objects 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

cat[] cats1 = {🐱 , 🐱, 🐱, 🐱}; cats1 cat[] cats2 = cats1; cats2

dog[] dogs1 = {🐶 , 🐶 , 🐶 }; dogs1 dog[] dogs2 = dogs1; dogs2

Arrays pass by reference 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] iq index 1 2 value 126 167 95 index 1 2 value 252 334 190 a

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