CIS 110: Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
1 Advanced Material The following slides contain advanced material and are optional.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Java Unit 9: Arrays Declaring and Processing Arrays.
1-Sep-15 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Lecturer : Sakuni Sellapperuma. Introduction An array is a container object that holds a fixed number of values of a single type. The length of an array.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
CSC 142 Computer Science II Zhen Jiang West Chester University
REEM ALMOTIRI Information Technology Department Majmaah University.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Chapter 9 Introduction to Arrays Fundamentals of Java.
13-Jun-16 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chair of Software Engineering Void safety these slides contain advanced material and are optional.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Week 2 - Wednesday CS 121.
Computer Organization and Design Pointers, Arrays and Strings in C
4. Java language basics: Function
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Two-Dimensional Arrays
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 6: Data Types Lectures # 10.
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Testing and Debugging.
EGR 115 Introduction to Computing for Engineers
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Java Review: Reference Types
Arrays in C.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
More Loop Examples Functions and Parameters
Data.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
EKT150 : Computer Programming
Building Java Programs
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Scala Apologia 1-Jan-19.
Building Java Programs
Data Types and Data Structures
Barb Ericson Georgia Institute of Technology Oct 2005
Lecture 13: Two-Dimensional Arrays
CS 200 Arrays Jim Williams, PhD.
Lecture 10: Arrays AP Computer Science Principles
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
Building Java Programs
CSC 142 Arrays [Reading: chapter 12].
Java SE 7 One and Multi Dimensional Arrays Module 6
CS2013 Lecture 7 John Hurley Cal State LA.
How do you do the following?
More 2D Array and Loop Examples Functions and Parameters
Arrays.
CIS 110: Introduction to computer programming
Presentation transcript:

CIS 110: Introduction to Computer Programming Lecture 19 Array Wrap-up (§ 7.2-7.3) 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Outline Side note: bits and integral representations The null value Multi-dimensional arrays 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

Bits and integral representations 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

What is actually "in" a variable? So far, we've worked at this level of abstraction for variables: x 42 But computers don't physically store "42" in memory 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

Digital representation of data Recall from chapter 1: a computer stores all data digitally, i.e., as a binary number (base 2). x 101010 This is because of the natural of the electromagnetic switches that store the data. Either "on" (value 1) or "off" (value 0). 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Java Integers Java ints are 32-bit values. Each binary digit is called a bit. x 00000000000000000000000000101010 Reasoning about numbers in binary is tedious so we usually use decimal (base 10) instead. x 42 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Color components A color component of a pixel is made up of 3 numbers, one for each color. R 11111111 G 00000000 B 10001111 Each component can take on a value from 0-255 (i.e., 8 bits, or 28 values). 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Bit-packing We can store each component as an int, but that is wasteful. A component uses 8 bits, but an int uses 32 bits. Instead, let's pack all 3 values into a single int! RED BLUE c 00000000111111110000000100001111 GREEN 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania The null value 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Arrays of objects We've made arrays of primitive types so far. int x[] = { 0, 1, 2 }; X [0, 1, 2] We can make arrays of object types as well. String y[] = new String[3]; y [?, ?, ?] 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

A question of initialization When we make an array of primitive type, we initialize its elements to "zero values". int arr1[] = new int[3]; double arr2[] = new double[3]; boolean arr3[] = new boolean[3]; arr1 [0, 0, 0] arr2 [0.0, 0.0, 0.0] arr4 [false, false, false] What is the zero value of a String? In general any object type? 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

Null means "no reference" We specify that "this variable is not referencing an object" by using the null value. String s1 = "hello"; String s2 = null; s1 "Hello" s2 Null is the "zero value" for all objects types. String y[] = new String[3]; y [ , , ] 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

The dreaded NullPointerException When you try to call a method/access a field of an null reference, you get a NullPointerException. String s2 = null; s2.length(); s2 What String? s2 isn't referencing a String… AHHHHHH 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

"Null References: The Billion Dollar Mistake" Abstract of Tony Hoare's talk about null references (QCon London 2009): I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

Multi-dimensional arrays 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Arrays of Arrays We can declare arrays of primitive types. int x[] = { 0, 1, 2 }; X [0, 1, 2] We can declare arrays of object types. "hi" y [ , ] String[] y = { "hi", "bye" }; "bye" But arrays are objects, so we can declare arrays of arrays. int[][] z = new int[3][3]; 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

Rectangular two-dimensional arrays int[][] z = new int[3][3]; Really an array of arrays [0, 0, 0] z [ , , ] [0, 0, 0] [0, 0, 0] Conceptually a grid [0, 0, 0] z 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

Accessing two-dimensional arrays [0, 0, 0] [0, 5, 0] z int[][] z = new int[3][3]; z[2][1] = 5; Remember zero-based indexing. Think of z[i][j] as "ith row, jth column" 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Say hi to you neighbors Say the middle element is z[i][j]... z[i+1][j] z[i-1][j-1] ... 1, 2, 3 ... 5, 6, 7 ... 8, 9, 10 z[i-1][j+1] z[i][j-1] z[i][j+1] z[i+1][j-1] z[i+1][j+1] z[i+1][j] 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Layers upon layers [0, 1, 2] [3, 4, 5] [6, 7, 8] z int z[][] = new int[3][3]; ... z ... // The entire array ... z[1] ... // A single row (an array itself) ... z[1][2] ... // A single element 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

Traversing two-dimensional arrays int z[][] = new int[3][4]; // for each row... for (int i = 0; i < z.length; i++) { // for each column... for (int j = 0; j < z[i].length; j++) { // print out the element System.out.println(z[i][j]); } [0, 1, 2] [3, 4, 5] [6, 7, 8] [9, 0, 1] 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania N-dimension arrays We can have as many dimensions as we want! int threeDims[][][] = new int[3][4][5]; int fourDims[][][][] = new int[5][7][9][10]; Same logic as before applies! How we interpret the dimensions is irrelevant as long as we are consistent. 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Jagged arrays Array rows need not have the same length, i.e., they can be jagged. [0, 1] z [ , , ] [2, 3, 4] [5] 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

Initializing jagged arrays We have to manually create jagged arrays by initializing each row at a time. [0, 1] z [ , , ] [2, 3, 4] [5] int[][] z = new int[3][]; z[0] = { 0, 1 }; z[1] = { 2, 3, 4 }; z[2] = new int[1]; z[2][0] = 5; Otherwise they behave like rectangular arrays! 5/8/2019 CIS 110 (11fa) - University of Pennsylvania

The Arrays helper class (in java.util) // Returns the string representation of arr suitable for // printing, e.g., [0, 2, 3, 4, 5] Arrays.toString(arr); // Returns true if the elements of the array are pairwise equals Arrays.equals(arr1, arr2); // Fills the array with the given value Arrays.fill(arr, value); // Returns a copy of the given array with the specified length, // either truncating elements or filling with zero-values to // meet that length Arrays.copyOf(arr, len); // toString and equals variants that work for multi-dimensional arrays Arrays.deepToString(arr); Arrays.deepEquals(arr1, arr2); 5/8/2019 CIS 110 (11fa) - University of Pennsylvania