CSE 116/504 – Intro. To Computer Science for Majors II

Slides:



Advertisements
Similar presentations
Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing.
Advertisements

Terms and Rules Professor Evan Korth New York University (All rights reserved)
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
CSC 212 – Data Structures Lecture 12: Java Review.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
English Conundrum s  In English, add “s” to end of word to make plural s  But for 1 special word, adding an “s” at the end:  Makes word go from plural.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Chapter 8: Arrays.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Question of the Day  There are two escalators at each subway stop. One going up & one down.  Whenever an escalator needs to be fixed, they almost always.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.
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.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Array in C# Array in C# RIHS Arshad Khan
Modern Programming Tools And Techniques-I
Sixth Lecture ArrayList Abstract Class and Interface
Arrays (review) CSE 2011 Winter May 2018.
Examples of Classes & Objects
Lecture 7: Reusing Code & Interfaces
Elementary Programming
Lecture 2: Primitives & References
Inheritance and Encapsulation
CSE 116/504 – Intro. To Computer Science for Majors II
Lecture 15: More Iterators
CSE 116/504 – Intro. To Computer Science for Majors II
Review Session.
Lecture 8: Polymorphism & Class Design
Object Oriented Programming (OOP) LAB # 8
CS 302 Week 8 Jim Williams, PhD.
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.
Review for Final Exam.
Arrays in Java What, why and how Copyright Curt Hill.
Introduction To Programming Information Technology , 1’st Semester
BIT115: Introduction to Programming
Grouped Data Arrays, and Array Lists.
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
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.
Single-Dimensional Arrays chapter6
CS 200 Objects and ArrayList
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Java Programming Language
2009 Test Key.
CS 200 Objects and ArrayList
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
Week 7 - Monday CS 121.
Presentation transcript:

CSE 116/504 – Intro. To Computer Science for Majors II Lecture 9: Arrays

Announcements Grades posted to UBLearns on regular basis May take few days, but allows you to track progress Lowest 3 homeworks dropped to calculate final grade Will also see lowest quiz grade excluded in calculations Homework for week due Mondays at 12:45PM Problems NOT optional If it takes longer than 15 minutes, get help ASAP

