Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: 8.1-8.3 self-checks: Ch. 8 #1-9 exercises:

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

1 Building Java Programs Chapter 8: Classes These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-1: Intro to Classes and Objects reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: #1-9 exercises: #1-4.
Building Java Programs Chapter 8 Classes. 2 A programming problem Given a file of cities' (x, y) coordinates, which begins with the number of cities:
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Based on slides at buildingjavaprograms.com Objects and Classes, take 2 Managing Complexity with Programmer-Defined Types  Classes are programmer-defined.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: #1-9 exercises: #1-4.
1 CIS 2168 Data Structures and Algorithms in JAVA Brief Introduction to JAVA.
Objects.
1 Object state: fields. Fields field: A variable inside an object that represents part of its internal state.  Each object will have its own copy of.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects; Fields and Methods reading: 8.1 – 8.2.
Building Java Programs Chapter 8 Classes Copyright (c) Pearson All rights reserved.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Object Oriented Programming
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 10: Intro to Classes
CSE 143 Lecture 3 More ArrayList; object-oriented programming
Building Java Programs Chapter 8
Classes and Objects, State and Behavior
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Topic 27 classes and objects, state and behavior
A programming problem Given a file of cities' (x, y) coordinates, which begins with the number of cities: Write.
Chapter 8 Lecture 8-1: Classes and Objects
Building Java Programs
Classes and objects Readings: 8.1.
References and objects
Building Java Programs
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 11: Intro to Classes
Building Java Programs
Building Java Programs
Building Java Programs
A programming problem Given a file of cities' (x, y) coordinates, which begins with the number of cities: Write.
Building Java Programs
Building Java Programs
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Building Java Programs
CSE 143 Lecture 3 More ArrayList; object-oriented programming
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Building Java Programs Chapter 8
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: Ch. 8 #1-9 exercises: Ch. 8 #1-4

Copyright 2008 by Pearson Education 2 Problem Declaring same group of related variables several times in a program int x1 = 3; int y1 = 5; int x2 = 12; int y2 = 4; Annoying and redundant Unclear and hard to keep track of variables

Copyright 2008 by Pearson Education 3 Solution: Objects Group together related variables into an object Like creating your own data structure out of Java building blocks public class { ; } Syntax to use this data structure: = new ();

Copyright 2008 by Pearson Education 4 Solution: Objects Group together related variables into an object Like creating your own data structure out of Java building blocks public class Point { int x; int y; } Syntax to use this data structure: Point p1 = new Point();

Copyright 2008 by Pearson Education 5 Two Uses for Java Classes class: A program entity that represents either: 1.A program / module, or 2.A template for a new type of objects. The DrawingPanel class is a template for creating DrawingPanel objects. object: An entity that combines state and behavior

Copyright 2008 by Pearson Education 6 Java class: Program An executable program with a main method Can be run; statements execute procedurally What we’ve been writing all quarter public class BMI2 { public static void main(String[] args) { giveIntro(); Scanner console = new Scanner(System.in); double bmi1 = getBMI(console); double bmi2 = getBMI(console); reportResults(bmi1, bmi2); }... }

Copyright 2008 by Pearson Education 7 Java class: Object Definition A blueprint for a new data type Not executable, not a complete program Created objects are an instance of the class Blueprint: public class Point { int x; int y; } Instance: Point p1 = new Point();

Copyright 2008 by Pearson Education 8 Blueprint analogy iPod blueprint state: current song volume battery life behavior: power on/off change station/song change volume choose random song iPod #1 state: song = “ Octopus’s Garden " volume = 17 battery life = 2.5 hrs behavior: power on/off change station/song change volume choose random song iPod #2 state: song = “Lovely Rita" volume = 9 battery life = 3.41 hrs behavior: power on/off change station/song change volume choose random song iPod #3 state: song = “For No One" volume = 24 battery life = 1.8 hrs behavior: power on/off change station/song change volume choose random song create s

Copyright 2008 by Pearson Education 9 Abstraction abstraction: A distancing between ideas and details. We can use objects without knowing how they work. abstraction in an iPod: You understand its external behavior (buttons, screen). You don't understand its inner details, and you don't need to.

Copyright 2008 by Pearson Education 10 Client and Object Classes client program: A program that uses objects. Example: HW6 Names is a client of DrawingPanel and Graphics. object: An entity that combines state and behavior state: data fields behavior: methods

Copyright 2008 by Pearson Education 11 The Object Concept procedural programming: Programs that perform their behavior as a series of steps to be carried out object-oriented programming (OOP): Programs that perform their behavior as interactions between objects Takes practice to understand the object concept

Copyright 2008 by Pearson Education 12 Fields field: A variable inside an object that is part of its state. Each object has its own copy of each field. Clients can access/modify an object's fields access:. modify:. = ; Example: Point p1 = new Point(); Point p2 = new Point(); System.out.println("the x-coord is " + p1.x); // access p2.y = 13; // modify

Copyright 2008 by Pearson Education 13 Behavior Objects can tie related data and behavior together instance method: A method inside an object that operates on that object public ( ) { ; } Syntax to use method:. ( ); Example: p1.translate(11, 6);

Copyright 2008 by Pearson Education 14 Implicit Parameter Each instance method call happens on a particular object. Example: p1.translate(11, 6); The code for an instance method has an implied knowledge of what object it is operating on. implicit parameter: The object on which an instance method is called. Can be referred to inside the object using this keyword

Copyright 2008 by Pearson Education 15 Accessors accessor: An instance method that provides information about the state of an object. Example: public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); } This gives clients "read-only" access to the object's fields.

Copyright 2008 by Pearson Education 16 Mutators mutator: An instance method that modifies the object’s internal state. Example: public void translate(int dx, int dy) { x += dx; y += dy; } This gives clients both read and write access to code.