1 Chapter 6 - Object-Oriented Programming Object-oriented programming overview objects classes encapsulation UML Class Diagram First OOP Class private.

Slides:



Advertisements
Similar presentations
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 6 - Object-Oriented Programming
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Road Map Introduction to object oriented programming. Classes
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
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.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Writing Classes (Chapter 4)
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
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.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
10-Nov-15 Java Object Oriented Programming What is it?
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Chapter 9 - Classes with Class Members Class Variables Class Methods How to Access Class Members When to Use Class Members Class Constants Example Program.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
Chapter 9 Introduction to Arrays Fundamentals of Java.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
User-Written Functions
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object-Oriented Programming
6 Chapter Functions.
Object Oriented Programming in java
Classes, Objects and Methods
Chapter 7 Objects and Classes
Presentation transcript:

1 Chapter 6 - Object-Oriented Programming Object-oriented programming overview objects classes encapsulation UML Class Diagram First OOP Class private and public Access Driver Class Reference Variables and Instantiation Calling a Method Calling Object 1

2 The this Reference Default Values Variable Persistence OOP Tracing Procedure UML Class Diagram for Next Version of the Mouse Program Local Variables return statement void Return Type Empty return Statement Argument Passing Specialized methods: accessor methods mutator methods boolean methods Chapter 6 - Object-Oriented Programming

3 Object-Oriented Programming Overview In the old days, the standard programming technique was called "procedural programming." That's because the emphasis was on the procedures or tasks that made up a program. You'd design your program around what you thought were the key procedures. Today, the most popular programming technique is object-oriented programming (OOP). With OOP, instead of thinking first about procedures, you think first about the things in your problem. The things are called objects. 1

4 Object-Oriented Programming Overview An object is: A set of related data which identifies the current state of the object. +a set of behaviors Example objects: Car object in a traffic-flow simulation: data = ? methods = ? human entitiesphysical objectsmathematical entities employeescars in a traffic-flow simulationpoints on coordinate system customersAircraft in an air-traffic control systemComplex numbers studentsElectrical components in a circuit- design program time

5 Object-Oriented Programming Overview Benefits of OOP: Programs are more understandable - Since people tend to think about problems in terms of objects, it's easier for people to understand a program that's split into objects. Fewer errors - Since objects provide encapsulation (isolation) for the data, it's harder for the data to get messed up. 1 2

6 Object-Oriented Programming Overview A class is a description for a set of objects. On the next slide, note the three computers on a conveyer belt in a manufacturing plant: The three computers represent objects, and the specifications document represents a class. The specifications document is a blueprint that describes the computers: it lists the computers' components and describes the computers' features. Think of an object as a physical example for a class's description. More formally, we say that an object is an instance of a class. 1 2

7 Object-Oriented Programming Overview

8 A class is a description for a set of objects. The description consists of: a list of variables +a list of methods Classes can define two types of variables – instance variables and class variables. And classes can define two types of methods – instance methods and class methods. Instance variables and instance methods are more common than class variables and class methods, and we'll focus on instance variables and instance methods in this chapter and the next several chapters. 1 2

9 Object-Oriented Programming Overview A class's instance variables specify the type of data that an object can store. For example, if you have a class for computer objects, and the Computer class contains a hardDiskSize instance variable, then each computer object stores a value for the size of the computer's hard disk. A class's instance methods specify the behavior that an object can exhibit. For example, if you have a class for computer objects, and the Computer class contains a printSpecifications instance method, then each computer object can print a specifications report (the specifications report shows the computer's hard disk size, CPU speed, cost, etc.).

10 Object-Oriented Programming Overview Note the use of the term “instance” in “instance variable” and “instance method.” That reinforces the fact that instance variables and instance methods are associated with a particular object instance. For example, each computer object has its own value for the hardDiskSize instance variable. That contrasts with class variables and class methods, which you saw in Chapter 5. Class variables and class methods are associated with an entire class. For example, the Math class contains the PI class variable and the round class method. PI and round are associated with the entire Math class, not with a particular instance of the Math class. We'll cover class variables and class methods in more detail in Chapter 9. 1

