Presentation is loading. Please wait.

Presentation is loading. Please wait.

Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.

Similar presentations


Presentation on theme: "Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide."— Presentation transcript:

1 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #1 Classes and Objects Insect class The Insect class defines the fields and methods that will exist in all objects that are an instances of the Insect class. housefly object The housefly object is an instance of the Insect class. mosquito object The mosquito object is an instance of the Insect class. The process of creating a new object is called instantiation.

2 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #2 Why? Programming simulates real world activities (usually). The real world is populated by objects, actors who behave in certain ways. Object oriented programming is a natural way to simulate the objects and behaviors in real world activity. (but you can’t take the analogy too far)

3 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #3 Also why Objects provide a way to encapsulate data and behavior into one package. That package will operate in known ways and then can become part of a larger whole. Look at the java services we have used. They are classes and objects which are “tried and true”. OOP fosters quality code and code reuse.

4 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #4 Object-Oriented Programming Object Data (Fields) Methods That Operate on the Data

5 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #5 Object-Oriented Programming Object-oriented programming combines data and behavior via encapsulation. Data hiding is the ability of an object to hide data from other objects in the program. Only an object’s methods should be able to directly manipulate its data. Other objects are allowed manipulate an object’s data via the object’s methods.

6 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #6 Object-Oriented Programming Object Data (Fields) typically private to this object Methods That Operate on the Data Code Outside the Object

7 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #7 Object-Oriented Programming Data Hiding In object-oriented programming, a class “knows itself”. Data is accessed only through well-defined interfaces. These interfaces are the methods. The interfaces protect the data from corruption.

8 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #8 An Everyday Example of an Object—An Alarm Clock Fields define the state that the alarm is currently in. The current second (a value in the range of 0-59) The current minute (a value in the range of 0-59) The current hour (a value in the range of 1-12) The time the alarm is set for (a valid hour and minute) Whether the alarm is on or off (“on” or “off”)

9 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #9 An Everyday Example of an Object—An Alarm Clock Methods are used to change a field’s value Set time Set alarm time Turn alarm on Turn alarm off Increment the current second Increment the current minute Increment the current hour Sound alarm Public methods are accessed by users outside the object. Private methods are part of the object’s internal design.

10 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #10 Classes and Instances Many objects can be created from a class. Each object is independent of the others. String person = “Jenny”; String pet = “Fido”; String favoriteColor = “Blue”;

11 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #11 Classes and Instances Each instance of the class String contains different data. The instances are all share the same design. Each instance has all of the attributes and methods that were defined in the String class. Classes are defined to represent a single concept or service.

12 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #12 Note for this class Our focus this semester is on carrying out activities. Design of classes will be left for next semester. You will be given the methods and attributes to use in your classes. You will be welcome to add more methods as you find helpful.

13 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #13 Building a Rectangle class Think about a Rectangle. Draw one. What are the pertinent features that describe your rectangle? Draw a different one that is of a different size? Do the pertinent features remain the same?

14 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #14 Building a Rectangle class A Rectangle object will have the following fields (attributes): length. The length field will hold the rectangle’s length. width. The width field will hold the rectangle’s width. These are the pertinent data that describe any rectangle.

15 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #15 What behaviors should be included?

16 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #16 Building a Rectangle class The Rectangle class will also have the following methods (behaviors): setLength. stores a value in this Rectangle’s length field. setWidth. stores a value in this Rectangle’s width field. getLength. returns the value in this Rectangle’s length field. getWidth. returns the value in this Rectangle’s width field. getArea. returns the area of the this Rectangle.

17 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #17 Let’s build a Rectangle class Demo the rectangle class

18 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide #18 Header for the setLength Method public void setLength (double len) Access specifier Return Type Parameter variable declaration Method Name Notice the word static does not appear in the method header designed to work on an instance of a class (instance method).


Download ppt "Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide."

Similar presentations


Ads by Google