Classes, Encapsulation, Methods and Constructors (Continued)

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

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
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,
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes The programs we’ve written in previous examples have.
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 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Methods in Java Selim Aksoy Bilkent University Department of Computer Engineering
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.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved2/48 Writing Classes We've been using predefined classes. Now we will learn.
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.
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.
Procedural programming in Java Methods, parameters and return values.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Wednesday –POD –I have updated grades in powerschool. If you have a zero for a lab grade, it probably means you didn’t DropItToMe. Please do so. –Slides.
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.
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.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
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).
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter VII: Arrays.
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Writing Classes Chapter 4.
Writing Classes We've been using predefined classes from the Java API. Now we will learn to write our own classes. Chapter 4 focuses on: class definitions.
Chapter 4: Writing Classes
Lecture 14 Writing Classes part 2 Richard Gesick.
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.
Chapter 4: Writing classes
Group Status Project Status.
Chapter 4 Writing Classes
Anatomy of a Method.
Writing Classes.
Chapter 4 Writing Classes.
CS139 October 11, 2004.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Chapter 4: Writing Classes
Presentation transcript:

Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and parameter passing Constructors Method Overloading Reading for this lecture:L&L, 4.1-4.5 & App E

Method Declarations A method declaration specifies the code that will be executed when the method is invoked (called) When a method is invoked, the flow of control jumps to the method and executes its code When complete, the flow returns to the place where the method was called and continues The invocation may or may not return a value, depending on how the method is defined

Method Control Flow If the called method is in the same class, only the method name is needed myMethod(); myMethod compute

Method Control Flow The called method is often part of another class or object obj.doIt(); main doIt helpMe helpMe();

Method Header A method declaration begins with a method header char calc (int num1, int num2, String message) method name parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal parameter return type

Method Body The method header is followed by the method body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } The method body: { …code… }

Local Data Local variables can be declared inside a method The formal parameters of a method are also 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/object level, exist for as long as the object exists

Method Body The method header is followed by the method body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } sum and result are local variables, So are the parameters num1, num2, message They are created each time the method is called, and are destroyed when it finishes executing

The return Statement The return type of a method indicates the type of value that the method sends back to the caller A method that does not return a value has a void return type A return statement specifies the value that will be returned upon completion of the method code return expression; Its expression must conform to the return type

Method Body The method header is followed by the method body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } The return expression must be consistent with the return type (here, both of type char)

Parameters When a method is called, the actual parameters in the call are copied into the formal parameters in the method header ch = obj.calc (25, count, "Hello"); char calc (int num1, int num2, String message) { // Here num1 = 25, num2 = count’s value, // message = "Hello" int sum = num1 + num2; char result = message.charAt (sum); return result; }

Parameters When a method is called, the actual parameters in the call are copied into the formal parameters in the method header ch = obj.calc (25, count, "Hello"); char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } The return value becomes the value of the call expression obj.calc(…)

Objects as Parameters Another important issue related to method design involves parameter passing Parameters in a Java method are passed by value A copy of the actual parameter (the value passed in) is stored into the formal parameter (in the method header) Therefore passing parameters is similar to an assignment statement When an object is passed to a method, the actual parameter and the formal parameter become aliases of each other

Passing Objects to Methods What a method does with a parameter may or may not have a permanent effect (outside the method) public void setDieHigh(Die d) // in class XXX { d.setFaceValue(6); } Calling code: having an obj of type XXX, can call setDieHigh, passing a Die object to it to change Die die1 = new Die(); // faceValue = 1 obj.setDieHigh(die1); // makes faceValue = 6

Method Overloading Method overloading is the process of giving a single method name multiple definitions If a method is overloaded, the method name is not sufficient to determine which method is being called The signature of each overloaded method must be unique The signature includes the number, type, and order of the parameters

Method Overloading The compiler determines which method is being invoked by analyzing the parameters result = tryMe(25, 4.32) Invocation float tryMe(int x) { return x + .375; } float tryMe(int x, float y) return x*y;

Method Overloading The println method is overloaded: and so on... 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 (3);

Method Overloading The return type of the method is not part of the signature Overloaded methods cannot differ only by their return type Constructors can be overloaded and often are Overloaded constructors provide multiple ways to initialize a new object

Constructor Overloading Constructors can be overloaded and often are Overloaded constructors provide multiple ways to initialize a new object, for example: public Die() { faceValue = 1; } public Die(int val) { faceValue = val;} With this setup, we have two ways to create Die objects: Die die1 = new Die(); Die die2 = new Die(3);

Accessors and Mutators A class usually provides methods to indirectly access and modify the private data values An accessor method returns the current value of a variable A mutator method changes the value of a variable The names of accessor and mutator methods take the form getX and setX, respectively, where X is the name of the value They are sometimes called “getters” and “setters”

Mutator Restrictions The use of mutators gives the class designer the ability to restrict a client’s options to modify an object’s state A mutator is often designed so that the values of variables can be set only within particular limits For example, the setFaceValue mutator of the Die class should restrict the value to the valid range (1 to MAX)