Download presentation
Presentation is loading. Please wait.
Published byJerome Lester Modified over 9 years ago
1
CSC 212 – Data Structures Lecture 5: Variables
2
Problem of the Day Why do underground subway stations always have more escalators going up than down?
3
Keys To Academic Success Cannot control class times, but can control when you wake up Complete in-class activities & homeworks Review answers versus actual solutions When you cannot explain differences, ask Read directions and follow them explicitly Ask whenever something is unclear Practice, practice, practice Do problems in book, from past classes, etc.
4
Variables All variables behave in similar manner Assignment to primitive --- copy value Assignment to reference --- alias instance But further divide variables by “lifetime” Fields Parameters Locals How each of these used very different… ...but correct use makes coding & debugging simpler
5
Fields (redux) Fields declared in class, but not in method By convention, declarations at start of class Should be declared private (or protected) * Document with javadoc or normal comments Each class instance have the fields Field values in separate instances may * differ Objects’ fields initialized at instantiation Initial value is 0, false, ‘’, or null
6
Tracing with Objects public class BankAccount { private Client owner; private float balance; private String identifier; BankAccount check = new BankAccount(haxx0r, 1.25, “0001”); BankAccount richOne = new BankAccount(billG, 9000000,“2408”); check.balance -= 1.20; // Bought a coffee check.identifier = “2408”; // 1 st theft attempt richOne.owner = haxx0r; // 2 nd theft attempt owner balance 1.25 identifier “0001” check haxx0r billG owner balance 9000000 identifier “2408” richOne 0.05 “2408”
7
Parameters Variables named in method description Created & initialized each time method called “Live” until method completes Initialized with value of argument Primitives copy value into parameter References parameter aliases argument Documented as part of method comments Done using @param paramName paramDescript
8
Tracing Methods & Parameters Every method call, draw new frame Write parameters in at top of frame (Include this as a parameter) * Initialize parameters using arguments Frame only used to trace that single call “Erase” frame when call completes/returns Usually just cross it out to mark as done
9
Tracing Parameters public void badTransfer(BankAccount source, float amount) { if (amount >= source.balance) { amount = source.balance(); source = null; } else { source.balance -= amount; } balance += amount; } check.badTransfer(richOne, 1000000); owner balance 0.05 identifier “2408” check haxx0r owner balance 9000000 identifier “2408” richOne this source amount 1000000 balance 8000000 1000000.05
10
Tracing Parameters public void badTransfer(BankAccount source, float amount) { if (amount >= source.balance) { amount = source.balance(); source = null; } else { source.balance -= amount; } balance += amount; } check.badTransfer(richOne, Float.MAX_VALUE); owner balance 1000000.05 identifier “2408” check haxx0r owner balance 8000000 identifier “2408” richOne this source amount ~3.4·10 38 8000000 9000000.05
11
Locals Variables declared inside a method Required to assigned value before using “Live” only for code block in which declared Cannot be included in javadoc Local variables help a method complete Only comment as part of describing how method works
12
Tracing Locals Add local in frame for current call to method “Erase” local once the code block completes Code block is one or more statements enclosed by braces
13
Code Blocks public void goodTransfer(BankAccount source, float amount) { if (amount >= source.getBalance) { amount = source.getBalance(); } else { source.balance -= amount; } balance += amount; for (int attempt = 0; attempt < 4; attempt++) { if (source.balance <= amount) { source.balance -= amount; break; } } }
14
Your Turn Get back into groups and try daily activity
15
Before Next Lecture… Finish week #2 homework Finish lab #1 Monday’s lecture continues with the difference between static and non-static data Keep thinking & ask any questions you have May want to review any old notes & handouts
16
Before Next Lecture… Finish week #2 homework Finish lab #1 Monday’s lecture continues with difference between static and non-static fields and methods Keep thinking & ask any questions you have May want to review any old notes & handouts
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.