11 UML Class Diagram UML: Stands for Unified Modeling Language. It's a diagrammatic methodology for describing classes, objects, and the relationships between them. It is widely accepted in the software industry as a standard for modeling OOP designs. Example: UML class diagram for a Mouse class: Mouse  class name age : int weight : double percentGrowthRate : double  attributes / variables setPercentGrowthRate(percentGrowthRate : double) grow() display()  operations / methods

12 First OOP Class /************************************************************ * Mouse.java * Dean & Dean * * This class models a mouse for a growth simulation program. ************************************************************/ public class Mouse { private int age = 0; // age of mouse in days private double weight = 1.0; // weight of mouse in grams private double percentGrowthRate; // % weight increase per day //********************************************************* // This method assigns the mouse's percent growth rate. public void setPercentGrowthRate(double percentGrowthRate) { this.percentGrowthRate = percentGrowthRate; } // end setPercentGrowthRate instance variable declarations To access instance variables, use this dot parameter method body

13 First OOP Class (hidden) /************************************************************ * Mouse.java * Dean & Dean * * This class models a mouse for a growth simulation program. ************************************************************/ public class Mouse { private int age = 0; // age of mouse in days private double weight = 1.0; // weight of mouse in grams private double percentGrowthRate; // % weight increase per day //********************************************************* // This method assigns the mouse's percent growth rate. public void setPercentGrowthRate(double percentGrowthRate) { this.percentGrowthRate = percentGrowthRate; } // end setPercentGrowthRate instance variable declarations To access instance variables, use this dot. parameter method body 5

14 First OOP Class //********************************************************* // This method simulates one day of growth for the mouse. public void grow() { this.weight += (.01 * this.percentGrowthRate * this.weight); this.age++; } // end grow //********************************************************* // This method prints the mouses's age and weight. public void display() { System.out.printf( "Age = %d, weight = %.3f\n", this.age, this.weight); } // end display } // end class Mouse 1 2 3

15 private and public Access private and public are access modifiers. When you apply an access modifier to a member of a class, you determine how easy it is for the member to be accessed. Accessing a member refers to either reading the member's value or modifying it. If you declare a member to be private, then the member can be accessed only from within the member's class. Instance variables are almost always declared with the private modifier because you almost always want an object's data to be hidden. Making the data hard to access is what encapsulation is all about and it's one of the cornerstones of OOP. If you declare a member to be public, then the member can be accessed from anywhere (from within the member's class, and also from outside the member's class). Methods are usually declared with the public modifier because you normally want to be able to call them from anywhere

16 Driver Class /**************************************** * MouseDriver.java * Dean & Dean * * This is a driver for the Mouse class. ****************************************/ import java.util.Scanner; public class MouseDriver { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); double growthRate; Mouse gus = new Mouse(); Mouse jaq = new Mouse();

17 Driver Class System.out.print("Enter growth rate as a percentage: "); growthRate = stdIn.nextDouble(); gus.setPercentGrowthRate(growthRate); jaq.setPercentGrowthRate(growthRate); gus.grow(); jaq.grow(); gus.grow(); gus.display(); jaq.display(); } // end main } // end class MouseDriver 1 2

18 Reference Variables and Instantiation To declare a reference variable (which holds the address in memory where an object is stored): ; To instantiate/create an object and assign its address into a reference variable: = new () Example code: Mouse gus; gus = new Mouse(); This single line is equivalent to the above two lines: Mouse gus = new Mouse(); declaration instantiation initialization

19 Calling a Method After instantiating an object and assigning its address into a reference variable, call/invoke an instance method using this syntax:. ( ); Here are three example instance method calls from the MouseDriver class: gus.setPercentGrowthRate(growthRate); gus.grow(); gus.display();

20 A calling object is the object that appears at the left of the dot in a call to an instance method. Can you find the calling objects below? public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); double growthRate; Mouse gus = new Mouse(); System.out.print("Enter growth rate as a percentage: "); growthRate = stdIn.nextDouble(); gus.setPercentGrowthRate(growthRate); gus.grow(); gus.display(); } // end main Calling Object 1 2 3

