Topics to cover Instance variables vs. local variables.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

JAVA Revision Lecture Electronic Voting System Marina De Vos.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Programming Languages and Paradigms The C Programming Language.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Using Classes to Store Data Computer Science 2 Gerb.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
Written by: Dr. JJ Shepherd
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Introduction to Programming with Java, for Beginners Scope.
The different kinds of variables in a Java program.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Values, variables and types © Allan C. Milne v
Chapter 2 Practice.
Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Creating and Using Class Methods. Definition Class Object.
Chapter 2. Variable and Data type
CIS162AD Constructors Part 2 08_constructors.ppt.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Lecture 3: More on Classes Textbook: Chapter 2 Recap: –Classes vs. Objects –Q: What comes first a class or an object? –Q: Do we need a class to create.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
CS0004: Introduction to Programming
Objects as a programming concept
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Classes and Objects.
Examples of Classes & Objects
AKA the birth, life, and death of variables.
C Functions -Continue…-.
Function There are two types of Function User Defined Function
Using local variable without initialization is an error.
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Classes & Objects: Examples
Variables and Their scope
Local Variables, Global Variables and Variable Scope
Static in Classes CSCE 121 J. Michael Moore.
Tonga Institute of Higher Education
Defining Classes and Methods
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Parameters and Arguments
Scope Rules.
Chapter 4 Test Review First day
Presentation transcript:

Topics to cover Instance variables vs. local variables

A new method in the Person class public String predictFuture () { int magicNumber = (int) (Math.random() * 4 + 1); String prediction = "Yours is the life of a student"; switch(magicNumber) { case 1: prediction = "You will do well in life"; break; case 2: prediction = "You will be rich and famous"; break; case 3: prediction = "Happiness will be yours"; break; case 4: prediction = "You will be a programmer"; break; case 5: prediction = "Life is not kind"; break; } return prediction;

Local Variables Declared within the body of a method Have “local scope” Can only be used in the method in which they are declared Local variables can be declared in any block of code

Instance Variables Are declared at the start of the class definition outside of any interior block of code Are used to store the “state” of an object Can be used by any method in the class Have unique values for each object