© 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
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Advertisements

Interfaces A Java interface is a collection
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
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
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
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,
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
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.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
COMP 14: decomposition, overloading, relationships June 7, 2000 Nick Vallidis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
ECE122 L17: Method Development and Testing April 5, 2007 ECE 122 Engineering Problem Solving with Java Lecture 17 Method Development and Testing.
Methods in Java Selim Aksoy Bilkent University Department of Computer Engineering
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
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.
Writing Classes (Chapter 4)
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
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.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
Lecture 4 b Writing Classes b class declarations b method declarations b constructors b instance variables b encapsulation b method overloading b program.
© 2006 Pearson Education C4 D6 Obj: section 4.4, to learn to divide complicated methods into simpler, supporting methods HW: p. 246 #4.7 – 4.10 Do Now:
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
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.
© 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.
© 2006 Pearson Education C4 D7 Obj: to describe relationships between objects HW: p.250 #4.19 Do Now: p.248 True/False #4.6 – 4.10.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 5: Enhancing Classes
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
CSCI 1100/1202 April 10,
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 5: Enhancing Classes
Chapter 4 Writing Classes.
CS139 October 11, 2004.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
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 Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2006 by John Lewis, William Loftus, and Cara Cocking. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes. *AP is a registered trademark of The College Entrance Examination Board which was not involved in the production of, and does not endorse, this product. Part 3

© 2006 Pearson Education Preconditions and Postconditions  A precondition is a condition that should be true when a method is called  A postcondition is a condition that should be true when a method finishes executing  These conditions are expressed in comments above the method header

© 2006 Pearson Education 3 Constructors Revisited  constructor = special method used to initialize a newly created object  When writing a constructor, remember that: it has the same name as the class it does not return a value it has no return type, not even void it typically sets the initial values of instance variables  Not necessary to define a constructor for a class, the default will be used.

© 2006 Pearson Education Local Data  Local variables can be declared inside a method  The formal parameters of a method create automatic local variables when the method is invoked  When the method finishes, all local variables are destroyed (including the formal parameters)  Keep in mind that instance variables, declared at the class level, exists as long as the object exists  Any method in the class can refer to instance data

© 2006 Pearson Education Accessors and Mutators  Since instance data usually has private visibility, it can only be accessed through methods  An accessor method provides read-only access to a particular value  A mutator method changes a particular value  For a data value X, accessor and mutator methods are usually named getX and setX

© 2006 Pearson Education 6 Overloading Methods  Method overloading is the process of using the same method name for multiple methods  The signature of each overloaded method must be unique  The signature includes the number, type, and order of the parameters  The compiler determines which version of the method is being invoked by analyzing the parameters  The return type of the method is not part of the signature

© 2006 Pearson Education Overloading Methods double tryMe (int x) { return x +.375; } Version 1 double tryMe (int x, double y) { return x*y; } Version 2 result = tryMe (25, 4.32) Invocation

© 2006 Pearson Education 8 Overloaded Methods  The println method is overloaded: println (String s) println (int i) println (double d) and so on...  The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println (total);

© 2006 Pearson Education 9 Overloading Methods  Constructors can be overloaded  Overloaded constructors provide multiple ways to initialize a new object  See SnakeEyes.java (page 221)SnakeEyes.java  See Die.java (page 222)Die.java

© 2006 Pearson Education Method Decomposition  A method should be relatively small, so that it can be understood as a single entity  A potentially large method should be decomposed into several smaller methods as needed for clarity  A service method of an object may call one or more support methods to accomplish its goal  Support methods could call other support methods if appropriate

© 2006 Pearson Education Pig Latin  The process of translating an English sentence into Pig Latin can be decomposed into the process of translating each word  The process of translating a word can be decomposed into the process of translating words that begin with vowels begin with consonant blends (sh, cr, tw, etc.) begins with single consonants  See PigLatin.java (page 224)PigLatin.java  See PigLatinTranslator.java (page 225)PigLatinTranslator.java

© 2006 Pearson Education Object Relationships  Objects can have various types of relationships to each other  A general association is sometimes referred to as a use relationship  A general association indicates that one object (or class) uses or refers to another object (or class) in some way Author Book writes

© 2006 Pearson Education Object Relationships  Some use associations occur between objects of the same class  For example, we might add two Rational number objects together as follows: r3 = r1.add(r2);  One object ( r1 ) is executing the method and another ( r2 ) is passed as a parameter  See RationalNumbers.java (page 229)RationalNumbers.java  See Rational.java (page 231)Rational.java

© 2006 Pearson Education Using classes  An Account object contains a reference to a String object (the owner's name)  This represents a has-a relationship  A bank account has a name  Likewise, a student may have one or more addresses  See StudentBody.java (page 235)StudentBody.java  See Student.java (page 236)Student.java  See Address.java (page 237)Address.java

© 2006 Pearson Education Applet Methods  In previous examples we've used the paint method of the Applet class to draw on an applet  The Applet class has several methods that are invoked automatically at certain points in an applet's life  The init method, for instance, is executed only once when the applet is initially loaded  The start and stop methods are called when the applet becomes active or inactive  The Applet class also contains other methods that generally assist in applet processing

© 2006 Pearson Education Graphical Objects  Any object we define by writing a class can have graphical elements  The object must simply obtain a graphics context (a Graphics object) in which to draw  An applet can pass its graphics context to another object just as it can any other parameter  See LineUp.java (page 240)LineUp.java  See StickFigure.java (page 242)StickFigure.java

© 2006 Pearson Education Summary  Chapter 4 has focused on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing method overloading method decomposition graphics-based objects