21 The this reference: When used in conjunction with a dot and an instance variable, "this" is referred to as the this reference. Note this example from the Mouse class's grow method: this.weight += (.01 * this.percentGrowthRate * this.weight); The this reference is used inside of a method to refer to the object that called the method; in other words, the this reference refers to the calling object. So what’s so great about having a special name for the calling object inside of a method? Why not just use the original name, gus or jaq, inside the method? Because if the original name were to be used, then the method would only work for the one specified calling object. By using a generic name ( this ) for the calling object, then the method is more general purpose. For example, by using this, the grow method is able to specify weight gain for any Mouse object that calls it. If gus calls grow, then gus ’s weight is updated, whereas if jaq calls grow, then jaq ’s weight is updated. The this Reference 1 2

22 A variable's default value is the value that the variable gets if there's no explicit initialization. Mouse class's instance variable declarations: private int age = 0; private double weight = 1.0; private double percentGrowthRate; Here are the default values for instance variables: integer types get 0 floating point types get 0.0 boolean types get false reference types get null Note that a String is a reference type so it gets null by default. Default Values explicit initializations percentGrowthRate gets default value of

23 A variable's persistence is how long a variable's value survives before it's wiped out. Instance variables persist for the duration of a particular object. Thus, if an object makes two method calls, the second called method does not reset the calling object's instance variables to their initialized values. Instead, the object's instance variables retain their values from one method call to the next. Variable Persistence 1 2

24 OOP Tracing Procedure OOP tracing procedure: 1. Provide a trace setup. Starting with the first line in main, trace the program in the normal fashion, but follow these additional rules: 2. When starting a method: Under the method's local variable headings, write local variable initial values. Use a question mark for local variables that are uninitialized. 3. When an object is instantiated: Under the object's class-name heading, provide a column heading named "obj#", where # is a unique number. Under the obj# heading, provide an underlined column heading for each of the object's instance variables. Under the instance variable headings, write instance variable initial values

25 OOP Tracing Procedure 4. When there's an assignment into a reference variable: Rather than writing the actual address that's assigned into the reference variable (and there's no way to know the actual address), write obj# under the reference variable's column heading, where obj# matches up with the associated obj# in the object portion of the trace. When there's a method call: Under the called method's this column heading, write the calling object's obj#. If the method call contains an argument, write the argument's value under the called method's associated parameter. On your program listing, jump to the first statement in the called method

26 OOP Tracing Procedure 6. When there's a this reference: Find the obj# under the current method's this column heading. Go to the found obj#'s heading and read or update the obj#’s value accordingly. 7. When finishing a method: In the method's section, draw a horizontal line under the last row that contains a transaction generated by that method. This indicates that the method has finished and the values in the method's local variables are wiped out. On your program listing, jump to the code that immediately follows the method call that called the just-finished method

27 Tracing the Mouse Program import java.util.Scanner; public class MouseDriver2 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); double growthRate; Mouse gus, jaq; System.out.print("Enter % growth rate: "); growthRate = stdIn.nextDouble(); gus = new Mouse(); gus.setPercentGrowthRate(growthRate); gus.grow(); gus.display(); jaq = new Mouse(); jaq.grow(); jaq.display(); } // end main } // end class MouseDriver2 There's a logic error here. We “accidentally” forget to initialize the growth rate in jaq ! 1

