Presentation is loading. Please wait.

Presentation is loading. Please wait.

***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.

Similar presentations


Presentation on theme: "***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented."— Presentation transcript:

1 ***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented style. Applications typically address one task at a time such as finding the area of a circle. If we were to continue writing an application program for each task, we would soon be overwhelmed trying to organize and access our programs. This is where object oriented programming can help. Instead of writing many individual applications to describe one kind or “class” of problem, we will create a Java class that will combine them all into a single, logically accessible object. Let’s look at the cylinder volume problem to see how this works.

2 ***** SWTJC STEM ***** Chapter 4-1 cg 42 Procedure Oriented Solution Cylinder Volume Calculate Volume of Cylinder V = PI * r 2 *h Radius r = 5 Cylinder Volume = 785 Height h = 10 This is the solution diagramed in procedural form.

3 ***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Solution Cylinder Volume Attributes radius, height ----------------------- Methods Set radius, height Get radius, height, and volume Cylinder Object Messages to/from Object setRadius(5) setHeight(10) getRadius( ) returns 5 getHeight( ) returns 10 getVolume( ) returns 785 This is the solution diagramed in object form.

4 ***** SWTJC STEM ***** Chapter 4-1 cg 42 Creating a Class Object From this point on we need to be clear: We are no longer creating applications, but rather classes of objects. In the present example, from the Cylinder class from which we can instantiate or create an instance of one or more cylinder objects. We can use JBuilder to create our new class. In the “Class Wizard”, check “Generate default constructor” box. The constructor is a special method with the same name as the class that helps set up and initialize the object. Each attribute of the class must have a instance variable declared as private. By making them private, each instance of cylinder object created from the class will have its own set of attributes. For the cylinder class, “radius” and “height” are the stored attributes for which we need to set up private variables.

5 ***** SWTJC STEM ***** Chapter 4-1 cg 42 Declaring Attributes By declaring them as private variables, they are accessible only from within the object using the “get” and “set” methods to be described later. Thus, we have encapsulated them, that is, removed them from direct public access as variables. Finally, we will use the constructor to set the initial values of radius and height. The code so far looks like this: public class Cylinder { private double radius, height; // Constructor public Cylinder(double value1, double value2) { radius = value1;height = value2; } Declare attributes as private variables Set initial values

6 ***** SWTJC STEM ***** Chapter 4-1 cg 43 Constructing an Object To “construct” a cylinder object “myCylinder” with radius 5 and height 10 from the Cylinder class, use the statement: Cylinder myCylinder = new Cylinder(5, 10); Class Name (Appears Twice) Object Name (Your Choice) Java Reserved Word Initialization Parameters (radius,height)

7 ***** SWTJC STEM ***** Chapter 4-1 cg 43 Mutating (Setting) Attributes Within the object, we need a way to set or mutate the attributes, radius and height. For each attribute we will create a setter or mutator method. // Radius mutator – Sets radius public void setRadius(double value){ radius = value; } // Height mutator – Sets height public void setHeight(double value){ height = value; } To set the radius to 4 and height to 8, use: myCylinder.setRadius(4); myCylinder.setHeight(8); Object NameMethod Name (parameters).

8 ***** SWTJC STEM ***** Chapter 4-1 cg 43 Accessing (Getting) Attributes To get or access attributes (radius, height, and volume), we create getter or accessor methods. // Radius accessor – Get radius public double getRadius(){ return radius; } // Height accessor – Get height public double getHeight(){ return height; } // Volume accessor – Calculate and get volume public double getVolume(){ return Math.PI * radius * radius * height; } To get the attributes use: myCylinder.getRadius(); myCylinder.getHeight(); myCylinder.getVolume();

9 ***** SWTJC STEM ***** Chapter 4-1 cg 43 Handling Calculated Values As a general rule, calculated values, such as “volume” in the example, are not stored or set within a class. In the cylinder class, “volume” is always calculated from given a radius and height. Thus, there is no variable for volume and no need to “set” it with a mutator method. There is an accessor or getter method to “get” volume that does a direct calculation before returning a value.

10 ***** SWTJC STEM ***** Chapter 4-1 cg 43 Testing A Cylinder Object Here’s a driver application to test a cylinder object. public class CylinderTest { public static void main(String[] args) { //First construct a cylinder object with radius 5 & height 10 Cylinder myCylinder = new Cylinder(5, 10); // Get (access) volume System.out.println("radius= " + myCylinder.getRadius()); System.out.println("height= " + myCylinder.getHeight()); System.out.println("volume= " + myCylinder.getVolume()); // Set (mutate) radius to 4 and height to 8 myCylinder.setRadius(4); myCylinder.setHeight(8); // Get (access) radius, height, and volume System.out.println("radius= " + myCylinder.getRadius()); System.out.println("height= " + myCylinder.getHeight()); System.out.println("volume= " + myCylinder.getVolume()); } }

11 ***** SWTJC STEM ***** Chapter 4-1 cg 43 The Results Running CylinderTest we get: radius = 5.0 height = 10.0 volume = 785.3981633974483 radius = 4.0 height = 8.0 volume = 402.1238596594935

12 ***** SWTJC STEM ***** Chapter 4-1 cg 43 Testing a Two Cylinder Object Let’s create (instantiate) two cylinder objects, each with its own set of attributes. See MultipleCylinder.java.... Cylinder testCylinder1 = new Cylinder(5,10); // Instance 1 Cylinder testCylinder2 = new Cylinder(4,8); // Instance 2... ** Cylinder (Instance) 1 ** radius1 = 5.0 height1 = 10.0 volume1 = 785.3981633974483 ** Cylinder (Instance) 2 ** radius2 = 4.0 height2 = 8.0 volume2 = 402.1238596594935 Note the cylinder objects are fully independent!

13 ***** SWTJC STEM ***** Chapter 4-1 cg 43 Instance Data and Scope Attributes such as the variables radius and height are called instance variables because memory space is allocated for each instance of the object that is created. Thus, both cylinder 1 and 2 may have different “radii” and “heights”. A variable’s scope is the area within a program in which that variable can be referenced. In Java, the scope is determined by the pair of braces in which the variable is declared. In the cylinder example, radius is declared at the class level (open brace after class statement) and may be referenced in all methods in the class (until the closing brace for the class). A variable declared within a method may be referenced only within that method; i.e., within the corresponding method’s braces.

14 ***** SWTJC STEM ***** Chapter 4-1 cg 43 Visibility Modifiers The scope of class members (variables and methods) within a class can be affected by the visibility modifiers “public” and “private”. If a member of a class has public visibility, it can be directly referenced from outside the object. If a member of a class has private visibility, it can be referenced from within the class, but not from outside. In general, public visibility violates encapsulation. Instance variables should be declared as private. Support methods used only within a class should be declared as private. Service methods used by other classes should have public visibility. Let’s examine the example program and discuss the visibility of its members.

15 ***** SWTJC STEM ***** Chapter 4-1 cg 43 Visibility Modifiers - Example The attributes radius and height are instance variables and therefore declared private: private double radius, height; The class definition and all service methods should have public visibility and be declared public: public class Cylinder {... public void setRadius(double value){... }... public void setHeight(double value){... } Visible outside object Visible inside object onlyVisible outside object

16 ***** SWTJC STEM ***** Chapter 4-1 cg 43 Visibility Modifiers Support Method The example does not have a support method. If it did it would be declared private and only invoked within the object itself. In the next lecture we will add a support method to the example.


Download ppt "***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented."

Similar presentations


Ads by Google