University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Arrays II Lecture 15, Thu Mar 2 2006

Slides:



Advertisements
Similar presentations
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism Lecture 20, Tue Mar
Advertisements

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Inheritance II Lecture 23, Thu Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism II Lecture 21, Thu Mar
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Inheritance Lecture 22, Tue Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Advanced Class Design Lecture 19, Thu Mar
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Parameter/Scope Review II, Inheritance Lecture 33,
1 CS100J 13 March 2006 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,
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Programming with Collections Collections in Java Using Arrays Week 9.
1 CS100J 14 March 2006 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,
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 2D Arrays, Sorting Lecture 16, Tue Mar
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays II Lecture 22, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 30, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 29, Fri Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 2D Arrays, Sorting Lecture 23, Fri Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Arrays and Class Design Lecture 18, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays Lecture 21, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design II Lecture 31, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Yet More Array Practice Lecture 24, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 28, Wed Mar
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
1 CS100J 25 October 2006 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,
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design III, Parameter/Scope Review Lecture.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Java Unit 9: Arrays Declaring and Processing Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Lecture 7: Arrays Tami Meredith. Roadmap Variables Arrays Array indices & Using arrays Array size versus length Two dimensional arrays Sorting an array.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
OOP Basics Classes & Methods (c) IDMS/SQL News
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.
Java Programming Language Lecture27- An Introduction.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Windows Programming Lecture 03. Pointers and Arrays.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
CS 200 Loops Jim Williams, PhD.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Review for Midterm 3.
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Arrays II Lecture 15, Thu Mar based on slides by Kurt Eiselt

News n Assignment 2 n do not wait until last minute n corrections to ASCIIArtiste.java posted (Sun) n definitely read WebCT bboards! n Remember CSLC available! n Mon-Thu 10-6, Fri 10-4, x150 (near Reboot) n extra TA lab coverage for A2 help: n Sun 12-2 Parker n Tue 4-6 Hastings, 6-8 Leavitt

Reading n This week: 8.1, , topics 6.3 and 6.4 n Next week: no new reading

n Common confusion on midterm: what's wrong? public class RoachPopulation { private int myPopulation; private static final double KILL_PERCENT = 0.10; public RoachPopulation(int population) { this.myPopulation = population; } public void waitForDoubling(int newPopulation) newPopulation *= 2; } public int getRoaches() { return this.myPopulation } Scope Revisited, With Diagrams public class RoachPopulationDriver { public static void main (String[] args) { RoachPopulation r = new RoachPopulation(10); int num = r.getRoaches(); r.waitForDoubling(); num = r.getRoaches(); }

cansSold Recap: Array Declaration and Types n Just like ordinary variable, must n declare array before we use it n give array a type n Since cansSold contains integers, make integer array: int[] cansSold = new int[10] n Looks like variable declaration, except: n empty brackets on the left tell Java that cansSold is an array... n the number in the brackets on the right tell Java that array should have room for 10 elements when it's created