28 Tracing the Mouse Program public class Mouse { private int age = 0; // age of mouse in days private double weight = 1.0; // weight of mouse in grams private double percentGrowthRate; // % weight increase per day public void setPercentGrowthRate(double percentGrowthRate) { this.percentGrowthRate = percentGrowthRate; } // end setPercentGrowthRate public void grow() { this.weight += (.01 * this.percentGrowthRate * this.weight); this.age++; } // end grow public void display() { System.out.printf("Age = %d, weight = %.3f\n", this.age, this.weight); } // end display } // end class Mouse

29 Tracing the Mouse Program Use this trace setup for the Mouse program: Trace setup procedure: 1. Provide a heading for each class. 2. Under each class-name heading, provide a heading for each of the class's methods. 3. Under each method-name heading: Provide a heading named this for the method's calling object. Exception: Since main doesn't have a calling object, don't write this under main. Provide a heading for each of the method's parameters and local variables

30 Tracing the Mouse Program 1 2 3

31 Tracing the Mouse Program

32 UML Class Diagram for Next Version of the Mouse Program Member accessibility: Use "-" for private access and "+" for public access. Method notes: We use them here to specify local variables Initialization values: Use "= ". 6 7

33 Local Variables A local variable is a variable that's declared inside a method. That's different from an instance variable which is declared at the top of a class, outside all the methods. A local variable is called "local" because it can be used only inside of the method in which it is declared – it is completely local to the method. In the Mouse2Driver class on the next slide, note how the main method has three local variables - stdIn, mickey, and days. And in the Mouse2 class, note how the grow method has one local variable - i. 1

34 Mouse2Driver Class import java.util.Scanner; public class Mouse2Driver { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); Mouse2 mickey = new Mouse2(); int days; mickey.setPercentGrowthRate(10); System.out.print("Enter number of days to grow: "); days = stdIn.nextInt(); mickey.grow(days); System.out.printf("Age = %d, weight = %.3f\n", mickey.getAge(), mickey.getWeight()); } // end main } // end class Mouse2Driver local variables

