© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,

Slides:



Advertisements
Similar presentations
This is Java Jeopardy Writing Methods…chapter 4…
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Java Software Solutions
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Chapter 4: Writing Classes. 2 Objects  An object has: state - descriptive characteristics behaviors - what it can do (or what can be done to it)  For.
Chapter 4: Writing Classes
Chapter 4: Writing Classes
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 3: OOD and Writing Worker Classes Clark Savage Turner, Ph.D. Copyright 2003, Csturner, adapted from notes.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes, Encapsulation, Methods and Constructors
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Objects and Classes Objects and Classes objects defining classes method declaration object references and aliases instance variables encapsulation and.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
1 (c) elsaddik CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Appendix B: Object-Oriented Design Java Software Structures: Designing.
Writing Classes (Chapter 4)
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Programming in Java (COP 2250) Lecture 9 Chengyong Yang Fall, 2005.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
SEEM Java – Basic Introduction, Classes and Objects.
Lecture 4 b Writing Classes b class declarations b method declarations b constructors b instance variables b encapsulation b method overloading b program.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Chapter 4: Writing Classes. 2 b We've been using predefined classes. Now we will learn to write our own classes to define new objects b Chapter 4 focuses.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Writing Classes We have talked about classes and objects.
Anatomy of a Class & Method
Classes A class is a blueprint of an object
Chapter 4: Writing Classes
Chapter 5 – Writing Classes
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Chapter 4 Writing Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
What to expect this week
Chapter 4: Writing Classes
Presentation transcript:

© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus, and Cara Cocking

© 2006 Pearson Education 2 Writing Classes  We've been using predefined classes. Now we will learn to write our own classes to define objects  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing method overloading method decomposition graphics-based objects

© 2006 Pearson Education 3 Objects  An object has: state - descriptive characteristics behaviors - what it can do (or what can be done to it)  For example, consider a coin that can be flipped so that it's face shows either "heads" or "tails" state = (heads or tails) behavior = can be flipped  Note that the behavior of the coin might change its state

© 2006 Pearson Education 4 Classes  A class is a blueprint of an object  It is the model or pattern from which objects are created  For example, the String class is used to define String objects  Each String object contains specific characters (its state)  Each String object can perform services (behaviors) such as toUpperCase

© 2006 Pearson Education Classes  The String class was provided for us by the Java standard class library  But we can also write our own classes that define specific objects that we need  For example, suppose we want to write a program that simulates the flipping of a coin  We can write a Coin class to represent a coin object

© 2006 Pearson Education Classes  A class contains data declarations and method declarations int x, y; char ch; Data declarations Method declarations

© 2006 Pearson Education The Coin Class  In our Coin class we could define the following data: face, an integer that represents the current face HEADS and TAILS, integer constants that represent the two possible states  We might also define the following methods: a Coin constructor, to initialize the object a flip method, to flip the coin a isHeads method, to determine if the current face is heads a toString method, to return a string description for printing

© 2006 Pearson Education The Coin Class  See CountFlips.java (page 199)CountFlips.java  See Coin.java (page 200)Coin.java  Note that the CountFlips program did not use the toString method  A program will not necessarily use every service provided by an object  Once the Coin class has been defined, we can use it again in other programs as needed

© 2006 Pearson Education Data Scope  The scope of data is the area in a program in which that data can be used (referenced)  Data declared at the class level can be used by all methods in that class  Data declared within a method can be used only in that method  Data declared within a method is called local data

© 2006 Pearson Education Instance Data  The face variable in the Coin class is called instance data because each instance (object) of the Coin class has its own  A class declares the type of the data, but it does not reserve any memory space for it  Every time a Coin object is created, a new face variable is created as well  The objects of a class share the method definitions, but each has its own data space  That's the only way two objects can have different states

© 2006 Pearson Education Instance Data See FlipRace.java (page 203)FlipRace.java