CIS 110: Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Advertisements

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:
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
CS1101: Programming Methodology Aaron Tan.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
Building java programs chapter 6
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
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:
String and Scanner CS 21a: Introduction to Computing I First Semester,
Week 14 - Monday.  What did we talk about last time?  Inheritance.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
CS1020 Data Structures and Algorithms I Lecture Note #2 Arrays.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Introduction to programming in java
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Chapter VII: Arrays.
Array in C# Array in C# RIHS Arshad Khan
Building Java Programs
File - CIS 1068 Program Design and Abstraction
File Input / Output.
The Data Types and Data Structures
Foundations of Programming: Arrays
Week 14 - Wednesday CS 121.
Building Java Programs
CSC 142 Computer Science II
Arrays.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CIS 110: Introduction to Computer Programming
python.reset() Also moving to a more reasonable room (CSE 403)
CS2011 Introduction to Programming I Arrays (I)
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Single-Dimensional Arrays chapter6
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
CIS 110: Introduction to Computer Programming
Building Java Programs
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
CIS 110: Introduction to Computer Programming
Building Java Programs
Building Java Programs
LCC 6310 Computation as an Expressive Medium
Building Java Programs
CIS 110: Introduction to Computer Programming
How do you do the following?
Building Java Programs
More on iterations using
Week 7 - Monday CS 121.
Presentation transcript:

CIS 110: Introduction to Computer Programming Lecture 17 All hail the mighty array (§ 7.1) 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Outline Catch-up from last week: file output Introduction to Arrays 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania About me My name: Michael-Peter Osera. Call me Michael-Peter. I am a 4th year Ph.D. student (not a professor). Ethnomusicology researcher. Die-hard supporter of clubs in and around Philly! 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Now to midterm #2 Su M T W Th F S 7 8 9 10 11 12 Arrays Homework 7 out 13 14 15 16 17 18 19 More arrays + review 20 21 22 23 24 25 26 Midterm + objects Midterm #2 review Midterm #2 Homework 7 due 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania File output 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania The PrintStream class PrintStreams allow us to write to files! public static void main(String[] args) throws FileNotFoundException { PrintStream out = new PrintStream(new File("helloworld.txt")); out.println("Hello World!"); } PrintStreams have: print println printf Sound familiar? helloworld.txt Hello World! 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

System.out is a PrintStream! public class System { public static PrintStream out = /* ... */; } System.out is a PrintStream that outputs to the console. PrintStreams that we make function identically but output goes to a File instead! 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

Example: AllCapsWriter public class AllCapsWriter { public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner( new File("in.txt")); PrintStream out = new PrintStream( new File("out.txt")); while (in.hasNextLine()) { out.println(in.nextLine().toUpperCase()); } 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Arrays 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

Remembering lots of stuff We can't store arbitrary amounts of data! Scanner in = new Scanner(System.in); System.out.println("Enter 10 numbers: "); double current = 0; for (int i = 0; i < 10; i++) { System.out.print("Enter a double: "); current = in.nextDouble(); // ... } We necessarily "forget" each double the user enters after each iteration of the loop. 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Introducing the array Arrays allow us to store lots of data at once! Scanner in = new Scanner(System.in); System.out.println("Enter 10 numbers: "); double[] values = new double[10]; for (int i = 0; i < 10; i++) { System.out.print("Enter a double: "); values[i] = in.nextDouble(); } The values array contains all 10 doubles entered by the user! 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

Declaring arrays An array is an object that contains multiple values. A variable values that holds an array of doubles. double[] values = new double[10]; An array type. "An array of doubles" Array creation. Instantiates a new array containing 10 doubles. Arrays are homogenous: all elements have the same type. 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Accessing Arrays We index into arrays to access individual elements. values[i] = in.nextDouble(); Index into the ith position of the array and store the next double from the user there. 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

Array operation examples: initialization double[] values = new double[5]; [0] [1] [2] [3] [4] 0.0 0.0 0.0 0.0 0.0 values Array variables containing references to arrays. Array elements are auto-initialized to "zero values". 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

Array operation examples: assignment values[2] = 4.7; values[0] = 1.8; [0] [1] [2] [3] [4] 1.8 0.0 4.7 0.0 0.0 values Arrays have zero-based indices. An indexed array is just a storage location we can assign into and access like a variable. 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

Array operation examples: usage System.out.println(values[0] + values[2]); > 6.5 [0] [1] [2] [3] [4] 1.8 0.0 4.7 0.0 0.0 values To use an element of the array, we simply index into the array at the desired position. 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

Array operation examples: length System.out.println("Length = " + values.length); > 5 [0] [1] [2] [3] [4] 1.8 0.0 4.7 0.0 0.0 values The length field of the array object contains that arrays length. Note: no parenthesis after length unlike Strings! It really is a variable rather than a method. 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

IndexOutOfBoundsExceptions values[10] = 4.1; > Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 [0] [1] [2] [3] [4] 1.8 0.0 4.7 0.0 0.0 values We get IndexOutOfBounds exceptions if we try to access an index that doesn't exist. We can't change the size of an array once it is made! 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

For-loop traversal template We can put this all together to write a useful traversal pattern: for (int i = 0; i < arr.length; i++) { /** ...arr[i]...*/ } For each element of arr, do something to it. 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Enhanced for-loop "Doing something" to each element is so common there's special syntax to do this: int arr[] = new int[10]; for (int i : arr) { /** ...i... */ } Upside: succinct captures for each element… Downside: don't have access to current index. 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Random access With Scanners, we process data sequentially. Arrays allow us to have random access to data. Scanner in = new Scanner(System.in); System.out.println("Enter 10 lines: "); String[] lines = new String[10]; for (int i = 0; i < 10; i++) { System.out.print("Enter a line: "); lines[i] = in.nextLine(); } System.out.print("Which line do you want? "); int index = in.nextInt(); System.out.println("Index " + index + " = " + lines[index]); 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

Alternative array initialization There's special syntax for initializing an array with non-default values. int[] values = { 4, 3, 2, 1, 0 }; [0] [1] [2] [3] [4] 4 3 2 1 values 12/27/2018 CIS 110 (11fa) - University of Pennsylvania

References, arrays, and methods Methods can change the value of arrays unlike with variables! Due to reference semantics that we'll talk about next time. public static void initialize(int[] values) { for (int i = 0; i < values.length; i++) { values[i] = values.length - i - 1; } public static void main(String[] args) { int[] values = new int[3]; // Before: values = { 0, 0, 0 } initialize(values); // After: values = { 2, 1, 0 } 12/27/2018 CIS 110 (11fa) - University of Pennsylvania