1 / 16

Object Oriented Program Terms

***** SWTJC STEM *****. 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.

eyal
Download Presentation

Object Oriented Program Terms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ***** SWTJC STEM ***** 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. Object Oriented Program Terms Chapter 4-1 cg 42

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

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

  4. ***** SWTJC STEM ***** From this point on we need to be clear: We are no longer creating applications, but rather classes ofobjects. 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. Creating a Class Object Chapter 4-1 cg 42

  5. Declare attributes as private variables Set initial values ***** SWTJC STEM ***** • By declaring them as private variables,they areaccessible 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; } } Declaring Attributes Chapter 4-1 cg 42

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

  7. ***** SWTJC STEM ***** • 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); Mutating (Setting) Attributes . Object Name Method Name (parameters) Chapter 4-1 cg 43

  8. ***** SWTJC STEM ***** • 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(); Accessing (Getting) Attributes Chapter 4-1 cg 43

  9. ***** SWTJC STEM ***** 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. Handling Calculated Values Chapter 4-1 cg 43

  10. ***** SWTJC STEM ***** • 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()); } } Testing A Cylinder Object Chapter 4-1 cg 43

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

  12. ***** SWTJC STEM ***** • 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! Testing a Two Cylinder Object Chapter 4-1 cg 43

  13. ***** SWTJC STEM ***** 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. Instance Data and Scope Chapter 4-1 cg 43

  14. ***** SWTJC STEM ***** • 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. Visibility Modifiers Chapter 4-1 cg 43

  15. Visible inside object only Visible outside object Visible outside object ***** SWTJC STEM ***** • 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){ ... } Visibility Modifiers - Example Visible outside object Chapter 4-1 cg 43

  16. ***** SWTJC STEM ***** 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. Visibility Modifiers Support Method Chapter 4-1 cg 43

More Related