35 Mouse2 Class import java.util.Scanner; public class Mouse2 { private int age = 0; // age in days private double weight = 1.0; // weight in grams private double percentGrowthRate; // % daily weight gain //******************************************************** public void setPercentGrowthRate(double percentGrowthRate) { this.percentGrowthRate = percentGrowthRate; } // end setPercentGrowthRate //******************************************************** public int getAge() { return this.age; } // end getAge 1 2

36 Mouse2 Class //******************************************************** public double getWeight() { return this.weight; } // end getWeight //******************************************************** public void grow(int days) { for (int i=0; i<days; i++) { this.weight += (.01 * this.percentGrowthRate * this.weight); } this.age += days; } // end grow } // end class Mouse2 local variable 1 2 3

37 The default value for a local variable is garbage. Garbage means that the variable's value is unknown - it's whatever just happens to be in memory at the time that the variable is created. When doing a trace, use a "?" to indicate garbage. If a program attempts to access a variable that contains garbage, the compiler generates an error. For example, what happens if you remove =0 from the grow method's for loop heading? Will the following code work? for (int i; i<days; i++) { this.weight += (0.01 * this.percentGrowthRate * this.weight); } Since i is no longer assigned zero, i contains garbage when the i<days condition is tested. That causes the compiler to generate this error message: variable i might not have been initialized Local Variable Default Values 2 1

38 Local variables persist only for the duration of the method (or for loop) in which the local variable is defined. The next time the method (or for loop) is executed, the local variable's value resets to its initial value. Local Variable Persistence 2 1

39 return Statement The return statement allows you to pass a value from a method back to the place where the method was called. Note the following example. From the Mouse2 class: public int getAge() { return this.age; } // end getAge From the Mouse2Driver class: System.out.printf("Age = %d, weight = %.3f\n", mickey.getAge(), mickey.getWeight()); Note the return type in the above example. It has to match the type of the value that's being returned in the return statement. return type return statement method call

40 void Return Type As shown in the below grow method from the Mouse2 class, if a method does not return a value, then the method must specify void for its return type. public void grow(int days) { for (int i=0; i<days; i++) { this.weight += (0.01 * this.percentGrowthRate * this.weight); } this.age += days; } // end grow 1

41 Empty return Statement For methods with a void return type, it's legal to have an empty return statement. The empty return statement looks like this: return; The empty return statement does what you'd expect: It terminates the current method and causes control to be passed to the calling module at the point that immediately follows the method call that called the current method.

42 Empty return Statement Suppose you'd like to model mouse growth only up through mouse adolescence. This grow method does that by stopping a mouse's growth after 100 days: public void grow(int days) { int endAge; endAge = this.age + days; while (this.age < endAge) { if (this.age >= 100) { return; } this.weight +=.01 * this.percentGrowthRate * this.weight; this.age++; } // end while } // end grow empty return statement 1 2 3

43 Empty return Statement Code that uses an empty return statement(s) can always be replaced by code that does not use the empty return statement(s). For example, here's a return-less version of the previous grow method: public void grow(int days) { int endAge; endAge = this.age + days; if (endAge > 100) { endAge = 100; } while (this.age < endAge) { this.weight += (.01 * this.percentGrowthRate * this.weight); this.age++; } // end while } // end grow 1 2

44 Empty return Statement Software engineering observation: Real-world programmers are often asked to maintain (fix and improve) other people's code. In doing that, they oftentimes find themselves having to examine the loops and, even more specifically, the loop termination conditions in the program they're working on. Therefore, it's important that the loop termination conditions are clear. Normally, loop termination conditions appear in standard places: while loop heading, do loop closing, for loop heading's condition part. However, in using a return statement inside a loop, the return statement introduces a loop termination condition that's not in one of the standard places. For example, in the grow method two slides ago, the return statement is "hidden" inside an if statement that's embedded in a while loop. In the interest of maintainability, you should use restraint when considering the use of a return statement inside of a loop. Based on the context, if inserting a return statement(s) inside a loop improves clarity, then feel free to insert. However, if it simply makes the coding chores easier and it does not add clarity, then don't insert. 1

45 Argument Passing What is the output for the following Mouse3Driver and Mouse3 classes? public class Mouse3Driver { public static void main(String[] args) { Mouse3 minnie = new Mouse3(); int days = 365; minnie.grow(days); System.out.println("# of days aged = " + days); } // end main } // end class Mouse3Driver 1 2

46 Argument Passing public class Mouse3 { private int age = 0; // age in days private double weight = 1.0; // weight in grams private double percentGrowthRate = 10; // % daily weight gain //******************************************************** public void grow(int days) { this.age += days; while (days > 0) { this.weight += (.01 * this.percentGrowthRate * this.weight); days--; } } // end grow } // end class Mouse3 1

47 Argument Passing Java uses the pass-by-value mechanism to pass arguments to methods. Pass-by-value means that the JVM passes a copy of the argument's value (not the argument itself) to the parameter. Thus, if the parameter's value changes within the method, the argument in the calling module is unaffected. For example, in the previous two program slides, even though the days value within the grow method changes, the main method's days value is unaffected.

48 Argument Passing An argument and its associated parameter often use the same name. For example, we use days for the argument in Mouse3Driver 's grow method call, and we also use days for the parameter in Mouse3 's grow method heading. But be aware that an argument and its associated parameter don't have to use the same name. The only requirement is that an argument and its associated parameter are the same type. For example, if num is an int variable, then this method call successfully passes num 's value into the days parameter: minnnie.grow(num); As another example, since 365 is an int value, the following method call successfully passes 365 into the days parameter: minnie.grow(365); 1 2

49 Specialized Methods Accessor methods - They simply get/access the value of an instance variable. Example: public int getAge() { return this.age; } Mutator methods - They simply set/mutate the value of an instance variable. Example: public void setPercentGrowthRate(double percentGrowthRate) { this.percentGrowthRate = percentGrowthRate; } // end setPercentGrowthRate

50 Specialized Methods boolean methods - They check the truth or falsity of some condition. They always return a boolean value. They should normally start with "is". For example, here's an isAdolescent method that determines whether a Mouse object's age is ≤ 100: public boolean isAdolescent() { if (this.age <= 100) { return true; } else { return false; } } // end isAdolescent Here's how the isAdolescent method might be used in main : Mouse pinky = new Mouse();... if (pinky.isAdolescent() == false) { System.out.println( "The Mouse's growth is no longer" + " being simulated - too old."); } 2 1 3

51 Chapter 6 - Quiz Questions 1.Which of the following is a data item in a class? a)instance variable b)method c)public modifier 3.Given this method call. What is the thing called that's inside the parentheses? car.assignSpeed(speed); a)an argument b)a passenger c)a cargo 2.What word is used to create an object? a)new b)create c)allocate

52 Chapter 6 - Quiz Questions 4.What's the formal name for a method like setAge that sets the value of an instance variable? a)accessor method b)mutator method c)protracted method