Download presentation
Presentation is loading. Please wait.
Published byArabella Thornton Modified over 9 years ago
1
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
2
“AP Computer Science with.NET and J#” Lecture 6: Keeping Track of Your Objects using Collection Classes Microsoft.NET Workshops for Teachers
3
6-3 MicrosoftAP Computer Science with.NET and J# Workshop Track LectureTopic 1Java — History, Philosophy, and Overview 2Procedural Programming in Java and J# 3Object-oriented Programming in Java and J# 4Primitive Types vs. Object References (==, equals, toString, arrays) 5Writing Your Own Classes 6Keeping Track of Your Objects with the Collection Classes 7Algorithms and Algorithm Analysis 8Debugging and Exception Handling...... 18Collection Classes and Iteration 19Additional Resources and Ideas
4
6-4 MicrosoftAP Computer Science with.NET and J# Lecture — Objectives “As you create more and more objects, you’ll need a way to keep track of them all. Enter the Collection classes…” Topics: –Arrays revisited –ArrayLists [ Joe Hummel, Lake Forest College ]
5
6-5 MicrosoftAP Computer Science with.NET and J# Part 1 Arrays Revisited
6
6-6 MicrosoftAP Computer Science with.NET and J# Arrays Arrays are the most well-known collection class –Available in almost all programming languages –Straightforward to understand indexing mechanism int[] quizzes; quizzes = new int[10]; quizzes[0] =...; quizzes[1] =...;. quizzes[9] =...; int sum = 0; for (int i = 0; i < quizzes.length; i++) // for each element in array... sum = sum + quizzes[i]; double avg; avg = sum / 10.0; int[] quizzes; quizzes = new int[10]; quizzes[0] =...; quizzes[1] =...;. quizzes[9] =...; int sum = 0; for (int i = 0; i < quizzes.length; i++) // for each element in array... sum = sum + quizzes[i]; double avg; avg = sum / 10.0; … 0 1 2 9 0 0
7
6-7 MicrosoftAP Computer Science with.NET and J# Arrays of Objects Recall the difference between primitive & object types… Implication? –An array of objects is an array of references ! Account[] bank; bank = new Account[100]; bank[0] = new Account(12345, 99216, 1020.53); bank[1] = new Account(23456, 56712, 100.00);. Account[] bank; bank = new Account[100]; bank[0] = new Account(12345, 99216, 1020.53); bank[1] = new Account(23456, 56712, 100.00);. Account ————— 12345 99216 1020.53 … 0 1 2 99 Account ————— 23456 56712 100.00 public class Account { public int accountNumber; public int customerID; public double balance; public class Account { public int accountNumber; public int customerID; public double balance;
8
6-8 MicrosoftAP Computer Science with.NET and J# Working with Arrays of Objects Example: –Deposit $50.00 into bank account #42717 Account[] bank; bank = new Account[100]; bank[0] = new Account(12345, 99216, 1020.53); bank[1] = new Account(23456, 56712, 100.00);. for (int i = 0; i < bank.length; i++) // for each account... { if (bank[i].accountNumber == 42717) // found it! { bank[i].deposit(50.00); System.out.println(bank[i].toString()); break; // exit loop now, there’s no reason to keep searching } Account[] bank; bank = new Account[100]; bank[0] = new Account(12345, 99216, 1020.53); bank[1] = new Account(23456, 56712, 100.00);. for (int i = 0; i < bank.length; i++) // for each account... { if (bank[i].accountNumber == 42717) // found it! { bank[i].deposit(50.00); System.out.println(bank[i].toString()); break; // exit loop now, there’s no reason to keep searching } public class Account { public int accountNumber; public int customerID; public double balance; public class Account { public int accountNumber; public int customerID; public double balance;
9
6-9 MicrosoftAP Computer Science with.NET and J# Demo! Working with an array of bank Account objects…
10
6-10 MicrosoftAP Computer Science with.NET and J# Searching an Array of Objects Let’s generalize the previous example… Here’s a general search method callable by main: –Returns a reference to bank account object if found –Returns null ( no object ) if bank account object is not found public static Account search(Account[] bank, int accountNumber) { Account acct; // this will refer to Account object once we find it acct = null; // initialize to reference *no object* for (int i = 0; i < bank.length; i++) { if (bank[i].accountNumber == accountNumber) { acct = bank[i]; // copy reference to this object into local acct variable break; // now exit loop so we can return reference! } return acct; } public static Account search(Account[] bank, int accountNumber) { Account acct; // this will refer to Account object once we find it acct = null; // initialize to reference *no object* for (int i = 0; i < bank.length; i++) { if (bank[i].accountNumber == accountNumber) { acct = bank[i]; // copy reference to this object into local acct variable break; // now exit loop so we can return reference! } return acct; }
11
6-11 MicrosoftAP Computer Science with.NET and J# Calling Search Given previous search method, here’s how to call it: –to deposit $50.00 into bank account #42717 public static void main(String[] args) throws Exception { Account[] bank; bank = new Account[100]; bank[0] = new Account(12345, 99216, 1020.53); bank[1] = new Account(23456, 56712, 100.00);. Account acct; acct = search(bank, 42717); if (acct == null) System.out.println("Bank account does not exist!"); else { acct.deposit(50.00); System.out.println(acct.toString()); } public static void main(String[] args) throws Exception { Account[] bank; bank = new Account[100]; bank[0] = new Account(12345, 99216, 1020.53); bank[1] = new Account(23456, 56712, 100.00);. Account acct; acct = search(bank, 42717); if (acct == null) System.out.println("Bank account does not exist!"); else { acct.deposit(50.00); System.out.println(acct.toString()); }
12
6-12 MicrosoftAP Computer Science with.NET and J# Demo! Searching an array of bank Account objects…
13
6-13 MicrosoftAP Computer Science with.NET and J# Part 2 ArrayLists
14
6-14 MicrosoftAP Computer Science with.NET and J# Arraylists ArrayLists represent an improved Array class –An ArrayList grows dynamically in size — there is no upper-bound –An ArrayList contains methods for searching, sorting, … –one of the trade-offs is that you lose the convenient [ ] syntax… import java.util.*; ArrayList bank; bank = new ArrayList(); bank.add( new Account(12345, 99216, 1020.53) ); // adds to end bank.add( new Account(23456, 56712, 100.00) );. for (int i = 0; i < bank.size(); i++) System.out.println( bank.get(i).toString() ); import java.util.*; ArrayList bank; bank = new ArrayList(); bank.add( new Account(12345, 99216, 1020.53) ); // adds to end bank.add( new Account(23456, 56712, 100.00) );. for (int i = 0; i < bank.size(); i++) System.out.println( bank.get(i).toString() ); ArrayList
15
6-15 MicrosoftAP Computer Science with.NET and J# Working with ArrayLists ArrayLists represent a generic collection –This means they can be used to hold *any* kind object Implication? –You often need to use type-casting when retrieving objects… ArrayList bank; bank = new ArrayList();. for (int i = 0; i < bank.size(); i++) // for each account... { Account acct; acct = (Account) bank.get(i); if (acct.accountNumber == 42717) // found it! { acct.deposit(50.00); System.out.println(acct.toString()); break; } ArrayList bank; bank = new ArrayList();. for (int i = 0; i < bank.size(); i++) // for each account... { Account acct; acct = (Account) bank.get(i); if (acct.accountNumber == 42717) // found it! { acct.deposit(50.00); System.out.println(acct.toString()); break; }
16
6-16 MicrosoftAP Computer Science with.NET and J# Demo! Working with an ArrayList of bank Account objects…
17
6-17 MicrosoftAP Computer Science with.NET and J# Searching an ArrayList Much like searching an Array –Returns reference to object if found, null if not… –we’re going to ignore the built-in search methods for the time being… public static Account search(ArrayList bank, int accountNumber) { for (int i = 0; i < bank.size(); i++) // for each account... { Account acct; acct = (Account) bank.get(i); if (acct.accountNumber == accountNumber) // found it return acct; // return reference to this object now! } // if get here, we searched the entire collection and the account was not found... return null; } public static Account search(ArrayList bank, int accountNumber) { for (int i = 0; i < bank.size(); i++) // for each account... { Account acct; acct = (Account) bank.get(i); if (acct.accountNumber == accountNumber) // found it return acct; // return reference to this object now! } // if get here, we searched the entire collection and the account was not found... return null; }
18
6-18 MicrosoftAP Computer Science with.NET and J# Demo! Searching an ArrayList of bank Account objects…
19
6-19 MicrosoftAP Computer Science with.NET and J# Summary Collection classes keep track of data Most common collection classes: –Arrays –ArrayLists Arrays: –Well-understood, available in almost all programming languages ArrayLists: –More powerful –More in line with object-oriented programming –Requires object-based interaction — you lose array-based [ ] syntax
20
6-20 MicrosoftAP Computer Science with.NET and J# Resources Web site for slides, demos, associated lab exercises: –http://blogs.msdn.com/daryllmc/default.aspxhttp://blogs.msdn.com/daryllmc/default.aspx –http://www.lakeforest.edu/~hummel/workshops-HS.htmhttp://www.lakeforest.edu/~hummel/workshops-HS.htm –https://www.mainfunction.com/home/training/https://www.mainfunction.com/home/training/
21
6-21 MicrosoftAP Computer Science with.NET and J# That’s it! Next up: LectureTopic.. 7Algorithms and Algorithm Analysis......
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.