Inheritance Example public class SuperClass { public String getMyString() { return "SUPER"; } } public class SubClass extends SuperClass { public String getMyString() { return "sub " + super.getMyString(); } public String otherMethod() { return "other"; } public static void main(String[] args) { SubClass sub2, sub = new SubClass(); SuperClass super1 = new SuperClass(); System.out.println(sub.getMyString()); System.out.println(super1.getMyString()); System.out.println(sub.otherMethod()); super1 = sub; System.out.println(super1.getMyString()); SubClass sub2 = (SubClass)super1;

Declaring Array Variables Like all variables, must declare before use Type, brackets, & name declare array variable Still variables, so names follow usual rules Variable is array of the type, so use any legal type Each of the array's entries hold value of that type String[] bob; long[] johnSilver; VWCar[] errorProne;

Object[] parkingLot = new Car[17]; int[] bob; bob = new int[intVar]; Instantiating Arrays Cannot use until after instantiating array Type & length of array needed for instantiation Object[] parkingLot = new Car[17]; int[] bob; bob = new int[intVar]; Type is for elements & must be assignable to array’s type Size must be int since ½ a value hard to use

Object[] parkingLot = new Car[17]; int[] bob; bob = new int[intVar]; Instantiating Arrays Cannot use until after instantiating array Type & length of array needed for instantiation Object[] parkingLot = new Car[17]; int[] bob; bob = new int[intVar]; Type is for elements & must be assignable to array’s type Size must be int since ½ a value hard to use

Array Basics #1 Must specify size when allocated: Cannot grow array Cannot shrink array

Which Declaration is NOT Legal? int[] iBob = new int[23]; double[] dBob; Float fBob[17]; ArrayList[] alBob; int i=7; long[] lBob = new long[i]; 27

Which Declaration is NOT Legal? int[] iBob = new int[23]; double[] dBob; Float fBob[17]; ArrayList[] alBob; int i=7; long[] lBob = new long[i]; Convince your neighbor your answer is correct

Which Declaration is NOT Legal? int[] iBob = new int[23]; double[] dBob; Float fBob[17]; ArrayList[] alBob; int i=7; long[] lBob = new long[i]; 4

Which Declaration is NOT Legal? int[] iBob = new int[23]; double[] dBob; Float fBob[17]; ArrayList[] alBob; int i=7; long[] lBob = new long[i]; Need to use new to create array instances.

Which Declaration is NOT Legal? int[] iBob = new int[23]; double[] dBob; Float fBob[17]; Float[] fBob = new Float[17]; ArrayList[] alBob; int i=7; long[] lBob = new long[i]; Need to use new to create array instances.

Assigning Array Variables Can be set to null to mark there is no array Can be set to newly instantiated array Alias array by assigning to another array variable Does not copy array, but has both refer to same object Change to entry seen by both variables, since just aliases TV image is public domain: http://www.publicdomainpictures.net/pictures/70000/velka/tv-isolated-background-clipart.jpg Remote images by Dwight Burdette and used under Creative Commons 3.0 License. Original is at: https://commons.wikimedia.org/wiki/File:Philips_SRC206WM_Universal_Remote_Control.JPG

Array Key Concept #2 Array variable is reference variable target either array instance or null

Memory Trace Example int[] data = new int[3]; int[] al = data; for (int i = 0; i < data.length; i++) { data[i] = i * 2; } al[0] = 5; al = new int[1]; al[0] = 7;

Array Key Concept #3 Allocating array of object type DOES NOT allocate other objects for its entries

Entries Are Variables, Too! Within an array entries like individual variables int[] bob = new int[30]; Car[] parkingLot = new Car[300]; bob’s entries would be primitive variables parkingLot’s entries refer to instances Entries initialized like variable when array created bob’s entries would all be set to 0 null stored in all entries in parkingLot

Array entries act like fields Should initialize before using Array Key Concept #4 Array entries act like fields Should initialize before using

Entries Are Variables, Too! Object[] data = new Object[3]; Object[] al = data; for (int i = 0; i < data.length; i++) { data[i] = new Object(); } al[0] = new Object(); al = new Object[1]; al[0] = data[0]; data[0] = data[1];

Which is Correct Java Code? int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; 26

Which is Correct Java Code? int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; Convince your neighbor your answer is correct

Which is Correct Java Code? int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453;

Which is Correct Java Code? In new, size of array written in brackets int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453;

Which is Correct Java Code? int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; Must assign array entries individually

Which is Correct Java Code? int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; Entries at indices 0 - length - 1

Which is Correct Java Code? int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; Specify entry by writing index in brackets

Which is Correct Java Code? int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453; Entry like variable of array's type

Which is Correct Java Code? int[] a=new int(12); a[2] = 12; char[] b=new char[5]; b[] = “1234”; int[] c=new int[2]; c[2] = 12; int[] d=new int[20]; d(10) = 2.0; int s = 45; int[] d=new int[s]; d[s-4] = 32453;

Array Basics #1 Must specify size when allocated: Cannot grow array Cannot shrink array

Array Key Concept #2 Array variable is reference variable target either array instance or null

Array Key Concept #3 Allocating array of object type DOES NOT allocate other objects for its entries

Array entries act like fields Should initialize before using Array Key Concept #4 Array entries act like fields Should initialize before using

For Next Lecture Discussing 1st (& most interesting) ADT Wednesday What is an ADT and why do ADTs drive the world? What is a MultiSet & why only in Google’s Java API? How does MultiSet differ from array or ArrayList? How to use MultiSet in our programs? There are week #4 homework problems on web Due by 12:45PM Monday for this week & all following