CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 11, Fri Jan
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 13, Wed Feb
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CSC 212 – Data Structures Lecture 3: Fields, Methods, & Constructors.
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.
Introduction to Programming Writing Java Beginning Java Programs.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
CSC 212 – Data Structures Lecture 12: Java Review.
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
CSC 212 Object-Oriented Programming and Java Part 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture 2: Classes and Objects, using Scanner and String.
Classes CS 21a: Introduction to Computing I First Semester,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
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 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
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.
ECE122 Feb. 22, Any question on Vehicle sample code?
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
Introduction to Programming Writing Java Beginning Java Programs.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSC 212 Object-Oriented Programming and Java Part 2.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
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 & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
LECTURE 8: EXCEPTIONS CSC 212 – Data Structures. Error Handling Goals  What should we do when an error occurs?  Should alert system to the error  May.
Question of the Day completes starts  What word completes the 1 st word & starts the 2 nd one: DON???CAR.
Topics Instance variables, set and get methods Encapsulation
CSC 212 – Data Structures Lecture 6: Static and non-static members.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Problem of the Day  Why are manhole covers round?
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
CSC Programming for Science Lecture 5: Actual Programming.
Variable Scope & Lifetime
Objects as a programming concept
Lecture 3 John Woodward.
Lecture 3: Fields, Methods, & Constructors
More Object Oriented Programming
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Java Programming Language
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Chap 2. Identifiers, Keywords, and Types
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CSC 212 – Data Structures Lecture 5: Variables

Problem of the Day Why do underground subway stations always have more escalators going up than down?

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.

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

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

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, ,“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  identifier  “2408” richOne  0.05  “2408”

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 paramName paramDescript

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

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, ); owner balance  0.05 identifier  “2408” check haxx0r owner balance  identifier  “2408” richOne this source amount  balance  

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  identifier  “2408” check haxx0r owner balance  identifier  “2408” richOne this source amount  ~3.4·10 38  

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

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

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; } } }

Your Turn Get back into groups and try daily activity

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

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