cansSold Recap: Array Declaration and Types public class ArrayTest1 { public static void main(String[] args) { final int ARRAYSIZE = 10; int[] cansSold = new int[ARRAYSIZE]; cansSold[0] = 185; cansSold[1] = 92; cansSold[2] = 370; cansSold[3] = 485; cansSold[4] = 209; cansSold[5] = 128; cansSold[6] = 84; cansSold[7] = 151; cansSold[8] = 32; cansSold[9] = 563; // do useful stuff here System.out.println("Element 4 is " + cansSold[4]); }

cansSold Recap: Array Declaration and Types public class ArrayTest2 { public static void main(String[] args) { int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; // do useful stuff here System.out.println("Element 4 is " + cansSold[4]); } n Can also use initializer list n Right side of declaration does not include type or size n Java figures out size by itself n Types of values on right must match type declared on left n Initializer list may only be used when array is first declared

Histogram Loop Example n Now use same data as basis for histogram n Write one loop to look at value associated with each row of array n for each value print a line with that many asterisks n For example, if program reads value 6 from the array, should print line of 6 asterisks n Program then reads the value 8, prints a line of 8 asterisks, and so on. n Need outer loop to read individual values in the array n Need inner loop to print asterisks for each value ****** 8 ******** 11 *********** 18 ****************** 20 ******************** 17 ***************** 14 ************** 10 ********** 5 ***** 2 ** numbers

Objectives n Understanding when and how to use n arrays of objects n 2D arrays

Storing Different Data Types cansSold

Storing Different Data Types cansSold cashIn Could use two arrays of same size but with different types

cansSold cashIn Could use two arrays of same size but with different types Storing Different Data Types n Write program to compare what's been collected from each machine vs. how much should have been collected?

cansSold cashIn Could use two arrays of same size but with different types Storing Different Data Types n Write program to compare what's been collected from each machine vs. how much should have been collected? public class ArrayTest4 { public static void main(String[] args) { double expected; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; double[] cashIn = {201.25, , , , , , , , 40.00, }; for (int i = 0; i < cansSold.length; i++) { expected = cansSold[i] * 1.25; System.out.println("Machine " + (i + 1) + " off by $" + (expected - cashIn[i])); }

cansSold cashIn Could use two arrays of same size but with different types What happens when we run the program? Storing Different Data Types n Write program to compare what's been collected from each machine vs. how much should have been collected? public class ArrayTest4 { public static void main(String[] args) { double expected; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; double[] cashIn = {201.25, , , , , , , , 40.00, }; for (int i = 0; i < cansSold.length; i++) { expected = cansSold[i] * 1.25; System.out.println("Machine " + (i + 1) + " off by $" + (expected - cashIn[i])); }

cansSold Machine 0 off by $30.0 Machine 1 off by $14.5 Machine 2 off by $49.75 Machine 3 off by $51.0 Machine 4 off by $66.25 Machine 5 off by $0.0 Machine 6 off by $0.0 Machine 7 off by $0.0 Machine 8 off by $0.0 Machine 9 off by $0.0 cashIn Somebody has been stealing from the machines after all! We need an anti-theft plan… Storing Different Data Types

cansSold cashIn Arrays With Non-Primitive Types n Great if you're always storing primitives like integers or floating point numbers n What if we want to store String types too? n remember that String is an object, not a primitive data type

cansSold cashIn Arrays With Non-Primitive Types n Then we create array of objects n In this case objects will be Strings n Array won't hold actual object n holds references: pointers to objects String[] location = new String[10]; location

cansSold cashIn Arrays of Objects n Now we can put references to Strings in our String array. location[0] = "Chan Centre"; location "Chan Centre"

cansSold cashIn Arrays of Objects n Now we can put references to Strings in our String array. location[0] = "Chan Centre"; location[1] = "Law School"; location "Chan Centre" "Law School"

cansSold cashIn Arrays of Objects n Now we can put references to Strings in our String array. location[0] = "Chan Centre"; location[1] = "Law School"; location[2] = "Main Library"; location "Chan Centre" "Law School" "Main Library"

cansSold cashIn Arrays of Objects n Now we can put references to Strings in our String array. location[0] = "Chan Centre"; location[1] = "Law School"; location[2] = "Main Library";...and so on... location "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science"

cansSold cashIn Arrays of Objects n Or we could have done this: String[] location = {"Chan Centre", "Law School", "Main Library",.... }; location "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science"

cansSold cashIn Arrays of Objects n Each individual String object in array of course has all String methods available n For example, what would this return? location[2].length() location "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science"

cansSold cashIn Arrays of Objects n Each individual String object in array of course has all String methods available n For example, what would this return? location[2].length() n 12 location "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science"

Arrays of Objects n Think about a cleaner way to do all this… "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science" location cashIn cansSold

Arrays and Object Design n Multiple array approach not very object-oriented n can create arrays of objects n can create objects of our own design "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science" location cashIn cansSold

Arrays and Object Design n Cokematic object design - contains n number of cans remaining: integer n location: String, n number of cans sold: integer n cash collected: double

Cokematic n Cokematic object design - contains n number of cans remaining: integer n location: String, n number of cans sold: integer n cash collected: double public class Cokematic { private int numberOfCans; private String location; private int cansSold; private double cashIn; public Cokematic(int cans, String loc, int sold, double cash) { numberOfCans = cans; location = loc; cansSold = sold; cashIn = cash; System.out.println("Adding machine"); }

Cokematic n Cokematic object design - contains n number of cans remaining: integer n location: String, n number of cans sold: integer n cash collected: double public void buyCoke() { if (numberOfCans > 0) { numberOfCans = numberOfCans - 1; cansSold = cansSold + 1; cashIn = cashIn ; System.out.println("Have a Coke"); System.out.println(numberOfCans + " remaining"); } else { System.out.println("Sold out."); }

public String getLocation() { return location; } public int getCansSold() { return cansSold; } public double getCashIn() { return cashIn; } public void reloadMachine(int newCans) { numberOfCans = numberOfCans + newCans; System.out.println("reloading machine"); } public int getNumberOfCans() { return numberOfCans; } public String toString() { return (location + " sold: " + cansSold + " left: " + numberOfCans + " made: " + cashIn); }

Cokematic n In driver, executing Cokematic machine1 = new Cokematic(100, "Chan Centre", 185, );

Cokematic n In driver, executing Cokematic machine1 = new Cokematic(100, "Chan Centre", 185, ); n Results in machine1 Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn: buyCoke() getLocation() getCansSold() getCashIn() reloadMachine() toString()

Cokematic n In driver, executing Cokematic machine1 = new Cokematic(100, "Chan Centre", 185, ); n Results in n Note: leaving out methods in UML diagrams from now on to fit on page machine1 Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn: buyCoke() getLocation() getCansSold() getCashIn() reloadMachine() toString()

Cokematic n In driver, executing Cokematic machine1 = new Cokematic(100, "Chan Centre", 185, ); n Results in n Note: leaving out methods in UML diagrams from now on to fit on page machine1 Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn:

CokeEmpire n Contains array of Cokematic objects public class CokeEmpire { private Cokematic[] collection; // what does this do? public CokeEmpire() { collection = new Cokematic[10]; // what does this do? } public void addCokematic(int index, int cans, String loc, int sold, double cash) { collection[index] = new Cokematic(cans, loc, sold, cash); } public Cokematic getCokematic(int index) { return collection[index]; }

CokeEmpire n In driver, executing: CokeEmpire myMachines = new CokeEmpire();

CokeEmpire n In driver, executing CokeEmpire myMachines = new CokeEmpire(); n results in myMachines

CokeEmpire n Populate array with Cokematic objects myMachines

CokeEmpire n Populate array with Cokematic objects myMachines.addCokematic(0, 100, "Chan Centre", 185, ); myMachines Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn:

CokeEmpire n Populate array with Cokematic objects myMachines.addCokematic(1, 150, "Law School", 92, ); myMachines Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn: Cokematic numberOfCans: 150 location: "Law School" cansSold: 92 cashIn:

CokeEmpire n Populate array with Cokematic objects myMachines.addCokematic(2, 200, "Main Library", 370, ); myMachines Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn: Cokematic numberOfCans: 150 location: "Law School" cansSold: 92 cashIn: Cokematic numberOfCans: 200 location: "Main Library" cansSold: 370 cashIn:

CokeEmpire n What does this return? myMachines.getCokematic(1).getCansSold() myMachines Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn: Cokematic numberOfCans: 150 location: "Law School" cansSold: 92 cashIn: Cokematic numberOfCans: 200 location: "Main Library" cansSold: 370 cashIn:

CokeEmpire n What does this return? myMachines.getCokematic(1).getCansSold() myMachines Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn: Cokematic numberOfCans: 150 location: "Law School" cansSold: 92 cashIn: Cokematic numberOfCans: 200 location: "Main Library" cansSold: 370 cashIn:

CokeEmpire n What does this return? myMachines.getCokematic(1).getCansSold() myMachines Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn: Cokematic numberOfCans: 150 location: "Law School" cansSold: 92 cashIn: Cokematic numberOfCans: 200 location: "Main Library" cansSold: 370 cashIn:

CokeEmpire n What does this return? myMachines.getCokematic(1).getCansSold() myMachines Cokematic numberOfCans: 100 location: "Chan Centre" cansSold: 185 cashIn: Cokematic numberOfCans: 150 location: "Law School" cansSold: 92 cashIn: Cokematic numberOfCans: 200 location: "Main Library" cansSold: 370 cashIn:

Arrays of Arrays

n In any given array, all data must be of same type

Arrays of Arrays n In any given array, all data must be of same type n All arrays in array of arrays must be of same type

Arrays of Arrays n In any given array, all data must be of same type n All arrays in array of arrays must be of same type n So easier to use a two-dimensional array!

Two-Dimensional Arrays n In Java, 2D array implemented internally as array of arrays n but externally syntax of 2D array may seem easier to use rows columns

Two-Dimensional Arrays n In Java, 2D array implemented internally as array of arrays n but externally syntax of 2D array may seem easier to use n Typical control structure for computing with 2D array is nested loop n loop within another loop n Let's write program to n load array with values shown n print contents of array rows columns

Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { } rows columns

Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; } rows columns

Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } } rows columns

Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } } rows columns

Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } for (int col = 0; col < multTable[row].length; col++){ System.out.print(multTable[row][col] + " "); } } rows columns

Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++){ System.out.print(multTable[row][col] + " "); } } rows columns

Two-Dimensional Arrays public class ArrayTest5 { public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } for (int row = 0; row < multTable.length; row++){ for (int col = 0; col < multTable[row].length; col++){ System.out.print(multTable[row][col] + " "); } System.out.println(); } rows columns