Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester, 2013-2014.

Slides:



Advertisements
Similar presentations
Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester,
Advertisements

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.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Java Programming Review (Part I) Enterprise Systems Programming.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Introduction to Object-Oriented Programming
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.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSC Programming I Lecture 8 September 9, 2002.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
Chapter 4: A Paradigm Program structure Connecting to the Java world Types Access modifiers Lifetime modifiers.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Introduction to Java Java Translation Program Structure
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
Chapter 4 Introduction to Classes, Objects, Methods and strings
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Class Relationships and Object Interaction CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Java Basics Variables, Expressions, Statements, etc. CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo.
Programming Languages and Paradigms Activation Records in Java.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Methods.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
Chapter 3 Implementing Classes
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3 John Woodward.
Classes and OOP.
Yanal Alahmad Java Workshop Yanal Alahmad
Implementing Classes Yonglei Tao.
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter Three - Implementing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Methods/Functions.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,

Object-Oriented Programming ► The traditional definition of a program is: a sequence of instructions to be executed on a computer ► In the Object-Oriented Programming (OOP) paradigm, a program that executes is a collection of interacting objects ► In this paradigm, the programs we specify are what are in these objects and how these objects behave

So … What is an Object? ► A "thing" that has type, identity, state, and behavior ► type: belongs to a "class" of similar objects ► identity: is a distinct "instance" of a class of objects ► state / attributes: has a set of properties (a.k.a fields) ► each field can have different values ► behavior: has "methods" (things that the object knows how to do) ► we say we "call" a method on the object

Examples of Objects ► state/attributes ► on (true or false) ► behavior ► switch on ► switch off ► check if on LightBulb BankAccount ► state/attributes ► balance ► behavior ► deposit ► withdraw ► check balance Car ► state/attributes ► # of liters of gas in tank ► total # of km run so far ► efficiency (km/liter) ► behavior ► drive ► load gas ► change efficiency ► check gas ► check odometer reading Note ► each object is an "instance" of that "type" of object ► each instance has its own values for its attributes ► e.g., different accounts can have different balances

BankAccount Example (A Preview) public class BankAccount { private double balance = 0; public double getBalance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } … } BankAccount ► state/attributes ► balance ► behavior ► deposit ► withdraw ► check balance BankAccount double balance double getBalance() void deposit( double amount ) … and more BankAccount.java

Class Definition in Java ► type (or class) ► state/attributes (fields) ► behavior (methods) ► may have input parameters in parenthesis ► may have output (or "return") type ► has "body" with code public class BankAccount { private double balance = 0; public double getBalance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } … } BankAccount.java BankAccount "double" means a floating point number like

A Class with a Constructor public class BankAccount { private double balance; public BankAccount() { balance = 0; } public double getBalance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } … } BankAccount.java ► Constructor: special method that handles initialization ► For now, view constructors as an alternative to initializing fields as they are declared ► Later: more advanced uses for constructors

A Class and Its Instances ► A single class can have multiple instances ► Each instance is a separate object ► Each instance can have different values for its fields ► The definition of methods is the same for all instances of the same type ► Thus, there is only one class definition ► Written as the.java file for that class

Lab Exercise: Try it in BlueJ ► Create and compile a BankAccount class (BankAccount.java) ► Create BankAccount objects (instances of the class) ► Right-click on the class ► Carry out operations on the BankAccount objects (invoke the deposit and getBalance methods) ► Right-click on the instances

Lab Exercise, continued ► Instantiate and use a BankAccount object in a Java application (use the command prompt) ► How? In the same folder containing the BankAccount class, create BankSystem.java ► Inside the public static void main(String args[]) method of BankSystem.java, type the following code…

Lab Exercise, continued public static void main( String args[] ) { BankAccount b = new BankAccount(); b.deposit( ); b.withdraw( ); System.out.println( b.getBalance() ); b.deposit( ); System.out.println( b.getBalance() ); } ► Compile and execute BankSystem.java ► The values and should be printed out

Summary ► In Java, we write programs for objects ► These programs are called classes ► A class consists of fields and methods to specify the state and behavior for its objects ► Once a class has been defined, objects of that class can be created (instantiated) ► Methods are invoked on an object, and may cause the state of the object to change